diff --git a/backend/Dockerfile b/backend/Dockerfile index 4575e55..581ca0d 100644 --- a/backend/Dockerfile +++ b/backend/Dockerfile @@ -1,57 +1,58 @@ +# syntax=docker/dockerfile:1 # ============================================================================= # GoHorse Jobs Backend - Ultra-Optimized Dockerfile -# Target: < 20MB final image (scratch-based) # ============================================================================= -# syntax=docker/dockerfile:1 -# ----------------------------------------------------------------------------- -# Stage 1: Build (with cache mounts) -# ----------------------------------------------------------------------------- +# Stage 1: Build FROM mirror.gcr.io/library/golang:1.24-alpine AS builder +# Instala dependências necessárias para o build +RUN apk add --no-cache git ca-certificates tzdata + WORKDIR /build -# Install build deps (minimal) -RUN apk add --no-cache git ca-certificates tzdata - -# Cache go modules (separate layer for better caching) +# 1. Cache de Módulos: Copia apenas os arquivos de definição primeiro COPY go.mod go.sum ./ -#RUN --mount=type=cache,target=/go/pkg/mod \ -# go mod download && go mod verify -# Copy source +# 2. Download das dependências (Camada pesada que será cacheada) +# Removi o comentário do mount e adicionei o download explícito +RUN --mount=type=cache,target=/go/pkg/mod \ + go mod download && go mod verify + +# 3. Copia o código fonte (só será executado se o código mudar) 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 CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build \ +# 4. Build com Cache de Compilação +# O uso de --mount=type=cache no /root/.cache/go-build acelera builds subsequentes +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) +# Stage 2: Production (scratch) # ----------------------------------------------------------------------------- FROM scratch AS runner -# Copy essentials from builder +# Copia informações de fuso horário e certificados SSL 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 +# Copia o binário e as migrations COPY --from=builder /app/main /main COPY --from=builder /build/migrations /migrations -# Environment +# Configurações de execução ENV PORT=8521 \ - TZ=America/Sao_Paulo + TZ=America/Sao_Paulo \ + GODEBUG=netdns=go \ + GOGC=50 EXPOSE 8521 -ENTRYPOINT ["/main"] +ENTRYPOINT ["/main"] \ No newline at end of file