Images and Dockerfiles
1. What is a multi-stage build, and why use one?
A multi-stage build uses several FROM stages in one Dockerfile and copies only the finished output into the final stage. The compilers, dev dependencies and source files stay behind in the build stage.
FROM golang:1.25 AS build
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 go build -o /app ./cmd/api
FROM gcr.io/distroless/static-debian12
COPY --from=build /app /app
USER nonroot
ENTRYPOINT ["/app"]
The final image holds one binary, so it is smaller, pulls faster and has far fewer packages to patch. BuildKit also builds stages that do not depend on each other in parallel. See the multi-stage build docs.
2. How does the build cache work, and how do you keep it effective?
Each instruction produces a layer, and Docker reuses a cached layer when the instruction and its inputs are unchanged. Once one layer changes, every layer after it rebuilds.
So order instructions from least to most often changed. Copy the dependency manifest, install dependencies, and only then copy the source code. A code change then reuses the expensive install layer.
Cache mounts go further: RUN --mount=type=cache,target=/root/.npm npm ci keeps the package manager's download cache between builds without putting it in the image.
In CI, export the cache to a registry or the CI cache so fresh runners can reuse it. The build cache docs cover both.
3. What is the difference between COPY and ADD?
COPY copies files from the build context; ADD can also fetch URLs and Git repositories and unpack local tar archives. Prefer COPY unless you need one of those extras.
ADD's auto-extraction surprises people, because a .tar.gz gets unpacked when they expected it copied as-is. For remote files, ADD --checksum=sha256:... verifies the download, which a plain curl in a RUN step does not do by default.
4. What is the difference between CMD and ENTRYPOINT?
ENTRYPOINT sets the executable, and CMD sets its default arguments, which docker run image args replaces. Used together, the image behaves like a command with default flags.
ENTRYPOINT ["/usr/local/bin/api"]
CMD ["--port", "8080"]
Use the exec form, a JSON array, for both. The shell form wraps the process in /bin/sh -c, so the shell becomes PID 1 and your app never receives SIGTERM from docker stop.
It then gets killed after the grace period instead of shutting down cleanly.
5. How do you pick a base image?
Pick the smallest image that still runs your app, from a publisher that patches it quickly. The main options:
- Debian slim or Ubuntu: glibc, a package manager, easy to debug. A safe default.
- Alpine: very small, but uses musl libc, which can break or slow some prebuilt binaries and Python wheels.
- Distroless or hardened images: only the runtime, no shell or package manager, so fewer CVEs and less for an attacker to use.
- scratch: empty, for static binaries.
Pin the base by digest in production, and rebuild often so security fixes flow in.
6. How do you make an image smaller?
Remove what the app does not need at runtime, in the same layer that added it. Deleting a file in a later layer does not shrink the image, because the earlier layer still contains it.
RUN apt-get update \
&& apt-get install -y --no-install-recommends ca-certificates \
&& rm -rf /var/lib/apt/lists/*
Also use multi-stage builds, a .dockerignore that excludes .git, node_modules and test data, and a smaller base image. docker history shows which layer holds the weight.
7. How do you use a secret during a build without leaking it into the image?
Use a BuildKit secret mount. The secret is available to one RUN step as a file and is never written to a layer or the image history.
# Dockerfile
RUN --mount=type=secret,id=npmrc,target=/root/.npmrc npm ci
# build command
docker build --secret id=npmrc,src=$HOME/.npmrc .
ARG and ENV are the wrong tools: their values end up in the image metadata, where docker history or docker inspect can read them. See the build secrets docs.
8. How do you build one image for both amd64 and arm64?
Use docker buildx build --platform linux/amd64,linux/arm64, which produces one image index that points to a variant per platform. Each machine pulls the variant that matches it.
Builds for a foreign platform run under QEMU emulation, which is slow for compile-heavy work.
Faster options are native builder nodes for each architecture, or cross-compiling in the build stage with the BUILDPLATFORM and TARGETPLATFORM arguments. Storing multi-platform images locally needs the containerd image store (question 27).
See the multi-platform docs.