0
0
Dockerdevops~5 mins

Alert setup for container health in Docker - Commands & Configuration

Choose your learning style9 modes available
Introduction
Containers can stop working or become unhealthy without you noticing. Setting up alerts helps you know immediately when a container has a problem so you can fix it quickly.
When you want to know if your web server container crashes or stops responding.
When you run a database container and need to be alerted if it stops working.
When you deploy multiple containers and want to monitor their health automatically.
When you want to avoid downtime by fixing container issues before users notice.
When you want to track container restarts to find unstable services.
Config File - docker-compose.yml
docker-compose.yml
version: '3.8'
services:
  my-app:
    image: nginx:1.23
    ports:
      - "8080:80"
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 5s

This file defines a service named my-app using the nginx image.

The healthcheck section tells Docker how to check if the container is working properly by trying to access its web page.

If the health check fails 3 times in a row, Docker marks the container as unhealthy.

Commands
This command starts the container defined in the docker-compose.yml file in detached mode, so it runs in the background.
Terminal
docker-compose up -d
Expected OutputExpected
Creating network "default" with the default driver Creating my-app_1 ... done
-d - Run containers in the background (detached mode)
This command lists running containers so you can check if your container is up and see its status.
Terminal
docker ps
Expected OutputExpected
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES abc123def456 nginx:1.23 "/docker-entrypoint.…" 10 seconds ago Up 8 seconds (healthy) 0.0.0.0:8080->80/tcp my-app_1
This command shows detailed health status of the container named my-app_1 in JSON format so you can see if it is healthy or not.
Terminal
docker inspect --format='{{json .State.Health}}' my-app_1
Expected OutputExpected
{"Status":"healthy","FailingStreak":0,"Log":[{"Start":"2024-06-01T12:00:00Z","End":"2024-06-01T12:00:05Z","ExitCode":0,"Output":""}]}
--format - Format the output using a Go template for easier reading
This command shows recent health status events for the container to monitor changes and detect unhealthy states.
Terminal
docker events --filter 'container=my-app_1' --filter 'event=health_status' --since '10m'
Expected OutputExpected
2024-06-01T12:05:00.000000000Z container my-app_1 health_status: healthy 2024-06-01T12:10:00.000000000Z container my-app_1 health_status: unhealthy
--filter - Filter events by container and event type
--since - Show events only from the last 10 minutes
Key Concept

If you remember nothing else from this pattern, remember: Docker health checks let you automatically detect container problems and trigger alerts or actions.

Common Mistakes
Not defining a healthcheck in the Docker or docker-compose file.
Without a healthcheck, Docker cannot know if the container is working properly or not.
Always add a healthcheck section with a test command that verifies the container's main function.
Using a healthcheck command that always succeeds or never finishes.
This causes false healthy or stuck states, hiding real problems.
Use a simple, fast command that returns failure if the service is down, like a curl request to a local endpoint.
Ignoring the health status and not monitoring it regularly.
You won't know if the container becomes unhealthy and cannot fix issues quickly.
Use commands like docker inspect or docker events to watch health status and integrate alerts in your monitoring system.
Summary
Define a healthcheck in your docker-compose.yml to let Docker test container health automatically.
Start your container with docker-compose up -d and check its status with docker ps.
Use docker inspect and docker events to monitor health status and detect problems early.