Sub topic 10
Persisting Data with Volumes
This chapter assumes you can build and run an image, publish ports, and read container logs. You should also know that a container is a running instance of an image.
Why container data disappears
A container has a writable layer on top of its image layers. Anything the app writes goes there. When you remove the container with docker rm or --rm, that layer is deleted with it.
That is fine for a stateless web server. It is a problem for a database, uploads, or any file you need to keep. A volume solves this by storing data outside the container's writable layer.
Three ways to mount storage
| Type | Where data lives | Typical use |
|---|---|---|
| Named volume | Managed by Docker on the host | Database files, durable app data |
| Bind mount | A host path you choose | Live source code in development |
| tmpfs mount | Host memory only | Temporary secrets or scratch files |
This chapter focuses on named volumes. Docker creates and manages them, so you don't depend on a particular host directory layout.
Using a named volume
The -v name:path flag mounts a volume at a path inside the container. If the volume doesn't exist, Docker creates it.
docker volume create dbdata
docker run -d --name db \
-e POSTGRES_PASSWORD=secret \
-v dbdata:/var/lib/postgresql/data \
postgres:16Postgres stores its files in /var/lib/postgresql/data. Because that path is backed by dbdata, the files outlive the container.
To see that it works, remove the container and start a new one with the same volume:
docker rm -f db
docker run -d --name db \
-e POSTGRES_PASSWORD=secret \
-v dbdata:/var/lib/postgresql/data \
postgres:16The new container finds the existing database files and starts with your earlier data.
Managing volumes
These commands cover most day-to-day work:
docker volume ls
docker volume inspect dbdata
docker volume rm dbdata
docker volume prunelslists volumes.inspectshows the driver and the mount point on the host.rmdeletes a volume. It fails if a container still uses it.prunedeletes all volumes that no container uses.
Volumes in Compose
In the Compose file from the final chapter, the db service uses the same idea:
services:
db:
image: postgres:16
volumes:
- dbdata:/var/lib/postgresql/data
volumes:
dbdata:The top-level volumes: key declares the volume. The service entry mounts it. Running docker compose down removes containers and networks but keeps the volume. Adding -v (docker compose down -v) also deletes the volumes.
Common errors
- Data vanishes after a restart. You used a path that isn't mounted, or you mounted the wrong path. Check the image's documentation for where it stores data.
- A typo in the volume name creates a new, empty volume. Docker creates volumes silently, so
dbdat:/var/lib/postgresql/datastarts a fresh database. Rundocker volume lsto spot the extra volume. - Old settings seem ignored. Postgres applies
POSTGRES_USERandPOSTGRES_PASSWORDonly when it initializes an empty data directory. With an existing volume, those variables have no effect. - Permission errors with bind mounts. The container user, such as
node, must be able to write to the host directory. Named volumes avoid most of these problems.
Exercise
You run docker compose down -v on a project whose db service mounts the named volume dbdata. Then you run docker compose up. Is the earlier database data still there, and why?
Show answer
No. The -v flag deletes the named volumes declared in the Compose file, so dbdata was removed. docker compose up creates a new empty volume, and Postgres initializes a fresh database. Without -v, the volume would have survived down.
Exercise
You start a Postgres container with -v dbdata:/var/lib/postgresql/data -e POSTGRES_PASSWORD=secret. Later you recreate it with -e POSTGRES_PASSWORD=newpass and the same volume. Which password does the database use?
Show answer
It still uses secret. Postgres reads the password variable only when it initializes an empty data directory, and the volume already holds an initialized database.