Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 15.9 — Shipping Python: Docker and Deployment
Python · Example 1 of 2

Structured JSON logs to stdout

A log is the running diary a service writes about what it does. One JSON object per line is easy for log tools to read. The DEBUG line is hidden because the level is INFO (Topic 7.9 teaches logging).

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.

01

Write a .dockerignore list that keeps .env, .git, virtual environments and caches out of the build.

02

Write a function ready(settings) that returns (503, {...}) unless settings has a non-empty api_key.

03

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

01

Why copy pyproject.toml and the lock file before the source code in a Dockerfile?

02

Why run as a non-root user and keep secrets out of the image?

03

What is a health check and what should it check?

04

What does it mean for a service to be stateless, and why does that matter for containers?

Structured JSON logs to stdout
Sign in to run this example in your browser.

Expected output

{"level": "INFO", "logger": "service", "message": "service starting", "port": 8000}
{"level": "WARNING", "logger": "service", "message": "slow request", "ms": 840, "path": "/todos"}