Interview Prep/Real-World Interview/Write-a-Dockerfile tasks

Add a HEALTHCHECK to a Dockerfile for a web app without installing curl.

IntermediateHands-onWrite-a-Dockerfile tasks
Answer
FROM python:3.12-slim
# ...
HEALTHCHECK --interval=30s --timeout=3s --start-period=20s --retries=3 \
  CMD python -c "import urllib.request,sys; sys.exit(0 if urllib.request.urlopen('http://127.0.0.1:8000/health', timeout=2).status == 200 else 1)"

Key trick → Use the runtime the image already has (Python, Node, or the app binary's own healthcheck subcommand) instead of adding curl. Note that Kubernetes ignores HEALTHCHECK; it uses probes.

All Real-World Interview questions Roadmap: Interview readiness

More on Write-a-Dockerfile tasks