FROM golang:1.24.5-alpine AS builder WORKDIR /app # Install build dependencies RUN apk add --no-cache gcc musl-dev git # Copy go mod and sum files COPY go.mod go.sum ./ # Configure Go proxy with fallback and download dependencies with retry ENV GOPROXY=https://proxy.golang.org,direct ENV GOPRIVATE= ENV GOSUMDB=sum.golang.org # Download all dependencies with retry logic and cache mount RUN --mount=type=cache,target=/go/pkg/mod \ --mount=type=cache,target=/root/.cache/go-build \ for i in 1 2 3 4 5; do \ go mod download && break || \ (echo "Attempt $i failed, retrying in 5s..." && sleep 5); \ done && \ go mod verify # Copy the source code COPY . . # Build the application with cache mount and optimizations RUN --mount=type=cache,target=/go/pkg/mod \ --mount=type=cache,target=/root/.cache/go-build \ CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \ go build -ldflags="-w -s" -trimpath -o main . # Use a smaller image for the final container FROM alpine:latest WORKDIR /app # Copy the binary from builder COPY --from=builder /app/main . # Copy static files and templates COPY --from=builder /app/static ./static COPY --from=builder /app/templates ./templates # Expose port EXPOSE 8080 # Command to run the executable CMD ["./main"]