gohorsejobs/backend/Dockerfile
Tiago Yamamoto d894012af6 perf(docker): ultra-optimize backend build
- Use scratch base image (< 20MB final vs ~50MB alpine)
- Add BuildKit cache for go modules and build cache
- Selective COPY (cmd, internal, migrations, docs) instead of COPY . .
- Remove HEALTHCHECK (not supported by Podman OCI)
- Update .gitignore with more binary patterns
- Optimize .dockerignore to exclude tests and binaries
2025-12-28 12:05:12 -03:00

59 lines
1.9 KiB
Docker

# =============================================================================
# GoHorse Jobs Backend - Ultra-Optimized Dockerfile
# Target: < 20MB final image (scratch-based)
# =============================================================================
# syntax=docker/dockerfile:1
# -----------------------------------------------------------------------------
# Stage 1: Build (with cache mounts)
# -----------------------------------------------------------------------------
FROM mirror.gcr.io/library/golang:1.24-alpine AS builder
WORKDIR /build
# Install build deps (minimal)
RUN apk add --no-cache git ca-certificates tzdata
# Cache go modules (separate layer for better caching)
COPY go.mod go.sum ./
RUN --mount=type=cache,target=/go/pkg/mod \
go mod download && go mod verify
# Copy source
COPY cmd ./cmd
COPY internal ./internal
COPY migrations ./migrations
COPY docs ./docs
# Build with maximum optimizations:
# - CGO_ENABLED=0: Pure Go binary (no C deps)
# - ldflags -s -w: Strip debug info (-4MB)
# - trimpath: Remove local paths
# - -a: Rebuild all packages
RUN --mount=type=cache,target=/go/pkg/mod \
--mount=type=cache,target=/root/.cache/go-build \
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build \
-ldflags="-s -w -X main.Version=$(git describe --tags --always 2>/dev/null || echo 'dev')" \
-trimpath \
-o /app/main ./cmd/api
# -----------------------------------------------------------------------------
# Stage 2: Production (scratch for minimal size)
# -----------------------------------------------------------------------------
FROM scratch AS runner
# Copy essentials from builder
COPY --from=builder /usr/share/zoneinfo /usr/share/zoneinfo
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
# Copy binary and migrations
COPY --from=builder /app/main /main
COPY --from=builder /build/migrations /migrations
# Environment
ENV PORT=8521 \
TZ=America/Sao_Paulo
EXPOSE 8521
ENTRYPOINT ["/main"]