Understanding Docker Volumes and Host‑Container Data Sharing
This article explains how Docker volumes work, how to mount host directories into containers using the -v and --mount options, and demonstrates data persistence by creating a volume, inspecting its storage location, and verifying that files created inside the container appear on the host.
Docker stores its volumes under /var/lib/docker. When you need to share a host directory with a container—such as mapping MySQL's /var/lib/mysql to the host—you use Docker's volume mechanism.
To share data between the host and a container, simply add the -v flag when starting the container, using the format -v HOST_PATH:CONTAINER_PATH.
For example, to mount the host directory /data to /usr/local/data inside a MySQL container, run: docker run -v /data:/usr/local/data -it mysql After the container starts, the files in /usr/local/data inside the container correspond to /data on the host, and the data persists across container restarts.
Docker volumes are implemented by creating a directory for each volume under /var/lib/docker/volumes. Each volume directory contains an _data subdirectory that holds the actual files. The volume bypasses Docker's union file system and directly mounts the host directory into the container.
Example workflow:
1. Create a volume named volume-data: docker volume create volume-data 2. List the volumes directory to see the new volume: ls -l /var/lib/docker/volumes 3. Inspect the volume's content: ls -l /var/lib/docker/volumes/volume-data You will see an _data folder inside the volume directory.
4. Run a container and mount the volume to /data:
docker run -it --mount source=volume-data,target=/data busybox5. Inside the container, create a file: cd /data && touch data.log 6. On the host, verify the file appears in the volume's _data directory: ls -l /var/lib/docker/volumes/volume-data/_data The presence of data.log confirms that operations inside the container directly affect the host's storage, providing persistent data.
In summary, Docker volumes are stored under /var/lib/docker/volumes, each with an _data subdirectory. When a container is started with --mount (or -v), Docker maps the host directory to the container, enabling data persistence across container lifecycles.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Practical DevOps Architecture
Hands‑on DevOps operations using Docker, K8s, Jenkins, and Ansible—empowering ops professionals to grow together through sharing, discussion, knowledge consolidation, and continuous improvement.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
