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
-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.
- You have a Dockerfile that defines a
- You build the docker image and tag it
- You run the container
- 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
--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.