0
0
Dockerdevops~5 mins

Health checks in Compose in Docker - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes a service inside a container might start but not be ready to work. Health checks help Docker Compose know if a service is healthy and ready to use by running simple tests inside the container.
When you want to make sure a web server inside a container is fully ready before other services connect to it
When you run a database container and want to check if it is accepting connections
When you have multiple containers and want to restart a container automatically if it becomes unhealthy
When you want to delay starting dependent services until the main service is healthy
When you want to monitor the status of your containers easily with Docker Compose commands
Config File - docker-compose.yml
docker-compose.yml
version: '3.8'
services:
  web:
    image: nginx:1.23
    ports:
      - "8080:80"
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 5s

This Compose file defines a service named web using the nginx image.

The healthcheck section runs a command inside the container to check if the web server is responding.

  • test: The command to run for the health check. Here it tries to fetch the homepage using curl.
  • interval: How often to run the check (every 30 seconds).
  • timeout: How long to wait for the check to finish (10 seconds).
  • retries: How many times to retry before marking unhealthy (3 times).
  • start_period: Time to wait after container start before checking (5 seconds).
Commands
Starts the services defined in the docker-compose.yml file in detached mode so they run in the background.
Terminal
docker-compose up -d
Expected OutputExpected
Creating network "default" with the default driver Creating default_web_1 ... done
-d - Run containers in the background (detached mode)
Shows the status of the running containers including their health status.
Terminal
docker-compose ps
Expected OutputExpected
Name Command State Ports -------------------------------------------------------------------------------- default_web_1 nginx -g 'daemon off;' Up (healthy) 0.0.0.0:8080->80/tcp
Shows detailed health check results for the web container in JSON format.
Terminal
docker inspect --format='{{json .State.Health}}' default_web_1
Expected OutputExpected
{"Status":"healthy","FailingStreak":0,"Log":[{"Start":"2024-06-01T12:00:00Z","End":"2024-06-01T12:00:05Z","ExitCode":0,"Output":""}]}
Stops and removes the containers, networks, and volumes created by docker-compose up.
Terminal
docker-compose down
Expected OutputExpected
Stopping default_web_1 ... done Removing default_web_1 ... done Removing network default
Key Concept

If you remember nothing else from this pattern, remember: health checks let Docker Compose know if a container is ready and healthy by running simple commands inside it.

Common Mistakes
Not specifying a health check command or using a command that always succeeds
Docker will not be able to detect if the service is actually ready or healthy, so it may report unhealthy containers as healthy.
Use a command that truly tests the service readiness, like a curl request to a web server or a database connection test.
Setting too short intervals or timeouts for health checks
The container might be marked unhealthy too quickly during normal startup delays, causing unnecessary restarts.
Set reasonable intervals, timeouts, and start_period to allow the service enough time to start.
Ignoring health status in dependent services
Other services might start before the main service is healthy, causing failures or errors.
Use health check status to control service startup order or retries.
Summary
Define health checks in docker-compose.yml to test if a container service is ready.
Use docker-compose up to start services and docker-compose ps to see health status.
Inspect detailed health results with docker inspect and clean up with docker-compose down.