The challenge lab gives you a running database that holds real rows and was started without a named volume. You must change settings that only a new container can have, and keep every row.
The procedure
text
1. inspect docker inspect … .Mounts where is the data? named, anonymous, or none?
2. stop docker stop clean shutdown, files consistent
3. copy helper container old volume ──► named volume
4. recreate docker rm + docker run same image, same env, new mount and settings
5. verify query through the application not just ls
6. clean docker volume rm / prune only after step 5Commands you will need
| Step | Command |
|---|---|
| Show mounts | docker inspect --format '{{range .Mounts}}{{.Name}} -> {{.Destination}}{{end}}' NAME |
| Show env to reuse | docker inspect --format '{{json .Config.Env}}' NAME |
| Create a volume | docker volume create NAME |
| Copy between volumes | docker run --rm -v OLD:/from -v NEW:/to alpine:3.20 cp -rp /from/. /to/ |
| Check the copy | sudo ls -R /var/lib/docker/volumes/NEW/_data |
| Restart policy | --restart unless-stopped |
| Publish on the host's loopback only | -p 127.0.0.1:5432:5432 |
| Query Postgres | docker exec NAME psql -U postgres -c 'SELECT …' |
Mistakes that lose data
| Mistake | Result |
|---|---|
docker rm -v or docker rm -fv before copying | The anonymous volume is deleted with the container |
docker volume prune while the old container is gone but the copy is unverified | The only good copy is deleted |
docker run without the new -v | Postgres initialises an empty database in a new anonymous volume |
| Copying while Postgres runs | Files can be caught mid-write |