pegasus/Dockerfile

57 lines
1.6 KiB
Docker
Raw Normal View History

2025-09-15 12:09:02 -05:00
# syntax=docker/dockerfile:1.7
############################
# Frontend build (Vite/React)
############################
2025-09-16 07:37:10 -05:00
# Run toolchains on the build machine, not target arch
FROM --platform=$BUILDPLATFORM node:20-alpine AS fe
2025-09-08 00:48:47 -05:00
WORKDIR /src/frontend
2025-09-15 12:09:02 -05:00
COPY frontend/package*.json ./
2025-09-16 07:37:10 -05:00
RUN --mount=type=cache,target=/root/.npm npm ci
2025-09-15 12:09:02 -05:00
COPY frontend/ .
RUN npm run build
# expose artifacts in a neutral location
RUN mkdir -p /out && cp -r dist/* /out/
2025-09-08 00:48:47 -05:00
2025-09-15 12:09:02 -05:00
############################
# Backend build (Go)
############################
2025-09-16 07:37:10 -05:00
FROM --platform=$BUILDPLATFORM golang:1.22-alpine AS be
2025-09-15 12:09:02 -05:00
RUN apk add --no-cache ca-certificates upx git
2025-09-16 07:37:10 -05:00
ARG TARGETOS
ARG TARGETARCH
ENV CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} \
2025-09-15 12:09:02 -05:00
GOPROXY=https://proxy.golang.org,direct \
GOPRIVATE=scm.bstein.dev
2025-09-08 00:48:47 -05:00
WORKDIR /src/backend
2025-09-15 12:09:02 -05:00
2025-09-16 07:37:10 -05:00
# 1) Cache modules
2025-09-15 12:09:02 -05:00
COPY backend/go.mod backend/go.sum ./
RUN --mount=type=cache,target=/go/pkg/mod go mod download
2025-09-16 07:37:10 -05:00
# 2) Source
2025-09-15 12:09:02 -05:00
COPY backend/ .
2025-09-16 07:37:10 -05:00
# 3) FE assets where the embed expects them (//go:embed web/dist/**)
2025-09-15 12:09:02 -05:00
COPY --from=fe /out ./web/dist
2025-09-16 07:37:10 -05:00
# 4) Tidy (in case new deps appeared)
2025-09-15 12:09:02 -05:00
RUN --mount=type=cache,target=/go/pkg/mod go mod tidy
2025-09-16 07:37:10 -05:00
# 5) Build the binary; fail if build fails; allow UPX to fail only.
2025-09-15 12:09:02 -05:00
RUN --mount=type=cache,target=/go/pkg/mod \
2025-09-16 07:37:10 -05:00
set -eux; \
mkdir -p /out; \
go build -trimpath -ldflags="-s -w" -o /out/pegasus ./main.go; \
test -f /out/pegasus; \
upx -q --lzma /out/pegasus || true
2025-09-15 12:09:02 -05:00
############################
# Final, minimal image
############################
FROM gcr.io/distroless/static:nonroot AS final
2025-09-16 07:37:10 -05:00
COPY --from=be /out/pegasus /pegasus
2025-09-08 00:48:47 -05:00
USER nonroot:nonroot
ENTRYPOINT ["/pegasus"]