56 lines
1.6 KiB
Docker
56 lines
1.6 KiB
Docker
# Multi-stage build for optimized production image
|
|
FROM golang:1.23-alpine AS builder
|
|
|
|
# Install build dependencies including buf and protoc
|
|
RUN apk add --no-cache git ca-certificates tzdata curl && \
|
|
curl -sSL "https://github.com/bufbuild/buf/releases/latest/download/buf-$(uname -s)-$(uname -m)" -o /usr/local/bin/buf && \
|
|
chmod +x /usr/local/bin/buf
|
|
|
|
# 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 && \
|
|
go install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway@latest && \
|
|
go install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2@latest
|
|
|
|
# Copy source code
|
|
COPY . .
|
|
|
|
# Generate protobuf code
|
|
RUN buf dep update && buf generate
|
|
|
|
# Build the binary
|
|
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build \
|
|
-ldflags='-w -s -extldflags "-static"' \
|
|
-tags netgo -installsuffix netgo \
|
|
-o server cmd/server/main.go
|
|
|
|
# Final stage: minimal runtime image
|
|
FROM scratch
|
|
|
|
# Copy CA certificates for HTTPS requests
|
|
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 binary
|
|
COPY --from=builder /build/server /server
|
|
|
|
# Expose ports
|
|
EXPOSE 8080 8090
|
|
|
|
# Health check
|
|
HEALTHCHECK --interval=30s --timeout=5s --start-period=5s --retries=3 \
|
|
CMD ["/server", "health"] || exit 1
|
|
|
|
# Run the binary
|
|
ENTRYPOINT ["/server"] |