-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile
More file actions
71 lines (52 loc) · 1.93 KB
/
Dockerfile
File metadata and controls
71 lines (52 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# Multi-stage build for reproducible binaries
# The build environment is versioned to ensure consistent builds across all environments
# Stage 1: Builder
FROM golang:1.24-alpine AS builder
# Install git for go mod (if needed), ca-certificates, and file for verification
RUN apk add --no-cache git ca-certificates tzdata file
# Set working directory
WORKDIR /build
# Copy go.mod and go.sum first for better layer caching
COPY go.mod go.sum ./
# Download dependencies
RUN go mod download
RUN go mod verify
# Copy source code
COPY . .
# Run tests to ensure everything works before building
RUN go test -v ./...
# Build static binary with version info
# CGO_ENABLED=0 ensures static linking (no dynamic dependencies)
# -ldflags injects version information at build time
# -trimpath removes local path information for reproducibility
# -buildvcs=false disables VCS stamping (not available in shallow clones)
ARG VERSION=dev
ARG BUILD_TIME=unknown
ARG GIT_COMMIT=unknown
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build \
-buildvcs=false \
-ldflags="-w -s -X main.Version=${VERSION} -X main.BuildTime=${BUILD_TIME} -X main.GitCommit=${GIT_COMMIT}" \
-trimpath \
-o /build/recal \
./cmd/recal
# Verify the binary is statically linked
RUN file /build/recal | grep "statically linked"
# Stage 2: Distroless runtime
FROM gcr.io/distroless/static-debian12:nonroot
# Copy CA certificates from builder
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
# Copy timezone data
COPY --from=builder /usr/share/zoneinfo /usr/share/zoneinfo
# Copy the static binary
# NOTE: config.yaml is NOT copied - it should be mounted at runtime
COPY --from=builder /build/recal /app/recal
# Set working directory
WORKDIR /app
# Expose port
EXPOSE 8080
# Run as non-root user (distroless nonroot user is UID 65532)
USER nonroot:nonroot
# Set config file location
ENV CONFIG_FILE=/app/config.yaml
# Run the application
ENTRYPOINT ["/app/recal"]