CD2: Running many containers together

Lesson 2 of 5 · reading 15–25 minutes; practice and project time is on the module page

Volumes: keeping data when containers go

🎯You'll be able to

Store a database's data on a volume so it survives the container being replaced, and choose between named volumes and bind mounts.

🏠The everyday picture

A shop can be repainted or the counter replaced, but the stock in the storeroom stays where it is. The storeroom isn't part of the counter.

📘What it really is

📝A worked example

Input: in the finished kit, book seats with curl -X POST localhost:8080/api/bookings -H 'content-type: application/json' -d '{"route_id":5,"seats":3}', then replace the database container with terraform apply -replace=docker_container.db.

Expected output: the booking returns 201 with seats_left (37 on a fresh environment). After the replace, /api/routes still shows 37 seats left on route 5, because the data is on the volume routerani-dev-db. In the starter, which has no volume, the same steps bring back 40 seats: the booking is lost. That's Project 1 test 9.

🌍Where it's used

📝Case scenario

A small travel agency ran its booking database in a container with no volume. A routine Docker update restarted all containers from their images, and three weeks of bookings vanished. They had nightly backups of the server's home folder, which didn't include Docker's internal storage. Afterwards they used a named volume plus a nightly pg_dump copied to cloud storage, and restored it once a month to check.

🖱️Try it

Run docker volume ls, then docker volume inspect routerani-dev-db in the finished kit. Then run docker exec routerani-dev-db pg_dump -U routerani routerani | head -20. Success: you've found where the data lives and made the start of a backup you could copy elsewhere.

⚠️Common mistakes

🛠️For your project

Project 1 test 9 books seats, replaces the database container, and checks the booking is still there.

✅Quick check

Why isn't a volume a backup?

Show the answer

It's on the same host and disk; if they're lost, it's lost too.

Which folder needs the volume in the official PostgreSQL image?

Show the answer

/var/lib/postgresql/data.

← PreviousNetworks and names
ProLearners Assist