Understand docker volume from beginner to deep dive level

In order to understand what a Docker volume is, we first need to be clear about how the filesystem normally works in Docker. Docker images are stored as series of read-only layers. When we start a container, Docker takes the read-only image and adds a read-write layer on top. If the running container modifies an existing file, the file is copied out of the underlying read-only layer and into the top-most read-write layer where the changes are applied. The version in the read-write layer hides the underlying file, but does not destroy it — it still exists in the underlying layer.

When a Docker container is deleted, relaunching the image will start a fresh container without any of the changes made in the previously running container — those changes are lost. Docker calls this combination of read-only layers with a read-write layer on top a Union File System.

Docker has two options for containers to store files in the host machine, so that the files are persisted even after the container stops: volumes, and bind mounts. If you’re running Docker on Linux you can also use a tmpfs mount.

An easy way to visualize the difference among volumes, bind mounts, and tmpfs mounts is to think about where the data lives on the Docker host.

  1. Volumes – Volumes are stored in a part of the host filesystem which is managed by Docker (/var/lib/docker/volumes/ on Linux). Volumes are the best way to persist data in Docker.
  2. Bind mounts – Bind mounts may be stored anywhere on the host system. They may even be important system files or directories. Non-Docker processes on the Docker host or a Docker container can modify them at any time.
  3. tmpfs – tmpfs mounts are stored in the host system’s memory only, and are never written to the host system’s filesystem.

What are docker volumes?
Docker volumes are the preferred mechanism for persisting data generated by and used by Docker Containers. These are use of Volume…

  1. Decoupling container from storage
  2. Share volume (storage/data) among different containers
  3. Attach Volume to a container
  4. On Deleting container volume does not delete

Commands Collection

# How to create docker volume?
$ docker volume create myvol1

# How to list the docker volume?
$ docker volume ls

# How to inspect docker volume?
$ docker volume inspect vol-name

# How to delete docker volume?
$ docker volume rm vol-name

# How to attach Volume to a containers? or
# How to share volume among containers?
$ docker run --name jenkins1 -v myvol1:/var/jenkins_home -p 8080:8080 -p 50000:50000 jenkins
$ docker run --name jenkins2 -v myvol1:/var/jenkins_home -p 8085:8080 -p 50005:50000 jenkins[

# How to use Bind mounts storage in Docker Volume?
$ docker run --name jenkins2 -v /opt/jenkins:/var/jenkins_home -p 8090:8080 -p 50010:50000 jenkins
$ docker run --name jenkins2 -v /opt/jenkins:/var/jenkins_home -p 8095:8080 -p 50015:50000 jenkins

# Difference between the -v or --mount flag in Docker?
Originally, the -v or --volume flag was used for standalone containers and the --mount flag was used for swarm services. However, starting with Docker 17.06, you can also use --mount with standalone containers. In general, --mount is more explicit and verbose.

# The biggest difference is that the -v syntax combines all the options together in one field, while the --mount syntax separates them.
$ docker run -d --name devtest --mount source=myvol2,target=/app nginx:latest
$ docker run -d --name devtest -v myvol2:/app nginx:latest

# This commands will not work as its --mount require only volume type. but not the mounts.
docker run -d --name devtest --mount source=/opt/backup1,target=/var/jenkins_home jenkins
Rajesh Kumar