From e041cacee75a8489e083db742cdb33abfcd3ce20 Mon Sep 17 00:00:00 2001 From: Juan Vidal Allende Date: Fri, 24 Nov 2023 09:33:36 +0100 Subject: [PATCH] docker: use multi-stage build 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 --- Dockerfile | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index 18f1f16..efcdb74 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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"]