Docker
Docker Compose
Named Volumes
Container Orchestration
Multi-container Applications

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:

yaml
1services:
2  writer:
3    image: alpine:3.20
4    command: sh -c "echo hello from writer > /shared/message.txt && sleep 3600"
5    volumes:
6      - shared-data:/shared
7
8  reader:
9    image: alpine:3.20
10    command: sh -c "sleep 2 && ls -l /shared && cat /shared/message.txt && sleep 3600"
11    volumes:
12      - shared-data:/shared:ro
13
14volumes:
15  shared-data:

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:

bash
docker compose up

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:

yaml
1services:
2  app:
3    image: alpine:3.20
4    command: sh -c "echo data > /app/data/out.txt && sleep 3600"
5    volumes:
6      - shared-data:/app/data
7
8  backup:
9    image: alpine:3.20
10    command: sh -c "ls -l /backup && sleep 3600"
11    volumes:
12      - shared-data:/backup:ro
13
14volumes:
15  shared-data:

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:

yaml
1services:
2  app:
3    image: alpine:3.20
4    volumes:
5      - shared-data:/shared
6
7volumes:
8  shared-data:
9    external: true

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.


Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design