Sub topic 6
Persisting data with volumes and bind mounts
This chapter assumes you can read a compose.yaml, know what a container and an image are, and have the docker compose plugin installed. You should also know that a container's writable layer is deleted when the container is removed.
Why containers need volumes
A container's filesystem lasts only as long as the container. If you run docker compose down and then up again, a database in that container starts empty. To keep data, you mount storage that lives outside the container.
Compose supports two main kinds of mount.
| Kind | Source | Managed by | Typical use |
|---|---|---|---|
| Named volume | A name such as `db-data` | Docker | Database files, uploaded data |
| Bind mount | A host path such as `./src` | You | Source code in development, config files |
Named volumes
A named volume is storage that Docker creates and manages. You declare it under the top-level volumes: key, then mount it into a service by name.
name: shop
services:
db:
image: postgres:16
environment:
POSTGRES_USER: app
POSTGRES_PASSWORD: ${DB_PASSWORD}
POSTGRES_DB: app
volumes:
- db-data:/var/lib/postgresql/data
volumes:
db-data:The short form db-data:/var/lib/postgresql/data means source:target. The target is a path inside the container. Postgres stores its files in /var/lib/postgresql/data, so that is where the volume goes.
Compose prefixes the volume name with the project name. With name: shop, the real volume is called shop_db-data. You can see it with docker volume ls.
Bind mounts
A bind mount maps a host file or directory into the container. A source that starts with ./, ../ or / is treated as a path, not as a volume name.
services:
web:
build: .
volumes:
- ./src:/app/src
- ./nginx.conf:/etc/nginx/nginx.conf:roEdits on the host show up in the container immediately, which makes bind mounts useful for development. The :ro suffix makes the mount read-only inside the container. Use it for configuration the container should never change.
The long syntax
The long form is more explicit and avoids ambiguity between a name and a path.
services:
web:
image: myapp:latest
volumes:
- type: bind
source: ./src
target: /app/src
read_only: true
- type: volume
source: uploads
target: /app/uploads
volumes:
uploads:How data survives the stack
docker compose down removes containers and networks but keeps named volumes. docker compose down -v also deletes the named volumes declared in the file, and the data in them is gone.
Common errors
- Database ignores new settings. Postgres only reads
POSTGRES_USERandPOSTGRES_PASSWORDwhen the data directory is empty. If the volume already holds data, changing these variables has no effect. Remove the volume to start fresh. - Empty directory in the container. A bind mount hides whatever the image placed at the target path. Mounting
./srcover/app/srcreplaces the image's copy with your host folder, which is empty if the host path is wrong. - A missing host path. With the short syntax, Docker creates a missing source path as a directory. A typo in a file path can therefore leave you with a directory where you expected a file. The long syntax with
type: bindfails instead, unless you setcreate_host_path: true. - Permission denied. Bind-mounted files keep the host's owner and permissions. If the container user has a different user ID, writes can fail. Check the ownership of the host directory.
Exercise
You run docker compose down, then docker compose up, and your Postgres data is still there. Later you run docker compose down -v and the data is gone. Why does the first sequence keep the data and the second remove it?
Show answer
The data lives in a named volume, which is separate from the container. down removes containers and networks but leaves named volumes, so the new container mounts the same volume. down -v also deletes the named volumes declared in the Compose file, which destroys the data.
Exercise
A service mounts ./config:/etc/app and the container sees an empty /etc/app, although the image contains default files there. Give two causes to check.
Show answer
The bind mount replaces the image's contents at the target path, so the image's defaults are hidden. The host directory ./config is also probably empty or the path is wrong, relative to the project directory.