Docker solves "works on my machine" by bundling your app together with its exact environment — Python version, system libraries, dependencies — into an image. That image runs identically on your laptop, in CI, and in production.
The Dockerfile
A Dockerfile is the recipe to build the image: pick a base, install dependencies, copy your code, set the run command:
FROM python:3.12-slim # small official Python baseWORKDIR /appCOPY requirements.txt . # copy deps first…RUN pip install --no-cache-dir -r requirements.txt # …so this layer cachesCOPY . . # then the codeCMD ["python", "main.py"] # what to run# build: docker build -t myapp .# run: docker run myapp
Layers & caching
Key takeaways
- 01Docker packages app + its exact environment into a portable image.
- 02A Dockerfile is the recipe: base image, dependencies, code, run command.
- 03The same image runs identically in dev, CI and production.
- 04Order instructions so dependency layers cache — deps before code.
- 05Use slim base images and a .dockerignore to stay lean.
