A tip from my notebooks.
Typically you want to have small Docker images, to save cost on both storage space in the container registry and the production machines. One trick to get a smaller image than usual is to copy your artifacts using a multi-stage build. The first stage installs the needed tools to build, and the second stage only contains the results.
I've found that apt-get update
can otherwise really bloat the image by itself. Even with
commands like apt-get clean
and rm -rf /var/lib/apt/lists/*
, the image was still
bloated.
Here's an example of using the multi-stage technique, from one of my projects. base
builds and tests the project, and prod
copies just the artifacts from that image.
FROM ubuntu:24.10 as base
WORKDIR /app/
RUN apt-get update && apt-get install -y \
golang ca-certificates python3 python3-yaml
COPY . /app/
RUN go generate ./src/... && go build ./src/main && go test ./...
#-----------------------------------------------------------------------------------------
FROM ubuntu:24.10 as prod
WORKDIR /app/
COPY --from=base /app/main /app/LICENSE.txt /app/
CMD ["./main"]