Understanding and Implementing Docker Volumes for Data Persistence

Docker's core philosophy involves packaging applications and their environments into containers. While containers facilitate the execution of applications, the data generated within them is typically ephemeral. Without explicit measures, this data is lost when the container is removed. Docker addresses this by introducing volumes, which serve as a mechanism for persistent storage and data sharing between containers. Essentially, volumes are directories or files that are mounted into one or more containers by Docker. Crucially, they are not part of the container's copy-on-write filesystem, allowing them to persist data independently of the container's lifecycle. This independence is a key feature, ensuring that Docker does not delete volume data when a container is removed. Key characteristics of Docker volumes include:

  • Data can be shared or reused across multiple containers.
  • Changes made within a volume are immediately reflected.
  • Modifications to volume data do not affect existing image layers.
  • A volume's lifecycle persists as long as at least one container is using it.

These features enable two primary use cases: container data persistence and the inheritance and sharing of data between containers. ### Managing Volumes

Adding Volumes to Containers

1. Using Direct Commands

You can directly mount a host directory into a container using the -v flag with a absolute path: ``` docker run -it -v /path/on/host:/path/in/container image_name


This command ensures that data written to `/path/in/container` within the container is persisted in `/path/on/host` on the host machine, and vice versa. To make the container's content read-only within the mounted volume, append `:ro`: ```
docker run -it -v /path/on/host:/path/in/container:ro image_name
2. Using Dockerfile

The VOLUME instruction in a Dockerfile can declare one or more volumes for a image. ```

Example Dockerfile snippet

FROM centos VOLUME /data


When you build an image with this instruction and then run a container from it, Docker automatically creates a directory for the volume on the host machine and mounts it to the specified path inside the container. The host path is managed by Docker and can be found using Docker commands. ##### 3. Notes on Permissions

If you encounter permission denied errors when Docker attempts to access a mounted host directory, adding the `--privileged=true` flag to your `docker run` command can often resolve the issue. ### Volume Containers

#### 1. Concept

A volume container is a container specifically designated to hold shared volumes. Other containers can then mount these volumes from the volume container, facilitating data sharing. #### 2. Sharing Data Between Containers

To share data, you can start a container (e.g., `doc1`) and use it as a source for volumes. Then, other containers (`doc2`, `doc3`) can be launched using the `--volumes-from` flag, referencing the source container. ```
# Start a container with volumes
docker run -it --name doc1 -v /dataVolumeContainer1:/data1 my_image

# Start other containers inheriting volumes from doc1
docker run -it --volumes-from doc1 --name doc2 my_image
docker run -it --volumes-from doc1 --name doc3 my_image

Any data written to /data1 in doc1 will be accessible in doc2 and doc3. Similarly, data written to their respective volumes will be shared across all three. #### 3. Data Persistence and Lifecycle

When a volume container (doc1) is removed, the data within its volumes is not deleted as long as other containers (doc2, doc3) are still using those volumes. The volume's lifecycle is tied to its usage by containers, not the lifecycle of the initial volume container. If you create a new container (doc4) that inherits volumes from a container (doc3) which is subsequently deleted, doc4 will retain access to the data. This demonstrates that volume data persists until no containers are referencing it.

Tags: docker volumes data persistence containerization data sharing

Posted on Sun, 06 Sep 2026 16:13:02 +0000 by ltoso