CD1: Containers done well

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

Health checks and clean shutdowns

🎯You'll be able to

Give an image a health check, tell "alive" from "ready", and make sure the app receives the stop signal and finishes its work before exiting.

🏠The everyday picture

A shop opens its shutters only once the cash counter is set up, even though the staff arrived earlier. At closing time, it stops letting new customers in, serves the people already inside, and then locks up. It doesn't just switch the lights off mid-sale.

📘What it really is

📝A worked example

Input: start your image with the database from CD2, send traffic with node tools/load.js http://localhost:8080/api/routes 20 10, and run docker stop on one copy while traffic flows.

Expected output: the copy logs SIGTERM received: finishing requests in progress, /ready returns 503 during the shutdown, and the load tool reports 0 failed requests, because nginx sends new requests to the other copy. With the starter's CMD ["npm", "start"] and no health check, the same test can fail some requests. That's what Project 1 test 5 and Project 2 test 6 measure.

🌍Where it's used

📝Case scenario

A railway enquiry API had a health check on / that returned 200 as soon as the web server started, before it had loaded its timetable cache. After each deploy, new copies got traffic during the 20 seconds they took to load, and passengers saw "no trains found". Pointing the check at a readiness path that waited for the cache fixed it.

🖱️Try it

Start one container from your image, then run docker inspect --format '{{json .State.Health}}' <name>. Stop the database container and inspect again after 10 seconds. Success: the status moves from healthy to unhealthy, and the log shows the failing check.

⚠️Common mistakes

🛠️For your project

Project 1 test 3 checks for a HEALTHCHECK, and test 5 checks that every API copy reports healthy.

✅Quick check

What's the difference between liveness and readiness?

Show the answer

Liveness says the process works; readiness says it can serve requests now, including reaching what it depends on.

What does docker stop do?

Show the answer

Sends SIGTERM, waits (10 seconds by default), then sends SIGKILL.

← PreviousRunning containers safely
ProLearners Assist