← Knowledge Base
DockerReference note1 min read · updated 2026-11-27

Build an image: a small Dockerfile and multi-stage builds

A Dockerfile example, layer caching, a .dockerignore and a multi-stage build for smaller images.

dockerdockerfilebuild
FROM python:3.12-slim AS base
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
USER 10001
CMD ["python", "app.py"]
docker build -t myapp:1.0 .
docker run --rm -p 8000:8000 myapp:1.0

Order instructions from least to most frequently changing so that the layer cache works (dependencies before source code). Add a .dockerignore (.git, node_modules, *.log, secrets) to keep the build context small and clean. A multi-stage build compiles in one stage (FROM golang AS build) and copies only the result into a minimal final stage (COPY --from=build /out/app /app). Run as a non-root USER. Never bake secrets into an image.