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

Rajesh Kumar
Total Page Visits: 334 - Today Page Visits: 0