|
1 | | -ARG GO_VERSION=1 |
2 | | -FROM golang:${GO_VERSION}-bookworm as builder |
3 | | -# Update package lists and install ca-certificates |
| 1 | +ARG GO_VERSION=1.24 |
| 2 | +FROM golang:${GO_VERSION}-bookworm AS builder |
4 | 3 |
|
5 | 4 | WORKDIR /usr/src/app |
6 | | -RUN go env -w GOMODCACHE=/root/.cache/go-build |
| 5 | + |
| 6 | +# Copy dependency files first for better layer caching |
7 | 7 | COPY go.mod go.sum ./ |
8 | | -RUN --mount=type=cache,target=/root/.cache/go-build go mod download && go mod verify |
| 8 | + |
| 9 | +# Download dependencies with proper cache mounts |
| 10 | +# Separate cache directories for modules and build cache |
| 11 | +RUN --mount=type=cache,target=/go/pkg/mod \ |
| 12 | + --mount=type=cache,target=/root/.cache/go-build \ |
| 13 | + go mod download && go mod verify |
| 14 | + |
| 15 | +# Copy source code |
9 | 16 | COPY . . |
10 | | -RUN --mount=type=cache,target=/root/.cache/go-build go build -v -o /brease . |
11 | 17 |
|
12 | | -FROM debian:bookworm |
13 | | -# Update package lists and install ca-certificates and Infisical CLI |
14 | | -RUN apt-get clean && \ |
15 | | - rm -rf /var/lib/apt/lists/* && \ |
16 | | - apt-get update --fix-missing && apt-get install -y \ |
| 18 | +# Build with cache mounts and optimizations |
| 19 | +RUN --mount=type=cache,target=/go/pkg/mod \ |
| 20 | + --mount=type=cache,target=/root/.cache/go-build \ |
| 21 | + CGO_ENABLED=0 GOOS=linux go build -v -ldflags="-w -s" -o /brease . |
| 22 | + |
| 23 | +# Runtime stage - using distroless for minimal size and better security |
| 24 | +FROM gcr.io/distroless/static-debian12:nonroot AS runtime-distroless |
| 25 | + |
| 26 | +COPY --from=builder /brease /usr/local/bin/brease |
| 27 | + |
| 28 | +# Distroless doesn't support shell scripts, so we need a different approach |
| 29 | +# If start.sh is essential, use the debian variant below instead |
| 30 | +CMD ["/usr/local/bin/brease"] |
| 31 | + |
| 32 | +# Alternative runtime with Infisical support (use this if you need start.sh) |
| 33 | +FROM debian:bookworm-slim AS runtime-debian |
| 34 | + |
| 35 | +# Install dependencies in a single layer with cleanup |
| 36 | +RUN apt-get update && apt-get install -y --no-install-recommends \ |
17 | 37 | ca-certificates \ |
18 | 38 | curl \ |
19 | 39 | bash \ |
20 | 40 | && curl -1sLf 'https://artifacts-cli.infisical.com/setup.deb.sh' | bash \ |
21 | | - && apt-get update && apt-get install -y infisical \ |
| 41 | + && apt-get update && apt-get install -y --no-install-recommends infisical \ |
| 42 | + && apt-get clean \ |
22 | 43 | && rm -rf /var/lib/apt/lists/* |
23 | 44 |
|
24 | | -COPY --from=builder /brease /usr/local/bin/ |
| 45 | +# Create non-root user |
| 46 | +RUN useradd -r -u 65532 -g root nonroot |
| 47 | + |
| 48 | +COPY --from=builder /brease /usr/local/bin/brease |
25 | 49 | COPY start.sh /usr/local/bin/start.sh |
26 | | -RUN chmod +x /usr/local/bin/start.sh |
| 50 | +RUN chmod +x /usr/local/bin/start.sh /usr/local/bin/brease |
| 51 | + |
| 52 | +USER nonroot |
27 | 53 |
|
28 | 54 | CMD ["/usr/local/bin/start.sh"] |
| 55 | + |
| 56 | +# Final stage selector - uncomment the one you want to use |
| 57 | +FROM runtime-debian AS final |
0 commit comments