A health check that tests real dependencies
The function returns the status code and body a /health endpoint would send. Close the connection and watch it turn to 503.
To ship a Python service, you package the code and its exact dependencies into a container image, run it as a non-root user with its settings from environment variables, give it a health check, and let a platform start, restart and scale it.
Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.
Ready for more? This phase has 6 hands-on exercises with automatic checks.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Write a .dockerignore list that keeps .env, .git, virtual environments and caches out of the build.
Write a function ready(settings) that returns (503, {...}) unless settings has a non-empty api_key.
Write the docker run command that passes the key from your shell environment variable LLM_API_KEY and maps port 8000.
Explain it without notes
Why copy pyproject.toml and the lock file before the source code in a Dockerfile?
Why run as a non-root user and keep secrets out of the image?
What is a health check and what should it check?
What does it mean for a service to be stateless, and why does that matter for containers?
Expected output
(200, {'status': 'ok', 'checks': {'database': True, 'config': True}})
(503, {'status': 'unavailable', 'checks': {'database': True, 'config': False}})
(503, {'status': 'unavailable', 'checks': {'database': False, 'config': True}})