Skip to content

Commit

Permalink
docker: use multi-stage build
Browse files Browse the repository at this point in the history
By leveraging docker's multi-stage builds, the size of the final mq2prom
docker image is reduced from 409MB to 21.2MB.

This also adds extra hardening to the image, because a lot less packages
and intermediate artifacts are included as part of it.

As part of this commit, the go version used to build the binary inside
the docker image has been bumped to 1.20.11.

Fixes: #11

Signed-off-by: Juan Vidal Allende <[email protected]>
  • Loading branch information
jvidalallende committed Nov 24, 2023
1 parent d80a72c commit e041cac
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
FROM golang:1.20.10-alpine
# Builder phase, includes golang toolchain
FROM golang:1.20.11-alpine3.18 as builder
COPY . /src
WORKDIR /src
RUN go build -o mq2prom ./cmd

# Runtime phase, contains bare alpine plus the built binary and the config file
# IMPORTANT: keep the alpine version on the builder and the runtime base images aligned
FROM alpine:3.18
RUN mkdir /mq2prom
COPY . /mq2prom
WORKDIR /mq2prom
RUN go build -o mq2prom ./cmd
CMD ./mq2prom
COPY --from=builder /src/mq2prom /src/config.yaml .
CMD ["./mq2prom"]

0 comments on commit e041cac

Please sign in to comment.