Files
golang/Dockerfile
0x1d 6ef83e18f8
Some checks failed
CI / Lint (push) Failing after 45s
CI / Test (push) Failing after 10s
CI / Build (push) Has been skipped
CI / Docker Build (push) Has been skipped
feat: initialize golang project template with docker and ci/cd
2026-01-12 10:47:04 +01:00

30 lines
782 B
Docker

# Build stage
FROM golang:1.23-alpine AS builder
WORKDIR /app
# Copy go.mod and go.sum first to leverage Docker cache
# We need to check if go.sum exists, but COPY fails if it doesn't.
# Since we just ran go mod init, go.sum might not exist yet if no deps.
# SAFE PATTERN: COPY go.mod and optional go.sum
COPY go.mod go.sum* ./
RUN go mod download
# Copy the rest of the source code
COPY . .
# Build the application
# -ldflags="-w -s" strips debug information for smaller binary
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-w -s" -o /go/bin/app ./cmd/app
# Final stage
# Use distroless static image for security and minimal footprint
FROM gcr.io/distroless/static-debian12:nonroot
WORKDIR /
COPY --from=builder /go/bin/app /app
USER nonroot:nonroot
ENTRYPOINT ["/app"]