Volumes decouple the life of the data being stored in them from the
life of the container that created them. This makes it so you can docker rm my_container
and your data will not be removed.
A volume can be created in two ways:
Specifying VOLUME /some/dir
in a Dockerfile
Specying it as part of your run command as docker run -v /some/dir
Either way, these two things do exactly the same thing. It tells
Docker to create a directory on the host, within the docker root path
(by default /var/lib/docker
), and mount it to the path you've specified (/some/dir
above). When you remove the container using this volume, the volume itself continues to live on.
If the path specified does not exist within the container, a directory will be automatically created.
You can tell docker to remove a volume along with the container:
docker rm -v my_container
Sometimes you've already got a directory on your host that you want to use in the container, so the CLI has an extra option for specifying this:
docker run -v /host/path:/some/path ...
This tells docker to use the specified host path specifically,
instead of creating one itself within the docker root, and mount that to
the specified path within the container (/some/path
above).
Note, that this can also be a file instead of a directory. This is commonly referred to as a bind-mount within docker terminology (though technically speaking, all volumes are bind-mounts in the sense of what is actually happening). If the path on the host does not exist, a directory will be automatically be created at the given path.
From the docker documentation:
VOLUME ["/data"]
The VOLUME
instruction creates a mount point with the
specified name and marks it as holding externally mounted volumes from
native host or other containers. The value can be a JSON array, VOLUME ["/var/log/"]
, or a plain string with multiple arguments, such as VOLUME /var/log
or VOLUME /var/log /var/db
.
For more information/examples and mounting instructions via the Docker
client, refer to Share Directories via Volumes documentation.
The docker run command initializes the newly created volume with any data that exists at the specified location within the base image. For example, consider the following Dockerfile snippet:
FROM ubuntu
RUN mkdir /myvol
RUN echo "hello world" > /myvol/greeting
VOLUME /myvol
This Dockerfile results in an image that causes docker run to create a new mount point at /myvol
and copy the greeting file into the newly created volume.
Comments
Post a Comment
https://gengwg.blogspot.com/