From 40daf21cf7f79370df14b29980d533510e0bea7a Mon Sep 17 00:00:00 2001 From: Patrick Kuijvenhoven Date: Wed, 5 Jun 2024 20:59:10 +0200 Subject: [PATCH] feat(docker): Improve Dockerfile - include build process in Dockerfile - use multistage build process --- Dockerfile | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index ac8a284..e4c279a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,3 +1,26 @@ -FROM scratch +# syntax=docker/dockerfile:1 +# Based on https://docs.docker.com/language/golang/build-images/#multi-stage-builds + +# Build the application from source +FROM golang:1.22 + +WORKDIR /app + +COPY go.mod go.sum ./ +RUN go mod download + +COPY *.go ./ + +ARG CGO_ENABLED=0 +ARG GOARCH=amd64 +ARG GOOS=linux +RUN go build -ldflags '-s -w' -trimpath -o /docker-etchosts + +# Deploy the application binary into a lean image +FROM scratch AS build-release-stage + +WORKDIR / + +COPY --from=0 /docker-etchosts /docker-etchosts + ENTRYPOINT ["/docker-etchosts"] -COPY docker-etchosts /