Docker Volume Notes

Russell Bateman

  1. A Docker volume is a filesystem mounted on a Docker container to preserve data generated by processes in the running container. A volume's data is generally stored on the host of the container and independently of the container's lifecycle. Such a volume enables a user to back up data and share a filesystem between containers and/or the host itself. This is the preferred way to persist data in Docker by separating the container's lifecycle from its data.

  2. There are different commands associated with volumes.
    • $ docker run --volume
    • $ docker run --mount
    • Still other commands are
      • $ docker volume create volume-name
      • $ docker volume list
      • $ docker volume inspect volume-name
      • $ docker volume rm volume-name
  3. See Docker Volume Mount.

  4. See Docker volumes explained in 6 minutes.

  5. A Docker volume is a subdirectory in the physical host filesystem that's mounted into the Docker container's virtual filesystem.

  6. Anything what goes on in the Docker container does to the volume is visible in the host filesystem.

  7. Anything the host might do in the subdirectory mapped into the Docker container's filesystem is visible to the container.

  8. Docker's host-volume command:
    $ docker run --volume /home/mount/data:/var/lib/data
                          ----------------------- -------------------
                          host filesystem         container's filesystem
    In the host filesystem, this volume is mapped at /home/mount/data (duh).

  9. Docker's anonymous-volume command:
    $ docker run --volume /var/lib/data
                          container's filesystem
    In the host filesystem, this volume is mapped at /var/lib/docker/volumes/random-hash/_data.

  10. Docker's named-volume command (best-practice):
    $ docker run --volume name:/var/lib/data
                                 container's filesystem
    In the container filesystem, the path to this volume is known by name and it isn't necessary to know the actual path.