Sharing Data
Often, you need multiple containers to share data. Docker volumes and bind mounts make this easy. You can also share data between containers using a common volume or a shared network filesystem.
Sharing a Volume Between Containers
Create a volume and mount it into two or more containers. For example, share a volume named
shared-data between a writer and a reader container:docker volume create shared-data
docker run -d --name writer -v shared-data:/data alpine sh -c "while true; do date >> /data/log.txt; sleep 5; done"
docker run -it --name reader -v shared-data:/data alpine cat /data/log.txtThe reader sees the file written by the writer.Sharing Data via Bind Mounts
You can also use a bind mount to a host directory and share that among containers. This is often used in development to share source code.
docker run -v $(pwd):/app -w /app node npm install
docker run -v $(pwd):/app -p 3000:3000 node npm startBoth containers share the same host directory.Data Sharing with Docker Compose (Preview)
In Docker Compose, you can define volumes and share them across services. This will be covered in detail in the next module.
Volume Drivers and External Storage
For advanced scenarios, you can use volume drivers to mount cloud storage (like AWS EBS) or network filesystems (like NFS) into containers. This is useful for clustered environments.
Sharing Data Between Containers Without a Common Volume
If you don’t want to use a volume, containers can also share data over a network (e.g., a database container and an app container). But for file‑based sharing, volumes are the way to go.
Two Minute Drill
- Multiple containers can mount the same volume to share files.
- Bind mounts also allow sharing but are less portable.
- Volumes are the recommended way to share data in production.
- For distributed storage, use volume drivers.
Need more clarification?
Drop us an email at career@quipoinfotech.com
