Originally published on docker.learnio.dev — part of the Docker fundamentals course. View this lesson on the original site.
Add a HEALTHCHECK to nginx
You will run nginx with a Dockerfile you control, then prove Docker reports health status.
GitHub example
Academy repo: 1-install-docker/healthcheck-nginx
Clone that folder when it is ready, or create the files locally as described below.
Try it
- Create a directory with this
Dockerfile:
FROM nginx:stable-alpine
HEALTHCHECK --interval=10s --timeout=3s --retries=3 \
CMD wget -qO- http://127.0.0.1/ || exit 1
- Build and run:
docker build -t nginx-hc:local .
docker run -d --name nginx-hc -p 127.0.0.1:8080:80 nginx-hc:local
- Watch health status become
healthy:
docker inspect --format '{{.State.Health.Status}}' nginx-hc
- Stop and remove when done:
docker rm -f nginx-hc
If it goes wrong
- Stuck on
starting: increase--start-periodor fix the probe URL. wgetnot found: use an image that includes it or install it in the Dockerfile.
Remember
A healthcheck is a small script Docker runs inside the container on a schedule.
From Docker fundamentals — Install Docker, see the value, and learn the basics