3e84b9c3ad
Build on the native host arch (--platform=$BUILDPLATFORM) and cross-compile the static binary per target via buildx-provided TARGETOS/TARGETARCH, so `make docker-build` with REGISTRY set produces linux/amd64 + linux/arm64 images without emulating the Go toolchain under QEMU. Also correct the CMD comment to the real env var (NOTIFIER_SERVER_MODE). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
74 lines
2.0 KiB
Docker
74 lines
2.0 KiB
Docker
# Build stage — pinned to the build host's arch so cross-compilation
|
|
# via GOOS/GOARCH is fast and avoids QEMU emulation of the toolchain.
|
|
FROM --platform=$BUILDPLATFORM golang:1.25-alpine AS builder
|
|
|
|
# Platform args populated by buildx for each target in a multi-platform build.
|
|
ARG TARGETOS
|
|
ARG TARGETARCH
|
|
|
|
# Build arguments
|
|
ARG VERSION=dev
|
|
ARG GIT_COMMIT=unknown
|
|
ARG BUILD_TIME=unknown
|
|
ARG BUILD_FLAGS="-s -w"
|
|
|
|
# Install build dependencies including protoc
|
|
RUN apk add --no-cache git make protobuf protobuf-dev
|
|
|
|
# Set working directory
|
|
WORKDIR /build
|
|
|
|
# Copy go mod files
|
|
COPY go.mod go.sum ./
|
|
|
|
# Download dependencies
|
|
RUN go mod download
|
|
|
|
# Install protoc plugins
|
|
RUN go install google.golang.org/protobuf/cmd/protoc-gen-go@latest && \
|
|
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
|
|
|
|
# Copy source code
|
|
COPY . .
|
|
|
|
# Generate protobuf code
|
|
RUN make proto-gen
|
|
|
|
# Build binary with version information, cross-compiling to the target arch.
|
|
RUN CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -a -installsuffix cgo \
|
|
-ldflags "-X main.Version=${VERSION} -X main.GitCommit=${GIT_COMMIT} -X main.BuildTime=${BUILD_TIME} ${BUILD_FLAGS}" \
|
|
-o server ./cmd/server
|
|
|
|
# Runtime stage
|
|
FROM alpine:3.21
|
|
|
|
# Install runtime dependencies
|
|
RUN apk --no-cache add ca-certificates tzdata
|
|
|
|
# Create non-root user
|
|
RUN addgroup -g 1000 notifier && \
|
|
adduser -D -u 1000 -G notifier notifier
|
|
|
|
# Set working directory
|
|
WORKDIR /app
|
|
|
|
# Copy binary from builder
|
|
COPY --from=builder /build/server /app/
|
|
|
|
# Copy default config (can be overridden with volume mount)
|
|
COPY config.yaml /app/config.yaml
|
|
|
|
# Create directory for queue persistence
|
|
RUN mkdir -p /var/lib/notifier && \
|
|
chown -R notifier:notifier /var/lib/notifier
|
|
|
|
# Change to non-root user
|
|
USER notifier
|
|
|
|
# Expose ports
|
|
EXPOSE 8080 50051 9090 8081
|
|
|
|
# Run server (defaults to both REST and gRPC)
|
|
# Override mode with environment variable: -e NOTIFIER_SERVER_MODE=rest or -e NOTIFIER_SERVER_MODE=grpc
|
|
CMD ["/app/server"]
|