A running container can still be broken. I find that gap annoying every time, which is why I like a healthcheck: a small command Docker runs on a schedule, so docker ps tells you the truth. Here is how I’d set one up in Compose, how to confirm it behaves, and where to look when it says unhealthy. You need Docker Compose v2 and an image that has some tool inside to test with (curl, wget, a database client). One option, start_interval, needs Docker Engine 25.0 or newer.
- Time
- 7 min
- Steps
- 9
- Updated
- 26 Nov 2026
You will learn
- What a healthcheck actually does
- Add the healthcheck to your service
- Pick sane numbers
- Check the result
- Make dependent services wait
Step 1 of 9: What a healthcheck actually does
Docker runs your command inside the container at a fixed interval. Exit code 0 means healthy, 1 means unhealthy, and 2 is reserved, so don’t use it. A new container starts as starting, turns healthy when a check passes, and becomes unhealthy after a number of consecutive failures. The Compose version works the same way and has the same defaults as the HEALTHCHECK instruction in a Dockerfile, so an image may already ship with one.
The animation shows the whole life cycle. Failures during the start period are ignored, the first pass flips the container to healthy, and it only turns unhealthy after the configured number of failures in a row. One pass in between resets the streak.

Step 2 of 9: Add the healthcheck to your service
This is the shape I’d start from, for a web app that answers on HTTP:
services:
web:
image: myapp:latest
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8080/"]
interval: 30s
timeout: 5s
retries: 3
start_period: 40s
test is either a list or a string. In list form the first item must be NONE, CMD or CMD-SHELL. A plain string counts as CMD-SHELL plus that string, which runs it through the container’s shell. Use CMD-SHELL when you need pipes or ||. The durations take values like 30s or 1m30s.
Step 3 of 9: Pick sane numbers
The defaults from the Dockerfile reference are interval 30s, timeout 30s, start_period 0s and retries 3. My opinion: the 30 second timeout is far too generous for an HTTP check, so I’d lower it to a few seconds. start_period is the one people forget. It gives a slow starter room to boot, and failures during that window don’t count towards the retries. A database or a Java app often needs a minute, so set it above your slowest normal start. Compose also has start_interval for faster checks inside the start period, if your Docker version supports it.
To switch off a check that comes baked into an image, use disable: true (or test: ["NONE"]).

Step 4 of 9: Check the result
Bring it up and look at the STATUS column:
docker compose up -d
docker compose ps
You should see (health: starting) first and (healthy) a little later. If you would rather have the command block until things are ready, docker compose up --wait waits for the services to be running or healthy and implies detached mode. Add --wait-timeout (seconds) so a broken check does not make a script hang forever.
To list only the problem cases across the host, filter on the health state. The values are starting, healthy, unhealthy and none:
docker ps --filter health=unhealthy
A container without any healthcheck shows none, which is easy to mistake for fine.
Step 5 of 9: Make dependent services wait
This is where a healthcheck helps most. By default Compose only waits for a dependency to start, not to be ready. With the long form of depends_on you can ask for more:
services:
db:
image: postgres:17
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres || exit 1"]
interval: 10s
timeout: 5s
retries: 5
start_period: 20s
web:
image: myapp:latest
depends_on:
db:
condition: service_healthy
The other conditions are service_started, which is the default behaviour, and service_completed_successfully, which is meant for one-off jobs such as a migration. Note the || exit 1 on the database check. pg_isready exits with 2 when the server does not answer and 3 when no attempt was made, and Docker reserves 2, so I fold every failure into 1. The credentials in that command are just an example; use your own and keep real passwords out of the file.
Step 6 of 9: Why is it unhealthy?
This is the part I reach for most. Docker keeps the output of the last checks, and you can read it with inspect:
docker inspect --format '{{json .State.Health}}' <container>
The JSON holds the current Status, a FailingStreak counter and a Log list with the start and end time, exit code and output of recent checks. Whatever the command printed to stdout or stderr is stored there, but only the first 4096 bytes, so keep the output short. Run the same command by hand with docker exec to see what the check sees:
docker exec -it <container> curl -f http://localhost:8080/
If you want to watch the flips as they happen, Docker emits a health_status event:
docker events --filter event=health_status
Step 7 of 9: Troubleshooting: the usual suspects
Most unhealthy containers I run into come down to one of these. The tool is missing: curl or wget is not installed in the image, and the Log says “not found”. The port is wrong: the app listens on a different port than the one you test. The address is wrong: localhost resolves to IPv6 while the app only listens on IPv4, so try 127.0.0.1. The path is wrong: curl -f fails on HTTP errors, so a redirect to a login page or a 404 on / counts as unhealthy. Pick a real status endpoint if the app has one. The check is too slow: anything that runs longer than timeout counts as failed and is killed, which matters on a busy machine. And finally the start is slow: the container turns unhealthy before the app is up, so raise start_period.
Quoting is a quiet one too. In the list form every argument is its own item, with no shell, so a pipe or || needs CMD-SHELL or it simply will not do what you think.
Step 8 of 9: Variants for other kinds of services
Not everything speaks HTTP. For a database, use its own readiness tool, as in the Postgres example above. For a minimal image with wget but no curl, a check along the lines of wget -q --spider http://localhost:8080/ often works, but check which options your image’s wget supports. If the image ships its own healthcheck and you don’t like it, override test in your file or turn it off with disable: true. A Dockerfile can only have one HEALTHCHECK; if there are several, the last one wins.
If the image has no tools at all, you can bake one into your own build, for example a tiny binary or script that does the request. I would not install a whole package manager just for a health probe, but that is taste.
Step 9 of 9: My advice
A healthcheck only reports a status. Plain Docker does not restart an unhealthy container for you, so pair it with restart: and your own monitoring if you want action. The restart policies (no, always, on-failure, unless-stopped) react to the process exiting, not to the health state. And test something real. A check that only proves the process exists tells you nothing a running container didn’t already say. I’d start with a short interval and a short timeout, a generous start_period, and a check that touches the thing users touch. Related reading: Docker log rotation and Compose secrets and env files.
Further reading
- Docker Docs: Compose services reference, healthcheck
- Docker Docs: Dockerfile reference, HEALTHCHECK
- Docker Docs: Compose services reference, depends_on
- Docker Docs: docker container ls, filter health
- Docker Docs: docker events
- Docker Docs: docker compose up, –wait
- On this site: Keep passwords out of your Docker Compose file
- On this site: Make Docker Compose wait until the database is really ready, not just started
- On this site: Replace a cron job with a systemd timer, and see when it runs next
