Docker, start to ship · Chapter 12 of 26
Ship with a healthcheck
By the end of this chapter you can write a healthcheck and labels into an image, run the check, and read what it found. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Images and Dockerfiles
- 4 tasks
- Docker
- In the all labs plan
The first 2 chapters of Docker, start to ship are free. This one comes with the all labs plan.
Why you need this
Last Saturday the orders API was "running" for two hours while it answered nothing: the node process was alive, the app inside it was stuck. Ravi found out from a customer. Then he asked which version was on the server, and nobody could say who had built it.
Everything you know so far watches the process. docker ps says Up for as long as the container's main process is alive, working or not, and docker logs shows only what the app chose to print. A tag such as 1.0 is a name anyone can move to another image. Ravi wants the container to say whether the app really answers, and the image to say which version it is and whom to ask.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
Build chai-12-api:1.0 with a healthcheck on /health
Give the orders API a healthcheck, so Docker can ask if the app is working.
1. Write app/Dockerfile. Start from node:24-alpine, work in /app, copy server.js in, and run node server.js.
2. Add a HEALTHCHECK. Its command asks the app's /health route from inside the container, at http://localhost:3000/health.
3. Build it with the app folder as the context, as chai-12-api:1.0.
To pass: chai-12-api:1.0 has a HEALTHCHECK, and its command asks /health.
- Task 2
Label it with its version and its author
Write the version and the author into the image as labels.
1. In app/Dockerfile, add the label org.opencontainers.image.version with the value 1.0. A label called version also counts.
2. Add the label org.opencontainers.image.authors with a name in it. A label called maintainer also counts.
3. Build chai-12-api:1.0 again, with the same name and tag.
To pass: chai-12-api:1.0 has a version label of exactly 1.0, and an authors (or maintainer) label that is not empty.
- Task 3
Run chai-12-api on port 8012 and run its healthcheck
Run the API and run its healthcheck once yourself.
1. Start a container named chai-12-api from the chai-12-api:1.0 you built last, in the background.
2. Publish the container's port 3000 on the lab's port 8012.
3. Run curl on localhost:8012/health. It answers ok.
4. Run docker healthcheck run chai-12-api once.
To pass: chai-12-api is running from the newest chai-12-api:1.0, localhost:8012/health answers ok, and its healthcheck passes. You must type docker healthcheck run chai-12-api in the chapter folder. If you build the image again later, delete the container and start it again.
- Task 4 · interview question
Interview: what Docker does when unhealthy
Interview question: when a container's healthcheck says unhealthy, does Docker restart the container by itself?
1. Answer the question clearly in your own words.
2. Say what Docker does with the result of the healthcheck.
3. Save your answer in answer.txt, in the chapter folder.
To pass: answer.txt gives a clear answer to the question, and says what happens to the container's health status.
What you will learn
- HEALTHCHECK: a command written into the image that asks the app, from inside the container, whether it works.
- docker healthcheck run: runs that check once, which this lab needs.
- Health states: starting, healthy and unhealthy, and what moves a container from one to the next.
- LABEL: facts such as the version and the author, kept in the image.
More in Images and Dockerfiles
Build "Ship with a healthcheck" yourself
Tapri’s menu site works on one laptop and nowhere else. You ship the machine. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Docker, start to ship are free. This one comes with the all labs plan.