33# Comments are provided throughout this file to help you get started.
44# If you need more help, visit the Dockerfile reference guide at
55# https://docs.docker.com/go/dockerfile-reference/
6-
76# Want to help us make this template better? Share your feedback here: https://forms.gle/ybq9Krt8jtBL3iCk7
87
98# ###############################################################################
10- # Create a stage for building the backend application.
9+ # Build Arguments
10+ # ###############################################################################
1111ARG GO_VERSION=1.23.4
12+
13+ # ###############################################################################
14+ # Build Stage - Backend Application
15+ # ###############################################################################
1216FROM --platform=$BUILDPLATFORM golang:${GO_VERSION} AS backend-build
17+
18+ # Set working directory
1319WORKDIR /src
1420
15- # Download dependencies as a separate step to take advantage of Docker's caching.
21+ # Configure Git and Go environment for insecure connections
22+ RUN git config --global http.sslVerify false
23+
24+ ENV GOINSECURE=* \
25+ GOPROXY=direct \
26+ GOSUMDB=off
27+
28+ # Download Go dependencies (cached layer)
29+ # This step is separated to take advantage of Docker's layer caching
1630RUN --mount=type=cache,target=/go/pkg/mod/ \
1731 --mount=type=bind,source=go.sum,target=go.sum \
1832 --mount=type=bind,source=go.mod,target=go.mod \
1933 go mod download -x
2034
35+ # Build arguments for cross-compilation
2136ARG TARGETARCH
2237
38+ # Build the application binary
2339RUN --mount=type=cache,target=/go/pkg/mod/ \
2440 --mount=type=bind,target=. \
2541 CGO_ENABLED=0 GOARCH=$TARGETARCH go build -o /bin/server .
2642
2743# ###############################################################################
28- # Create a new stage for running the backend
44+ # Runtime Stage - Production Image
45+ # ###############################################################################
2946FROM alpine:latest AS backend
3047
48+ # Install necessary packages and certificates
3149RUN --mount=type=cache,target=/var/cache/apk \
32- apk --update add \
50+ apk --no-check-certificate -- update add \
3351 ca-certificates \
34- tzdata \
35- && \
36- update-ca-certificates
52+ tzdata && \
53+ update-ca-certificates
3754
55+ # Create non-root user for security
3856ARG UID=10001
3957RUN adduser \
4058 --disabled-password \
@@ -44,10 +62,15 @@ RUN adduser \
4462 --no-create-home \
4563 --uid "${UID}" \
4664 appuser
65+
66+ # Switch to non-root user
4767USER appuser
4868
69+ # Copy the built binary from build stage
4970COPY --from=backend-build /bin/server /bin/
5071
72+ # Expose application port
5173EXPOSE 8080
5274
53- ENTRYPOINT [ "/bin/server" ]
75+ # Set the entrypoint
76+ ENTRYPOINT ["/bin/server" ]
0 commit comments