Sub topic 10
Persisting data with volumes and bind mounts
This chapter assumes you can start containers with docker run, pass environment variables, and read basic docker ps output. A container's writable layer is deleted when you remove the container. Anything you want to keep must live outside that layer, in a volume or a bind mount.
Why container data disappears
Each container gets a thin writable layer on top of its image. Files written there survive docker stop and docker start. They are lost on docker rm. Two mechanisms store data elsewhere:
- A named volume is storage created and managed by Docker. You refer to it by name and do not need to know where it lives on disk.
- A bind mount maps a specific host directory or file into the container. You choose the host path.
Named volumes
The PostgreSQL image stores its data in /var/lib/postgresql/data. Mount a named volume there:
docker run -d --name db \
-e POSTGRES_PASSWORD=example \
-v pgdata:/var/lib/postgresql/data \
postgres:16The -v value has the form source:destination. Because pgdata is a name and not a path, Docker creates the volume if it does not exist. You can inspect and list volumes:
docker volume ls
docker volume inspect pgdataRemove the container and start a new one with the same -v flag. The new container sees the same database files.
docker rm -f db
docker run -d --name db2 \
-e POSTGRES_PASSWORD=example \
-v pgdata:/var/lib/postgresql/data \
postgres:16Note that POSTGRES_PASSWORD is only used when the data directory is empty. With an existing volume, the original password stays in effect.
Bind mounts
A bind mount uses an absolute host path as the source. It suits source code you edit on the host, or configuration files you want the container to read.
mkdir -p site
echo "<h1>Hello from the host</h1>" > site/index.html
docker run -d --name web -p 8080:80 \
-v "$(pwd)/site":/usr/share/nginx/html:ro \
nginx:1.27The :ro suffix makes the mount read-only inside the container. Edit site/index.html on the host and reload the page. The change appears immediately because both sides see the same files.
The --mount flag is a more explicit alternative to -v:
docker run -d --name web2 -p 8081:80 \
--mount type=bind,source="$(pwd)/site",target=/usr/share/nginx/html,readonly \
nginx:1.27Choosing between them
| Aspect | Named volume | Bind mount |
|---|---|---|
| Source | A name Docker manages | An absolute host path |
| Created automatically | Yes | No. With `--mount`, a missing path is an error |
| Typical use | Databases, application state | Source code, config files |
| Depends on host layout | No | Yes |
Common errors
- A relative source such as
-v data:/appcreates a volume nameddata. It does not mount a directory. Use./dataor$(pwd)/datafor a bind mount. - Mounting over a non-empty directory in the image hides the image's files at that path. A new empty named volume is populated from the image content, but a bind mount is not.
- Permission errors happen when the container user cannot write to the host directory. Check ownership on the host.
docker rmdoes not delete named volumes. Remove them withdocker volume rm pgdata, or usedocker rm -vfor anonymous volumes.
Exercise
You run docker run -d -v appdata:/data alpine sleep 600, then docker rm -f on that container. Does the data in /data survive, and how would you delete it?
Show answer
Yes, the named volume appdata survives container removal. Delete it with docker volume rm appdata once no container uses it.