skip to content
Alvin Lucillo

Docker multi-stage builds

/ 1 min read

💻 Tech

In Docker, multi-stage builds are a way to create smaller images, especially if the images will be used in production. The idea is to perform the build steps (e.g., downloading the packages the required by a Go project and compiling the project itself) and the output of the build step will then be copied to the new image, which is the production image.

# Build stage
FROM golang:latest AS build

WORKDIR /app

COPY go.mod go.sum ./

RUN go mod download

COPY . .

RUN go build -o /app/main .

# Production stage
FROM alpine:3.14

COPY --from=build /app/main /app/main

CMD ["/app/main"]