diff --git a/Dockerfile b/Dockerfile index d5e2ab9..59f8eba 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ # Build stage -FROM golang:1.24-alpine AS builder +FROM --platform=$BUILDPLATFORM golang:1.24-alpine AS builder # Install build dependencies RUN apk add --no-cache git make @@ -16,12 +16,23 @@ RUN go mod download # Copy source code COPY . . -# Build the application -RUN go build -v -o telemetry ./cmd/telemetry +# Build args from buildx +ARG TARGETOS +ARG TARGETARCH + +# Build static binary for correct platform +RUN CGO_ENABLED=0 \ + GOOS=$TARGETOS \ + GOARCH=$TARGETARCH \ + go build -trimpath -ldflags="-s -w" \ + -o telemetry ./cmd/telemetry # Runtime stage FROM alpine:latest +# Set working directory +WORKDIR /app + # Install runtime dependencies RUN apk --no-cache add ca-certificates tzdata @@ -29,9 +40,6 @@ RUN apk --no-cache add ca-certificates tzdata RUN addgroup -g 1000 -S telemetry && \ adduser -u 1000 -S telemetry -G telemetry -# Set working directory -WORKDIR /app - # Copy binary from builder COPY --from=builder /build/telemetry . COPY --from=builder /build/configs ./configs