Build a custom image that installs your app’s dependencies on a base image.
a Dockerfile is a kitchen recipe: FROM is base ingredients, RUN is cooking, COPY is adding ingredients, CMD is serving the dish.
FROM python:3.12-slim # base layer
WORKDIR /app
COPY requirements.txt ./ # first (cacheable)
RUN pip install -r ... # second
COPY . . # third (changes most often)
CMD ["python", "app.py"] # default command
A Dockerfile is a text file Docker reads top-to-bottom. Each instruction creates a layer; layer caching reuses unchanged layers - order matters for speed. Use small base images (alpine, python:3.12-slim) for size and security. Use multi-stage builds to keep build tools out of the final image. Pin versions (no latest). Prefer exec form CMD ["x", "y"] over shell form CMD x y (which wraps in /bin/sh -c).
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["python", "app.py"]
canonical Python Dockerfile. Two COPY lines let the deps layer cache when only code changes.
FROM node:20-alpine
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci --omit=dev
COPY . .
EXPOSE 3000
CMD ["node", "server.js"]
Node.js Dockerfile; npm ci is faster and stricter than npm install for reproducible builds.
FROM golang:1.22 AS build
WORKDIR /src
COPY . .
RUN CGO_ENABLED=0 go build -o /out/app
FROM gcr.io/distroless/static
COPY --from=build /out/app /app
CMD ["/app"]
multi-stage: build a Go binary in stage 1, copy binary to distroless static base - final image ~20MB with no shell.
Using a fat base like ubuntu:latest (hundreds of MB); putting COPY . . early (invalidates cache on every file change); forgetting to pin versions.
Small base; pin versions; separate dependency-copy from source-copy; multi-stage for compiled languages; prefer exec form CMD; treat your Dockerfile as code.
docker images size column; build logs.python:3.12-slim or alpine; multi-stage for compiled parts.dive or size-limit.RUN pip install runs every time.COPY . . BEFORE pip install; source change invalidated deps layer.