Sub topic 9
Overriding or disabling checks at runtime
This chapter assumes you can build an image with a HEALTHCHECK instruction, know the timing options (interval, timeout, start period, retries), and can read docker ps output. Here you will change a health check without rebuilding the image.
Why override at runtime
The HEALTHCHECK in a Dockerfile is a default. It is baked into the image, and every container started from that image inherits it. Sometimes that default does not fit:
- A test environment needs faster feedback than the 30-second default interval.
- The image's probe uses a tool that is missing in a derived image.
- A third-party image ships a check you do not want, or one that fails in your setup.
- You run a one-off container where health status adds noise.
Docker lets you change the check for a single container with flags on docker run. The image itself stays unchanged.
The --health-* flags
Each part of the Dockerfile instruction has a matching flag:
| Dockerfile part | `docker run` flag |
|---|---|
| `CMD ...` | `--health-cmd` |
| `--interval` | `--health-interval` |
| `--timeout` | `--health-timeout` |
| `--start-period` | `--health-start-period` |
| `--retries` | `--health-retries` |
The example below starts a container from the myapp image from earlier chapters, using a quicker probe:
docker run -d --name myapp-fast \
--health-cmd 'wget -q --spider http://localhost:8000/health || exit 1' \
--health-interval 5s \
--health-timeout 2s \
--health-retries 2 \
myappThis container probes every 5 seconds, allows each probe 2 seconds, and becomes unhealthy after 2 consecutive failures. That is roughly 10 seconds from first failure to unhealthy, compared with about 90 seconds under the image's defaults (3 retries at 30-second intervals).
Partial overrides
You do not have to repeat everything. If you pass only --health-interval 5s, Docker keeps the image's command and its other options and changes just the interval. Pass --health-cmd only when you want a different probe.
Disabling a check
To turn off a check that the image defines, use --no-healthcheck:
docker run -d --name myapp-nocheck --no-healthcheck myappThe container runs normally, but no probes execute. docker ps shows no health information in the STATUS column, and docker inspect has no State.Health section for it.
You can also disable a check inside a Dockerfile with HEALTHCHECK NONE. This matters when your image builds on a base image that already defines a check and you want to remove it for all containers from your image.
Confirming the override took effect
Inspect the configuration Docker stored for the container:
docker inspect --format '{{json .Config.Healthcheck}}' myapp-fastThe output lists the test command and the durations. Docker reports durations in nanoseconds, so 5 seconds appears as 5000000000. If the output is null, the container has no check, which is what you expect for myapp-nocheck.
Where the override sits in the order of precedence
Common mistakes
- Overriding the command with a tool the image lacks. The probe then fails with exit code 127 (command not found) or 126, and the container ends up
unhealthyeven though the app works. Check the probe log withdocker inspect. - Setting
--health-timeoutlonger than--health-interval. Probes can pile up or behave unpredictably, so keep the timeout well below the interval. - Forgetting that the override applies only to that container. A new
docker runwithout the flags goes back to the image default.
Exercise
You start a container with docker run -d --health-interval 5s myapp, where the myapp image already defines a HEALTHCHECK with a command, a 30s interval and 3 retries. Which command and retry count does the container use, and what is the interval?
Show answer
The container uses the image's probe command and 3 retries, because only the interval was overridden. The interval is 5 seconds. Unset values are inherited from the image.
Exercise
Write the docker run command that starts myapp as a container named quiet with no health check at all, and name one way to confirm that no check is configured.
Show answer
docker run -d --name quiet --no-healthcheck myapp. To confirm, run docker inspect --format '{{json .Config.Healthcheck}}' quiet, which should not show a probe command. You can also check that docker ps shows no health status for it.