Docker and Reproducible Environments
Use containers when repeatable packaging is worth it, and stay in control of the image, secrets, health checks, and how the container starts and stops.
Content date: October 15, 2026.
Plain English
An image is a packaged filesystem plus instructions for running it. A container is a running copy of an image, isolated from the rest of the computer. Packaging can reduce differences between your laptop and a server. It does not give you secure settings, lasting storage, backups, or a finished deployment. An image tag such as python:3.13-slim can point to a different image next month. A pinned digest, a fixed fingerprint of one exact image, makes your builds traceable, but then you must update it on purpose and retest.
Use this when
Use this when your project or class benefits from packaging a service so it runs the same everywhere. A local exercise with no dependencies does not need it.
If you are just starting
Run the supplied Python practice program without Docker first. Then read the short Dockerfile and its list of files to copy. If you have no way to run Docker, record that you reviewed the recipe and leave the container run pending, and do not call the plain Python run a container test. Check the license before installing Docker Desktop. As of October 2026, Docker says it is free for personal use, education, and small businesses under 250 employees and $10 million in annual revenue. Nothing in the starter path requires buying software.
When your project grows
Choose a maintained, trusted base image, record its platform and digest, include as few files as possible, and plan who applies updates. Keep credentials out of the copied files and the image layers. When a build needs a secret, use build secret mounts (RUN --mount=type=secret), because ARG and ENV values stay in the final image. Give the running container its secrets through a proper runtime channel. Run as a non-root user, limit resources and mounts, and expose only needed ports. Treat three things as different: the process is alive, the service is ready, and its dependencies are healthy. A Docker HEALTHCHECK reports status, but plain Docker does not restart an unhealthy container on its own. Define how the container stops, where data lives and is backed up, and how you roll back an image. Multi-stage builds help when build tools should not ship in the final image.
Worked example
A bakery's order-status page runs from a tiny recipe that copies only server.py, runs as numeric user 10001, and checks a made-up /health page. The default Python tag is labeled mutable on purpose. The author's computer had no Docker, so only the local web behavior was observed, and the user and healthcheck inside a built image remain unverified.
Practice
Review and verify a minimal container lifecycle
- Download the five practice files, run python -B verify.py, and write down that this tests only a local process.
- Read the Dockerfile and list the files it copies, the mutable base image, the user, and the healthcheck. Write a plan to pin and update the digest.
- If you have Docker, follow the README to build, run, inspect, and stop the container, and observe health, user ID, mounts, and listening address.
- In a throwaway copy, break the healthcheck, watch it fail, then repair and rebuild. If you have no Docker, leave these steps pending.
Deliverable: A recipe review with your local output, plus real image and healthcheck results if you could run Docker, with every unrun check marked pending.
Common mistakes to watch for
- A COPY command pulls a .env file or a folder of personal documents into the image.
- A secret deleted in a later layer still sits in the image history for anyone who pulls the image.
- A running process is mistaken for a healthy business workflow, so orders fail while the container shows green.
- Deleting a container destroys customer uploads that were never stored permanently.
Prompting
A weak prompt
Copy the whole repo including .env into a root container and call it production ready if it starts.
A better prompt
Review this container recipe for what it copies, its base image, user, ports, mounts, healthcheck, secret handling, and how it stops and stores data. Run the program locally as a separate step. If I have Docker, build it and show the image details, health status, user ID, and stop behavior, then break the healthcheck in a throwaway copy and repair it. Mark container checks not run as pending. Never include real credentials, and do not call it production ready.
A container makes your setup repeatable only if you can see exactly what goes into it, and an image that leaks a secret or runs as root repeats that problem everywhere it goes.
Check before you move on
- Copied files and credentials are kept separate.
- The base image and its architecture are recorded.
- The container runs as a non-root user with limited ports.
- You know what the health check does and does not prove.
- Container steps you actually ran are marked apart from the ones you did not.
You are done when
- Your report separates the plain local run, the review of the recipe, and a real container run.
- The container exercise counts as done only after you saw the build, health, user ID, mount, and stop behavior and repaired a broken healthcheck. Otherwise it stays pending.
Questions to think about
You ran server.py on your computer and it worked. Does that prove the Docker image will build?
No. It tests the program, not the copied files, base image, user, healthcheck, or isolation.
You committed an API key in one layer and deleted it in the next. Is it gone from the image?
No. The earlier layer still contains it and anyone with the image can extract it. Keep secrets out of the build and replace a key that leaked.
A container shows "healthy." Will it restart itself if it later turns unhealthy, and does "healthy" mean orders work?
No to both. Plain Docker only reports health, and a health page may not exercise the real workflow.