Docker images are read-only. However, containers put a thin “read-write layer” on top of the image. That is, they can change the files and folders in the image without actually changing the image. If the container is stopped and removed, all data written in it is lost. There are different options to manage the data.
Volumes
Volumes are folders (and files) on your host machine that are linked to folders / files within a container. Docker creates and manages volumes.
There are two types of Volumes:
- Anonymous Volumes
- Named Volumes:
Anonymous volumes are not given an explicit name when they are first mounted into a container, thus Docker assigns them a random name that is guaranteed to be unique within a specific Docker host. Apart from the name, named and anonymous volumes behave the same.
Anonymous volume
docker run -v /path/in/container nginx:latest
Named volume
docker run -v some-name:/path/in/container nginx:latest
Bind Mounts
Bind Mounts are very similar to Volumes - the key difference is, that the user, set the path on the host machine that should be connected to some path inside of a Container.
docker run -v /path/on/your/host/machine:path/in/container nginx:latest
tmpfs mounts
If docker is running on Linux, there is a third option: tmpfs mounts. A tmpfs mount is just transient and is only stored in the host’s memory. When the container exits, the tmpfs mount is deleted, and all files written there are lost.
docker run --tmpfs /home -d nginx:latest
References