Master Docker Data Volumes: Sharing Data Between Containers and Hosts
This guide explains how Docker data volumes enable file‑system sharing between containers and the host, covering volume concepts, mounting host directories, and using --volumes‑from to share data across multiple containers, with practical command examples and visual illustrations.
Data Volume Concept
A data volume is a channel between a container and the external file system, allowing connection to the host’s file system or to other containers at the file level. Containers write data to their own volume, which other containers can mount and manipulate.
Typically a volume is a directory that can be mounted from the host or shared with other containers, solving data sharing between containers and the host or between containers.
Two Forms of Data Sharing
1. Mount a host directory as a volume
Mount an existing local directory into a container as a volume:
sudo docker run -d -P --name web -v /src/webapp:/opt/webapp training/webappThe -v option creates a volume; /src/webapp:/opt/webapp maps the host directory before the colon to the container directory after the colon.
2. Share volumes between containers
Container A creates a volume: sudo docker run -it -v /dbdata --name dbdata ubuntu Container B mounts the volume from container A using --volumes-from: sudo docker run -it --volumes-from dbdata --name db1 ubuntu Any write to the shared directory by one container is visible to the others. Multiple containers can be attached using repeated --volumes-from parameters.
Example of mounting a volume from one container into another:
sudo docker run -d --name db2 --volumes-from db1 ubuntuSigned-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.
Java High-Performance Architecture
Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.
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.
