Files
satusehat-worker/Dockerfile
T
2026-07-30 09:29:49 +07:00

86 lines
1.7 KiB
Docker

# # Multi-stage build yang lebih optimal
# FROM golang:1.22-alpine AS build
# # Install build dependencies
# RUN apk add --no-cache git ca-certificates tzdata
# WORKDIR /build
# # Cache go mod dependencies
# COPY go.mod go.sum ./
# RUN go mod download && go mod verify
# # Copy source code
# COPY . .
# # Build dengan optimasi
# RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
# go build -a -installsuffix cgo \
# -ldflags='-w -s -extldflags "-static"' \
# -o main cmd/api/main.go
# # Final stage - distroless untuk keamanan
# FROM gcr.io/distroless/static:nonroot
# # Copy timezone data
# COPY --from=build /usr/share/zoneinfo /usr/share/zoneinfo
# # Copy binary
# COPY --from=build /build/main /app/main
# # Use non-root user
# USER nonroot:nonroot
# # Health check
# HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
# CMD ["/app/main", "-health"] || exit 1
# EXPOSE 8080
# ENTRYPOINT ["/app/main"]
# syntax=docker/dockerfile:1
# Build stage
FROM golang:1.25-alpine AS build
RUN apk add --no-cache \
git \
ca-certificates \
tzdata
WORKDIR /build
# Cache dependency layer
COPY go.mod go.sum ./
RUN go mod download && go mod verify
# Copy source code
COPY . .
# Build seluruh package, bukan hanya satu file main.go
RUN CGO_ENABLED=0 \
GOOS=linux \
GOARCH=amd64 \
go build \
-trimpath \
-ldflags="-s -w" \
-o /out/main \
./cmd/api
# Runtime stage
FROM gcr.io/distroless/static:nonroot
# Timezone dan CA certificates
COPY --from=build /usr/share/zoneinfo /usr/share/zoneinfo
COPY --from=build /etc/ssl/certs/ca-certificates.crt \
/etc/ssl/certs/ca-certificates.crt
# Application binary
COPY --from=build /out/main /app/main
USER nonroot:nonroot
EXPOSE 8080
ENTRYPOINT ["/app/main"]