More than “it builds”
Part 4.3 turns the working app into something that runs the same way in another environment. Claude Code can draft the Dockerfile and .dockerignore, but the human inspection is the important part. The goal is not only that the image builds; it is a smaller, safer runtime image that runs the actual app without secrets.
Key ideaA Dockerfile can build successfully and still be poor engineering.
What the Dockerfile should do
Six requirements, each with what to check in the generated file.
It builds
- One stage carrying build tools
- Floating python:latest
- Runs as root
- .env copied in with everything else
- --reload in the CMD
Closer to shippable
- Builder stage + slim runtime stage
- Explicit python:3.11-slim
- USER app before CMD; whoami → app
- .dockerignore keeps .env and .git out
- uvicorn on 0.0.0.0:8000, no reload
| Requirement | What to check |
|---|---|
| Multi-stage build | Use a builder stage for dependency preparation and a runtime stage that copies only what is needed to run. |
| Explicit slim Python base | Use an explicit slim runtime base such as python:3.11-slim. Avoid floating latest tags. |
| Non-root user | Create an app user, set ownership, and make sure USER app appears before CMD. |
| No baked secrets | Do not copy .env, credentials, tokens, or private files into the image. |
| Production-ish command | Run uvicorn app.main:app with --host 0.0.0.0 --port 8000 and no reload flag. |
| Health check | Verify the running container responds at /health. |
.dockerignore is part of the security story
Docker sends the build context to the builder, and a broad COPY can carry it into the image. The .dockerignore decides what never gets there. The notes say it should exclude anything that does not belong in the image.
- .env and any other secrets files.
- .git.
- venv or .venv.
- Tests, when they are not needed at runtime.
- Caches and build artifacts.
- Local machine files.
Verify it locally
These are the notes’ local verification commands. Build, run detached on port 8000, call /health, check which user the process runs as, then stop the container. The security check is that whoami returns app, not root. Record the image size too, and investigate if it is unexpectedly large, but size alone does not prove the image is correct.
Commands from the notesdocker build -t task-tracker:dev . docker run --rm -d -p 8000:8000 --name tt-dev task-tracker:dev curl -i http://localhost:8000/health docker exec tt-dev whoami docker stop tt-dev
Read with careThe notes and prompt D1 assume the app already exposes /health. If yours does not, that is a code change to plan and review separately; it is not part of writing the Dockerfile.
The three-line security log
Prompt D2 asks Claude for the exact verification commands and a log template, but you run the commands yourself and copy the real output into the log. Letting Claude judge Docker quality without runtime evidence is the mistake it avoids.
| Three-line Docker security log | Example entry |
|---|---|
| Non-root user | docker exec tt-dev whoami -> app |
| Slim runtime base | Runtime stage uses an explicit slim Python base. |
| No baked secrets | .dockerignore excludes .env, .git, virtual environments, caches, and build artifacts. |
Key takeaways
- Ask for multi-stage, explicit slim base, non-root user, no secrets and no --reload.
- .dockerignore keeps .env, .git, virtual environments and caches out of the image.
- Prove it with /health and whoami returning app, not with a successful build.
- Image size is worth recording, but it is not proof of correctness.