How to Set Up Health Checks for Docker Containers on a VPS

A container can be running and still be broken. The web server inside it may have stopped answering, the app may be stuck in a loop, or the database connection may have died, while Docker happily reports the container as up. Docker container health checks close that gap: they probe the app inside the container and tell Docker, and you, when it stops working properly.

Without a health check, Docker only knows whether the main process is alive. A process can be alive and useless at the same time. A health check runs a small command inside the container on a schedule, and marks the container healthy or unhealthy based on the result. That status then drives restarts, load balancer routing, and alerts, so a stuck container gets fixed instead of serving errors.

Docker Container Health Checks: How They Work

A health check is a command Docker runs inside the container at a set interval. The command exits with code zero when the app is fine, and any other code when it is not. Docker retries a few times before changing the status, so a single slow response does not mark a healthy container as broken. The three numbers that matter are the interval between checks, the timeout for each check, and the number of retries before Docker calls the container unhealthy.

The check should test what users need, not just whether a process exists. A web app should answer an HTTP request on its real port. A database should answer a real query. A worker should prove it can reach its queue. A check that only confirms the process is running adds nothing, because Docker already knows that. A check that hits the app the way a user would is the one that catches real failures.

Docker tracks the result in the container status. You can see it with docker ps, which shows healthy, unhealthy, or starting next to the container name. That status is available to everything else on the host: Compose can wait for a healthy database before starting the app, a reverse proxy can stop sending traffic to an unhealthy container, and a monitoring tool can alert on it.

Adding a HEALTHCHECK to Your Dockerfile

The simplest place to define a health check is the Dockerfile, with the HEALTHCHECK instruction. It lives with the image, so every container started from that image gets the same check. A web app check looks like this:

HEALTHCHECK --interval=30s --timeout=5s --retries=3 
  CMD curl -f http://localhost:8080/health || exit 1

That block is the docker healthcheck dockerfile pattern in its simplest form. It runs curl against the app health endpoint every 30 seconds, waits up to 5 seconds for an answer, and marks the container unhealthy after three failures in a row. The endpoint should be a real route in the app that checks its own dependencies, not a static page. An endpoint that returns 200 while the database is down will hide the exact failure you need to see.

If the image has no curl or wget, install one in the Dockerfile, or write the check in the language the app already uses. A small script that opens a socket to the app port works too. What matters is that the check runs inside the container and tests the app, not the host. If you are new to building images, the Docker Compose first file guide covers the project layout these checks live in.

Docker Compose Healthcheck Settings

Compose lets you define the same check in docker-compose.yml, which is handy when you cannot rebuild the image or when the check differs between environments. The syntax mirrors the Dockerfile:

services:
  web:
    image: myapp:latest
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8080/health"]
      interval: 30s
      timeout: 5s
      retries: 3
      start_period: 40s

The start_period is the setting most people miss. It gives the container a grace window after starting, during which failed checks do not count. A Java app or a big WordPress install can take a minute to warm up, and without a start period Docker would mark it unhealthy before it ever had a chance to answer. Set the start period longer than your slowest cold start, then let the normal interval take over.

Compose also lets services depend on health, not just on startup. A web container can wait until the database reports healthy before it starts, instead of starting at the same time and crashing on a connection error. That one line removes the most common Compose startup failure:

services:
  web:
    depends_on:
      db:
        condition: service_healthy

When you need to rebuild or move the stack, the guide to moving Docker containers to a new VPS covers how to carry the Compose file and its volumes to the new host without losing the health check setup.

What to Check for Each Kind of Container

Web apps should answer an HTTP request and prove the database is reachable. A health endpoint that runs a one-row query catches both a dead app and a dead database in a single call. Keep the endpoint light: one row, no heavy joins, no cache warming. A slow health endpoint will time out and mark a fine container as unhealthy.

Databases should answer a real query through their own client. MySQL can use mysqladmin ping, PostgreSQL can use pg_isready, and Redis can use redis-cli ping. Those tools are built for exactly this check, and they test the server the way a client would. A check that only looks for the database process misses a server that accepts connections but refuses queries.

Workers and queue consumers are harder, because they have no port to probe. Check the queue instead: a script that confirms the worker processed a job recently, or that the queue length is not growing without bound. A worker that is alive but not consuming jobs looks healthy to Docker and dead to your users, so the check must measure work, not existence. If the container writes lots of logs while you debug this, the Docker log management guide shows how to keep log growth from filling the disk.

Container Health Monitoring and Alerts

A health check only helps if someone sees the result. Docker does not restart an unhealthy container on its own, and it does not send you a message. You need something watching the status. The simplest watcher is a small script on a cron schedule that lists unhealthy containers and sends a message, or a restart policy that recreates the container when it fails.

For real alerting, pair the checks with a monitoring tool. Uptime Kuma can probe the public URL of each service and alert when it stops answering, which catches the same failures from outside the host. The Uptime Kuma install guide for Docker shows how to run it in a container next to the services it watches. Together, the internal health check and the external probe cover both sides: Docker knows the container is sick, and you get told about it.

Do not alert on every single failed check. The retries exist to absorb brief slowness, and an alert on the first failure will wake you for a hiccup that fixed itself. Alert on the status change to unhealthy, and include the container name and the last few log lines in the message, so you can judge the failure without logging in first.

Common Health Check Mistakes

The most common mistake is a check that is too strict. A timeout of two seconds on an app that normally answers in three will flap between healthy and unhealthy all day, and every flap is noise that hides real failures. Measure the normal response time first, then set the timeout comfortably above it.

The second mistake is checking the wrong thing. A check that curls the host from outside the container tests the network, not the app. A check that reads a static file tests the disk, not the app. Always test the path a user takes, inside the container, against the real port.

A Setup You Can Copy

Start with one service, the one whose failure hurts most. Give it a health endpoint, a Dockerfile or Compose check with a 30 second interval, a 5 second timeout, three retries, and a start period that covers the cold start. Watch docker ps for a day and confirm the status stays healthy under normal load. Then add the same pattern to the database and the next service, and wire the unhealthy status into your alerts.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.