Docker image for Go app

Create a multi-stage Dockerfile to build your Go app in a builder stage and run it in a minimal Alpine image.

Use a multi-stage Dockerfile to compile your Go app in a builder stage and copy the binary to a minimal runtime stage. This reduces image size and attack surface.

# Stage 1: Build
FROM golang:1.22-alpine AS builder
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main .

# Stage 2: Runtime
FROM alpine:latest
RUN apk --no-cache add ca-certificates
WORKDIR /root/
COPY --from=builder /app/main .
CMD ["./main"]