From 7038b3e4aed4a11df36151b19f3c71fa06261716 Mon Sep 17 00:00:00 2001 From: Ruidy Date: Sun, 27 Apr 2025 14:20:08 +0200 Subject: [PATCH] feat(docker): optimize Dockerfile for multi-stage builds - Introduced a builder stage to separate build dependencies. - Added a dev stage for runtime dependencies and live reload tools. - Reduced image size by cleaning up unnecessary files in the builder stage. - Improved build efficiency by copying only necessary files to the dev stage. --- Dockerfile.dev | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/Dockerfile.dev b/Dockerfile.dev index 8a1adb1..7653a23 100644 --- a/Dockerfile.dev +++ b/Dockerfile.dev @@ -1,18 +1,34 @@ -FROM golang:1.24-alpine - +# ----------- Builder Stage ----------- +FROM golang:1.24-alpine AS builder WORKDIR /app -RUN apk update \ - && apk add --no-cache build-base \ - && apk add --no-cache golangci-lint \ - && go install github.com/air-verse/air@latest \ - && go install github.com/a-h/templ/cmd/templ@latest +RUN apk update && apk add --no-cache build-base COPY go.mod go.sum ./ RUN go mod download COPY . . +RUN go build -ldflags="-s -w" -o rentease main.go + +# Install air and templ for live reload and templating in dev +RUN go install github.com/air-verse/air@latest \ + && go install github.com/a-h/templ/cmd/templ@latest \ + && rm -rf /go/pkg/mod /root/.cache/go-build + +# ----------- Dev Stage ----------- +FROM golang:1.24-alpine AS dev +WORKDIR /app + +# Install runtime dependencies +RUN apk add --no-cache ca-certificates && apk add --no-cache golangci-lint + +# Copy only necessary files from builder +COPY --from=builder /app/rentease ./rentease +COPY --from=builder /app/assets ./assets +COPY --from=builder /go/bin/air /go/bin/templ /go/bin/ +COPY . . + EXPOSE 8000 -CMD [ "air", "-c", ".air.toml" ] +CMD ["air", "-c", ".air.toml"]