This guide explains how to share data between Docker containers using volumes, bind mounts, and shared network storage. You’ll learn several methods to persist and exchange data, from simple named volumes to network-attached storage, with practical steps for each approach.
What You’ll Need
- Docker installed on your system (Docker Desktop for Windows/Mac, or Docker Engine on Linux)
- Basic familiarity with the command line and Docker commands like
docker runanddocker exec - Access to a terminal or command prompt with permissions to run Docker commands
Step-by-Step
- Understand the options. Docker offers three main ways to share data between containers: volumes (managed by Docker), bind mounts (map a host directory), and tmpfs mounts (in-memory, not persistent). For sharing between containers, volumes and bind mounts are most common.
- Create a named volume. Run
docker volume create shared-data. This creates a volume that Docker manages, stored in its own directory on the host. - Attach the volume to the first container. Run a container with the volume mounted, for example:
docker run -d --name writer -v shared-data:/data alpine tail -f /dev/null. This keeps the container running in the background. - Write data from the first container. Use
docker exec writer sh -c "echo 'hello from writer' > /data/message.txt"to create a file inside the shared volume. - Attach the same volume to a second container. Run another container with the same volume:
docker run -d --name reader -v shared-data:/data alpine tail -f /dev/null. - Read the data from the second container. Check the file with
docker exec reader cat /data/message.txt. You should see the message, proving the data is shared. - Use a bind mount for host directories. If you want to share a directory from your host machine, use a bind mount:
docker run -d --name app -v /host/path:/container/path myimage. Both containers can mount the same host path. - Share data between containers directly (without host). Create a volume and mount it into multiple containers as shown above. This is the recommended way for container-to-container sharing.
- Use a shared volume for databases. For database containers (e.g., MySQL, PostgreSQL), mount a volume to the data directory (e.g.,
/var/lib/mysql). Then any other container that mounts the same volume can access the database files, but it’s safer to connect via network to the database service. - Clean up. When done, stop and remove containers:
docker stop writer reader && docker rm writer reader. To remove the volume, usedocker volume rm shared-data(only if no container uses it).
Common Mistakes
- Using anonymous volumes. If you don’t specify a name (e.g.,
-v /data), Docker creates an anonymous volume that is hard to share with other containers. Always name your volumes. - Mixing up source and destination. In
-v host_dir:container_dir, the left side is the host path, the right side is the container path. Reversing them can cause errors or unintended writes. - Assuming containers share data automatically. Containers have isolated filesystems by default; you must explicitly mount the same volume or bind mount.
- Writing to a volume without proper permissions. If the container runs as a non-root user, it may not have write access to the volume. Use
--useror adjust permissions on the host directory. - Forgetting to stop containers before removing volumes. Docker will not delete a volume that is in use. Stop and remove all containers using it first.
Tips
- Use named volumes for data you want to persist. They are easier to back up and migrate than bind mounts.
- Consider using Docker Compose. Define volumes in a
docker-compose.ymlfile to manage multi-container setups with shared volumes more easily. - For read-only sharing, mount as read-only. Add
:roto the mount option, e.g.,-v shared-data:/data:ro, to prevent accidental writes. - Use
docker inspectto verify mounts. Rundocker inspect container_nameand look at theMountssection to confirm the volume is attached correctly. - Back up volumes. Use
docker run --rm -v shared-data:/data -v $(pwd):/backup alpine tar czf /backup/backup.tar.gz -C /data .to backup a volume to your host.
Troubleshooting
- File not visible in the other container. Check that both containers mount the exact same volume name or host path. Use
docker inspectto verify. Also ensure the file was written to the mounted path, not a different location inside the container. - Permission denied when writing to the volume. This often happens with bind mounts on Linux where the host directory has restrictive permissions. Change the host directory permissions (
chmod) or run the container with--user root(temporarily) to test. - Container exits immediately. If you use a command like
tail -f /dev/nullto keep it alive, ensure it’s correct. For other containers, use a long-running process. - Volume not persisting after container restart. Named volumes persist across container restarts, but if you use
docker rmwithout-v, the volume remains. If you used an anonymous volume, it’s deleted when the container is removed. - Data appears in one container but not the other. Verify that the containers are using the same volume, and that the path inside the container is correct. Also check for caching: some applications may cache file listings; try restarting the reader container.
Frequently Asked Questions
Can I share data between containers without a host directory?
Yes, use a named volume. It’s managed by Docker and can be mounted into multiple containers.
What’s the difference between a volume and a bind mount?
A volume is managed by Docker and stored in its own directory. A bind mount maps a specific host directory into the container. Volumes are easier to back up and are recommended for persistent data.
Can I share data between containers on different hosts?
Not with local volumes. You’d need a distributed file system or a network volume plugin (e.g., NFS, CIFS) that both hosts can access.
How do I share data between a container and a Docker service (Swarm)?
Use Docker volumes with a plugin that supports multi-host, or use bind mounts to a shared filesystem. In Docker Swarm, you can use the --mount flag in service definitions.
Is it safe to share a database volume between two database containers?
No, that can corrupt the data. Instead, run one database container and have other containers connect to it over the network.
How do I remove a volume that is in use?
Stop and remove all containers that use it, then run docker volume rm volume_name.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Channel TLD Websites: What They Are & How to Get One5.00 min/km
- Django Class Based Views CRUD REST API
- Ryobi One+ Battery System: Is the Switch Worth It?
- Tmux Terminal Upgrade: Is It Worth It?
- Most Secure OS for Privacy: 3 Picks by Device
- Refurbished Tech Buying Checklist