Home › Docker Tutorial › Layers, caching and multi-stage builds
Layers, caching and multi-stage builds
Make images small and builds fast.
Every instruction makes a layer, and Docker reuses unchanged layers, so put things that change rarely (installing dependencies) before things that change often (your source code). A multi-stage build compiles in a big image and copies only the result into a small one. Add a .dockerignore file to keep junk such as node_modules out of the build.
Example code
FROM golang:1.23 AS build
WORKDIR /src
COPY . .
RUN go build -o /app main.go
FROM alpine:3.20
COPY --from=build /app /app
CMD ["/app"]