11# Pin specific version for stability
22# Use separate stage for building image
33# Use debian for easier build utilities
4- FROM golang:1.19-bullseye AS base-builder
5-
6- # Add non root user
7- RUN useradd -u 1001 nonroot
4+ FROM golang:1.19-bullseye AS build-base
85
96WORKDIR /app
107
@@ -16,8 +13,7 @@ RUN --mount=type=cache,target=/go/pkg/mod \
1613 --mount=type=cache,target=/root/.cache/go-build \
1714 go mod download
1815
19- # Dev stage with additional dev dependencies installed
20- FROM base-builder AS dev
16+ FROM build-base AS dev
2117
2218# Install air for hot reload & delve for debugging
2319RUN go install github.com/cosmtrek/air@latest && \
@@ -27,8 +23,10 @@ COPY . .
2723
2824CMD ["air", "-c", ".air.toml"]
2925
30- # Production builder stage to produce the static binaries
31- FROM base-builder AS production-builder
26+ FROM build-base AS build-production
27+
28+ # Add non root user
29+ RUN useradd -u 1001 nonroot
3230
3331COPY . .
3432
@@ -47,21 +45,21 @@ RUN go build \
4745 -o api-golang
4846
4947# Use separate stage for deployable image
50- FROM scratch AS production
48+ FROM scratch
5149
5250# Set gin mode
5351ENV GIN_MODE=release
5452
5553WORKDIR /
5654
5755# Copy the passwd file
58- COPY --from=production-builder /etc/passwd /etc/passwd
56+ COPY --from=build-production /etc/passwd /etc/passwd
5957
6058# Copy the healthcheck binary from the build stage
61- COPY --from=production-builder /app/healthcheck/healthcheck healthcheck
59+ COPY --from=build-production /app/healthcheck/healthcheck healthcheck
6260
6361# Copy the app binary from the build stage
64- COPY --from=production-builder /app/api-golang api-golang
62+ COPY --from=build-production /app/api-golang api-golang
6563
6664# Use nonroot user
6765USER nonroot
0 commit comments