Sub topic 10
Sharing data between containers
This chapter assumes you can run containers with docker run, you know what a named volume is, and you can read -v mount syntax. Here you will see two ways to give several containers access to the same data, and the problems that come with it.
Why share a volume
A volume lives outside any one container. Any number of containers can mount it at once, so one container can produce files and others can read them. Common cases are a web server serving files a build job generated, a log shipper reading another service's logs, and a backup container reading a database's files.
Method 1: mount the same named volume
The simplest approach is to name the same volume in each docker run. Docker creates the volume the first time it is used.
docker run -d --name writer -v shared:/data alpine sh -c 'echo shared > /data/msg && sleep 3600'
docker run --rm -v shared:/data:ro alpine cat /data/msgThe second command prints shared. The :ro suffix makes the mount read-only inside the reader. A read-only consumer cannot damage the data, so use it whenever a container only needs to read.
Method 2: --volumes-from
The --volumes-from flag copies the mounts of an existing container into a new one. You don't need to know the volume names.
docker run --rm --volumes-from writer alpine cat /data/msgThe new container sees /data at the same path as in writer. To make the inherited mounts read-only, append :ro, as in --volumes-from writer:ro.
| Aspect | Same named volume | `--volumes-from` |
|---|---|---|
| You must know the volume name | Yes | No, only the container name |
| Mount path | You choose it | Copied from the source container |
| Works with Compose | Yes, natively | Possible, but a shared named volume is clearer |
| Source container must exist | No | Yes, running or stopped |
Prefer named volumes in most cases. They show the data dependency in the command or Compose file, and they don't tie one container to another's name.
Common errors and gotchas
- Concurrent writes. Docker does not coordinate access. If two containers write to the same file, such as a database file, you can corrupt it. Let one container own writes and mount the volume read-only elsewhere.
- Different users. Each container may run as a different user ID. A file written by one may not be readable or writable by another. Check ownership with
ls -lninside the container. Chapter 12 covers fixes. - Stale views through caching. A process that reads a file once at startup will not notice later changes. The volume is shared, but the application must reread the file.
- The source container is removed. With
--volumes-from, removing the source container does not delete named volumes. It does delete anonymous volumes unless you removed it without-vand something else still references them. Use named volumes to avoid surprises.
Cleaning up
Remove the containers first. Docker refuses to remove a volume that a container still references, even a stopped one.
docker rm -f writer
docker volume rm sharedExercise
You start a writer container with -v shared:/data. Write the command for a second container that can read /data but cannot modify it, using the volume name. Then name one risk of letting both containers write.
Show answer
docker run --rm -v shared:/data:ro alpine ls /data. If both containers write to the same file, their changes can overwrite each other and corrupt the data, because Docker provides no locking.