3. docker volumes

3.1. Volumes vs bind mounts

Read the overview [docker-volumes-01] for:

  • Overview of mount types(volumes, bind mounts, tmpfs mounts, named pipes)

  • Typical use cases for each type

  • Tips for bind mounts or volumes (what happens when mounting to a directory in the container in which some files or directories exist)

In general, the docker docs recommend to use volumes [docker-volumes-02] over bind mounts [docker-volumes-03]

3.2. VOLUME declaration in Dockerfile

Docker docs [docker-volumes-04]:

  • The VOLUME instruction creates a mount point with the specified name and marks it as holding externally mounted volumes from native host or other container

  • The docker run command initializes the newly created volume with any data that exists at the specified location within the base image

Difference between Dockerfile VOLUME, docker volume command, docker run -v [docker-volumes-05]:

  • When defined in the Dockerfile, each time a container is started from the image, the volume is created, even if the docker run does not have any -v option.

  • docker volume create creates a volume without having to define a Dockerfile and build an image and run a container

  • You can declare it on runtime docker run -v [host-dir:]container-dir. combining the two (VOLUME + docker run -v) means that you can mount the content of a host folder into your volume persisted by the container in /var/lib/docker/volumes/…

3.3. References