# Build stage FROM golang:1.23-alpine AS builder # Install build dependencies RUN apk add --no-cache git ca-certificates tzdata # Set working directory WORKDIR /app # Copy go mod files COPY go.mod go.sum ./ # Download dependencies RUN go mod download # Copy source code COPY . . # Build the API binary RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-w -s" -o /app/api ./cmd/api # Build the worker binary RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-w -s" -o /app/worker ./cmd/worker # Build the admin binary RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-w -s" -o /app/admin ./cmd/admin # Final stage - API FROM alpine:3.19 AS api # Install runtime dependencies RUN apk add --no-cache ca-certificates tzdata # Create non-root user RUN addgroup -g 1000 app && adduser -u 1000 -G app -s /bin/sh -D app # Set working directory WORKDIR /app # Copy binary from builder COPY --from=builder /app/api /app/api COPY --from=builder /app/templates /app/templates # Create uploads directory RUN mkdir -p /app/uploads && chown -R app:app /app # Switch to non-root user USER app # Expose port EXPOSE 8000 # Health check HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \ CMD wget --no-verbose --tries=1 --spider http://localhost:8000/api/health/ || exit 1 # Run the API CMD ["/app/api"] # Final stage - Worker FROM alpine:3.19 AS worker # Install runtime dependencies RUN apk add --no-cache ca-certificates tzdata # Create non-root user RUN addgroup -g 1000 app && adduser -u 1000 -G app -s /bin/sh -D app # Set working directory WORKDIR /app # Copy binary from builder COPY --from=builder /app/worker /app/worker COPY --from=builder /app/templates /app/templates # Switch to non-root user USER app # Run the worker CMD ["/app/worker"] # Final stage - Admin FROM alpine:3.19 AS admin # Install runtime dependencies RUN apk add --no-cache ca-certificates tzdata # Create non-root user RUN addgroup -g 1000 app && adduser -u 1000 -G app -s /bin/sh -D app # Set working directory WORKDIR /app # Copy binary from builder COPY --from=builder /app/admin /app/admin # Create uploads directory for GoAdmin RUN mkdir -p /app/uploads && chown -R app:app /app # Switch to non-root user USER app # Expose port EXPOSE 9000 # Run the admin panel CMD ["/app/admin"]