Skip to content

How to write one well

Small base, multi-stage, non-root user, one layer per concern, .dockerignore, health check, pinned digests.

A Dockerfile that ages well

  1. Pin the base image

    FROM ubuntu:24.04@sha256:... not ubuntu:latest. Latest moves; digests do not.

  2. Multi-stage always

    Build in one stage, COPY --from=builder only the artifact. Cuts image size by 80-95%.

  3. Non-root user

    USER appuser. Root in the container shares the host kernel capabilities.

  4. Group RUN commands

    RUN apt-get update && apt-get install -y X && rm -rf /var/lib/apt/lists/* = one layer, minimal size.

  5. `.dockerignore`

    Exclude node_modules, .git, *.log. A copy that pulls .git multiplies the image 10×.

  6. HEALTHCHECK

    HEALTHCHECK CMD curl -f http://localhost/ || exit 1. Kubernetes and CI rely on it.