Health checks and restarts in Docker Compose
Use two Nginx services to test Compose startup gates, HTTP 503 health failures, PID 1 exits, and when restart or up -d applies configuration changes.
When its health endpoint returned HTTP 503, the Nginx web container kept running with an
unhealthystatus. When its main process exited, Docker Engine restarted the same container. A Compose startup condition and an Engine restart policy account for those two outcomes.
Set up an Nginx web server and proxy
The lab ran on Docker Engine 29.9.3 (linux/arm64) and Docker Compose 5.7.1, with Nginx 1.30.8 in both containers. You’ll need Docker, Compose, curl, and an available local port 8080. The example uses the nginx:1.30-alpine series tag. It can resolve to a different patch release when you pull it.
Save these six files in an empty directory. Compose connects web and proxy to the same default network. The proxy publishes port 8080 on the host’s loopback address. It forwards requests to web:80 using the Compose service name. The web server has no published host port. Compose networking
compose.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
name: hello-compose
services:
web:
build: .
restart: unless-stopped
environment:
# Only used to show when a config change reaches the container.
WEB_REVISION: ${WEB_REVISION:-1}
healthcheck:
test: ["CMD", "wget", "-q", "-T", "2", "-O", "/dev/null", "http://127.0.0.1/healthz"]
interval: 3s
timeout: 3s
retries: 3
start_period: 10s
proxy:
image: nginx:1.30-alpine
volumes:
- ./proxy.conf:/etc/nginx/conf.d/default.conf:ro
ports:
- "127.0.0.1:8080:80"
depends_on:
web:
condition: service_healthy
restart: true
Dockerfile
1
2
3
4
5
6
FROM nginx:1.30-alpine
COPY web.conf /etc/nginx/conf.d/default.conf
COPY health-marker.sh /docker-entrypoint.d/40-health-marker.sh
RUN chmod 0755 /docker-entrypoint.d/40-health-marker.sh
COPY index.html /usr/share/nginx/html/index.html
index.html
1
2
3
4
5
<!doctype html>
<html>
<head><meta charset="utf-8"><title>Hello</title></head>
<body>Hello from my Docker image</body>
</html>
web.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
server {
listen 80;
root /usr/share/nginx/html;
location / {
index index.html;
}
# 200 while /var/lib/web-health/ready exists, 503 otherwise.
location = /healthz {
access_log off;
default_type text/plain;
root /var/lib/web-health;
try_files /ready =503;
}
}
proxy.conf
1
2
3
4
5
6
7
8
9
10
11
12
server {
listen 80;
location / {
# "web" is resolved once when nginx loads this config. If the web
# container is recreated with a new IP, this proxy keeps the old
# address until it is restarted or reloaded.
proxy_pass http://web:80;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
health-marker.sh
1
2
3
4
5
6
7
8
9
10
11
#!/bin/sh
# Runs on every container start through the nginx image entrypoint
# (/docker-entrypoint.d). Creates the readiness marker that /healthz checks.
# WEB_START_UNREADY=1 starts without it (used only by the lab override).
set -eu
mkdir -p /var/lib/web-health
if [ "${WEB_START_UNREADY:-0}" = "1" ]; then
rm -f /var/lib/web-health/ready
else
echo ok > /var/lib/web-health/ready
fi
The / route serves the HTML page. The /healthz route returns 200 while the readiness file exists and 503 when it’s missing. The image entrypoint runs health-marker.sh at every container start. The health check runs wget inside web and uses its exit status, so it checks Nginx directly.
The configured interval is 3 seconds. Docker counts a check that exceeds the 3-second timeout as a failure and marks the container unhealthy after three consecutive failures. The 10-second start period gives the service time to initialize, and failures during that period don’t count toward the retries. Once a check passes during that period, later failures count toward the limit. Dockerfile HEALTHCHECK
Compare when each startup condition opens the proxy
Save the following files in overrides/. The first starts web with no readiness file and extends the start period to 120 seconds. The second switches the proxy’s dependency condition to service_started.
overrides/unready.yaml
1
2
3
4
5
6
7
8
# Start web without the readiness marker. The long start_period keeps the
# health status at "starting" (not "unhealthy") until the marker is created.
services:
web:
environment:
WEB_START_UNREADY: "1"
healthcheck:
start_period: 120s
overrides/started-only.yaml
1
2
3
4
5
6
7
# Baseline: only wait for web to be started, not healthy.
services:
proxy:
depends_on:
web:
condition: service_started
restart: true
All three lab projects use port 8080, so run them one at a time. If a project name below is already in use, pick a fresh name for the lab and use it consistently. Start with service_started and request both endpoints.
1
2
3
4
5
6
7
docker compose -p hello-started -f compose.yaml -f overrides/unready.yaml \
-f overrides/started-only.yaml up -d --build
docker compose -p hello-started ps -a
curl -sS -o /dev/null -w '%{http_code}\n' http://127.0.0.1:8080/
curl -sS -o /dev/null -w '%{http_code}\n' http://127.0.0.1:8080/healthz
docker compose -p hello-started -f compose.yaml -f overrides/unready.yaml \
-f overrides/started-only.yaml down
The proxy was running while the web container’s health was still starting. Through the proxy, / returned 200 and /healthz returned 503.
Start a fresh project with service_healthy and leave up waiting in the first terminal.
1
docker compose -p hello-healthy -f compose.yaml -f overrides/unready.yaml up -d --build
In a second terminal in the same directory, check the status of both services. Once web is running, create the readiness file before the start period expires.
1
2
3
docker compose -p hello-healthy -f compose.yaml -f overrides/unready.yaml ps -a
docker compose -p hello-healthy -f compose.yaml -f overrides/unready.yaml \
exec web sh -c 'echo ok > /var/lib/web-health/ready'
For ten status checks over roughly ten seconds, web stayed running/starting and proxy stayed created. After the file was added, the probe passed, the proxy started, and up returned exit code 0. Clean up this project in the first terminal before starting the next one.
1
docker compose -p hello-healthy -f compose.yaml -f overrides/unready.yaml down
With service_started, Compose can start the dependent service as soon as its dependency is running. With service_healthy, it waits for the dependency to pass its health check. The same startup gate can hold a migration until its database is ready. Compose startup order · Flyway startup order example
Fail the health check while Nginx keeps serving
Start the default setup in a new project. Use config to see the resolved settings, ps to check the containers, and logs to look for the request to the proxy.
1
2
3
4
5
6
7
lab() { docker compose -p hello-restart "$@"; }
WEB_REVISION=1 lab config
WEB_REVISION=1 lab up -d --build
lab ps
curl -sS http://127.0.0.1:8080/
lab logs --tail=5 proxy
The request through the proxy returned HTTP 200 with Hello from my Docker image in the HTML. Before you remove the readiness file, record the web container’s ID, process status, health, restart count, and start time. Record the proxy’s start time so you can tell whether it restarted.
1
2
3
4
5
6
7
8
9
10
11
12
web_state() {
docker inspect --format '{{.Id}} {{.State.Status}} {{with .State.Health}}{{.Status}}{{else}}-{{end}} {{.RestartCount}} {{.State.StartedAt}}' "$(lab ps -aq web)"
}
proxy_started() {
docker inspect --format '{{.State.StartedAt}}' "$(lab ps -q proxy)"
}
web_state
proxy_started
lab exec web rm -f /var/lib/web-health/ready
curl -sS -o /dev/null -w '%{http_code}\n' http://127.0.0.1:8080/
curl -sS -o /dev/null -w '%{http_code}\n' http://127.0.0.1:8080/healthz
web_state prints the container ID, state, health status, restart count, and start time, in that order. Run it every few seconds until the health status changes to unhealthy. In the lab, it showed running unhealthy 0 after the failures added up. The container ID and restart count stayed the same, and so did the proxy’s start time. The page still returned 200 while the health endpoint returned 503.
The probe failed while Nginx’s main process kept handling requests. The Engine restart policy responds when the container exits. In this setup, an unhealthy dependency also leaves the already running proxy in place. Restore the readiness file and wait for web_state to show healthy before continuing. Engine restart policies
1
lab exec web sh -c 'echo ok > /var/lib/web-health/ready'
Compare a PID 1 exit with a Compose restart
Use lab ps to check that web has been running for over ten seconds. A restart policy only takes effect after the container has been up for at least 10 seconds and Docker has started monitoring it. Then ask the Nginx master process, PID 1 in this container, to exit gracefully. Restart policy details
1
2
3
web_state
proxy_started
lab exec web nginx -s quit
Once web is healthy again, run web_state and proxy_started. Under restart: unless-stopped, Engine restarted the same web container and the restart count rose from 0 to 1. The proxy’s start time stayed the same.
The service’s restart setting controls when Engine restarts the container automatically. Docker documents the options below. The lab used unless-stopped throughout. Engine restart policies
| Setting | Behavior |
|---|---|
restart: "no" | The default. Docker doesn’t restart the container automatically. |
on-failure[:max-retries] | Restarts after a nonzero exit code. You can cap the number of restart attempts. |
always | Restarts after an exit. If you stop the container manually, it can start again when the Docker daemon restarts. |
unless-stopped | Restarts after an exit. If you stop the container manually, it stays stopped when the Docker daemon restarts. |
Now pass a different revision to restart and then to up -d. Read the actual environment variable inside the web container after each command.
1
2
3
4
5
6
7
8
9
WEB_REVISION=2 lab restart web
web_state
proxy_started
lab exec web printenv WEB_REVISION
WEB_REVISION=2 lab up -d
web_state
proxy_started
lab exec web printenv WEB_REVISION
| Operation | web ID | WEB_REVISION | Proxy start time |
|---|---|---|---|
WEB_REVISION=2 lab restart web | Stayed 3e1e63f1015e | 1 | Advanced |
WEB_REVISION=2 lab up -d | Changed to 7cb8043d2153 | 2 | Advanced |
With depends_on.web.restart: true, Compose also restarts proxy when it explicitly restarts or updates web. Its restart command keeps the existing container’s configuration. The up -d command can recreate a container to apply changed service configuration. Compose startup order · restart · up
In this run, the recreated web container kept the same IP and a request through the proxy returned 200. Compose keeps the service name stable, but a replacement container can have a different address. This Nginx configuration resolves web when it loads the configuration. If the address changes, restart or reload the proxy so it resolves the name again. Replacing containers on a Compose network
Stop the service manually and clean up
1
2
3
4
5
lab stop web
sleep 12
lab ps -a
web_state
lab down
In the lab, web stayed exited and its restart count held steady for all 12 seconds. A manual stop and a PID 1 exit produced different outcomes under the same policy. The down command removes the lab project’s containers and default network. The image you built stays by default. Engine restart policies · Compose down
Compose names each built image <project>-<service>. To remove the lab images too, run the command below, adjusting the names if you changed the project names. The official nginx:1.30-alpine image stays.
1
docker image rm hello-started-web hello-healthy-web hello-restart-web