Skip to content

How to use Docker well

Multi-stage builds, non-root user, pinned digests, small base images, and a .dockerignore that is not empty.

Docker that ages well

  1. Multi-stage builds

    Build in one stage, copy only the artifact into the final image. Saves hundreds of MB.

  2. Non-root user

    USER appuser in the final stage. Root containers are host-kernel risks.

  3. Pinned base images

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

  4. Small base images

    alpine or distroless for the final stage. Smaller = faster pull, fewer CVEs.

  5. `.dockerignore`

    Exclude node_modules, .git, *.log. A copy that includes .git multiplies image size by 10.

  6. Healthcheck

    HEALTHCHECK CMD curl -f http://localhost/ || exit 1. Kubernetes uses it; CI uses it.