Persisting container data
https://docs.docker.com/get-started/docker-concepts/running-containers/persisting-container-data/
コンテナがスタートすると、imageによって提供されたファイルや設定を利用する。コンテンが削除されると、それらの変更は削除される・
When a container starts, it uses the files and configuration provided by the image. Each container is able to create, modify, and delete files and does so without affecting any other containers. When the container is deleted, these file changes are also deleted.
永続化データを利用する場合にこまる
While this ephemeral nature of containers is great, it poses a challenge when you want to persist the data. For example, if you restart a database container, you might not want to start with an empty database. So, how do you persist files?
Container volumes
Volumesはコンテナのライフサイクルを超えて永続化データを提供するストレージメカニズム。
Volumes are a storage mechanism that provide the ability to persist data beyond the lifecycle of an individual container. Think of it like providing a shortcut or symlink from inside the container to outside the container.
log-dataというvolumeを作成する。
code:bash
docker volume create log-data
volumeがコンテナの/logsへマウントされる。log-datavolumeが存在しない場合はDockerが自動作成する。
code:bash
docker run -d -p 80:80 -v log-data:/logs docker/welcome-to-docker
コンテナが起動すると、/logsフォルダーへの書き込みはコンテナ外のvolumeに保存される。コンテナを削除して、同様のvolumeを使って新たなコンテナを起動するとファイルが存在したままになる。
When the container runs, all files it writes into the /logs folder will be saved in this volume, outside of the container. If you delete the container and start a new container using the same volume, the files will still be there.
Docker におけるデータ管理とマウントタイプ(volume・bind mount)