Docker Compose - share a named volume between multiple containers
Codemia Knowledge Hub ·
Declare the volume once under the top-level volumes key, then mount that same named volume into every service that needs the data. A named volume is managed by Docker separately from any single container, so several containers can mount it at the same time.
One writer, one reader
A minimal example with a writer container and a reader container:
Both services mount shared-data. The writer mounts it read-write, and the reader mounts it read-only, a sensible safeguard when a container only consumes files.
Start it with:
The reader sees the file created by writer, because both look at the same volume contents. If you give each service its own volume name, nothing is shared: the names must match.
Named volume or bind mount
A bind mount points to a specific host path, while Docker manages a named volume. Named volumes are useful when you want:
- data persistence without caring about the exact host directory
- a portable Compose file that behaves similarly across machines
- easier lifecycle management through Docker commands
Bind mounts are often better for live source-code editing during development. Named volumes usually suit application data such as uploaded files, database contents or generated artifacts.
Mounting the volume at different paths
The mount point does not have to be the same in each container. The volume name is what matters:
The same files appear at /app/data in one container and at /backup in the other.
Using a volume created outside Compose
If the volume already exists outside the Compose project, mark it as external:
Compose then neither creates nor deletes the volume; it attaches to the existing Docker-managed volume named shared-data.
What a shared volume does not handle
A shared volume gives containers the same filesystem view, but no coordination. If several containers write to the same files, you still need application-level rules for:
- file locking
- atomic writes
- ownership and permissions
- startup ordering
If one container expects a file immediately but another only creates it after a long initialization, you need health checks, retries or explicit orchestration.
Permissions matter in particular: if one container writes files as one user and another runs as a different user, the second may be unable to read or modify them.
Stale data from earlier runs
Named volumes persist after containers stop, so files from earlier test runs may still be there. Use docker volume ls and docker volume rm carefully when you want a clean slate.
Related reading
- Docker Compose wait for container X before starting Y
- Docker container live migration in kubernetes
- Docker desktop - kubernetes failed to start
- Docker for Desktop runs the Kubernetes - Ip address is not working
- docker compose build single container
- Docker Compose Change env variables
- Docker for Mac - Kubernetes - reference local image
- Docker for Windows stuck at Kubernetes is Starting after updating to version 2.1.1.0 Edge or Stable
System design interview problems on this topic
- Design a Container Orchestration System: solution and walkthrough
- Design a Code Deployment System: solution and walkthrough
- Design a Task Scheduler: solution and walkthrough
- Design a GPU Cluster Scheduler for ML Training: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.