Docker Foundations·Ship the environment with the app

Health checks and restart policies

Health checks tell Docker whether a container is ready, and restart policies control recovery.

Beginner 12 min read Lesson 4 of 300 Challenge included
What you will be able to do
  • Add HEALTHCHECK to report real readiness.
  • Use depends_on with service_healthy in Compose.
  • Choose a restart policy that matches how you run the service.
  • Read docker ps and docker logs to confirm status.

In plain English

A HEALTHCHECK instruction runs a command inside the container to report whether it is healthy, so Docker and Compose know if the service is actually ready rather than just started. In Compose, depends_on with condition: service_healthy makes one service wait for another to pass its health check before starting. A restart policy such as unless-stopped tells Docker to restart a container automatically when it exits, while no and on-failure give you finer control. docker ps shows the health status of each container in its STATUS column. Logs from docker logs and failed health checks are usually the first place a crash explains itself.

Step by step

  1. 1Add HEALTHCHECK to your Dockerfile with a command that proves the app responds.
  2. 2Rebuild and run it, then watch docker ps until STATUS shows healthy.
  3. 3Run a container with --restart unless-stopped so it recovers after a crash.
  4. 4In Compose, set depends_on with condition: service_healthy on dependents.
  5. 5Read docker logs to see health check failures and crash reasons.
DockerfileDockerfile
FROM node:22-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev
COPY . .
EXPOSE 3000
HEALTHCHECK --interval=30s --timeout=3s --retries=3 CMD wget -qO- http://localhost:3000/health || exit 1
CMD ["node", "server.js"]

Common mistakes

  • Skipping HEALTHCHECK means depends_on can only guess by start order, not actual readiness.
  • Using restart always during development keeps restarting a broken container and hides the error.
  • Writing a health check that does nothing but return success gives false confidence in a dead service.
  • Reading STATUS too soon shows starting, not healthy, because checks run on an interval.
Your turn

Run a container that restarts itself

Easy

Run a web container with --restart unless-stopped so Docker brings it back after it exits.

BASH · starter
docker run -d --name web nginx:alpine
Check your understanding before moving on.
Knowledge checkQuestion 1

What does depends_on with condition: service_healthy do?

Select an answer to continue
Study tools

Make it yours.

Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.

Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign in
Finished reading?

Lock in what you just learned.

Mark this lesson complete to save your place and move one step closer to the certificate.

Next: Debugging containers
Lesson 4 of 300 · 0% of course