The VOLUME
command will mount a directory inside your container and store any files created or edited inside that directory on your hosts disk outside the container file structure, bypassing the union file system.
The idea is that your volumes can be shared between your docker containers and they will stay around as long as there's a container (running or stopped) that references them.
You can have other containers mount existing volumes (effectively sharing them between containers) by using the --volumes-from
command when you run a container.
The fundamental difference between VOLUME
and -v
is this: -v
will mount existing files from your operating system inside your docker container and VOLUME
will create a new, empty volume on your host and mount it inside your container.
Example:
- You have a Dockerfile that defines a
VOLUME /var/lib/mysql
.
- You build the docker image and tag it
some-volume
- You run the container
And then,
- You have another docker image that you want to use this volume
- You run the docker container with the following:
docker run --volumes-from some-volume docker-image-name:tag
- Now you have a docker container running that will have the volume from
some-volume
mounted in /var/lib/mysql
Note: Using --volumes-from
will mount the volume over whatever exists in the location of the volume. I.e., if you had stuff in /var/lib/mysql
, it will be replaced with the contents of the volume.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…