Spaces:
Running
Running
| {% if config.multiStage %} | |
| # Multi-stage build for Go | |
| FROM golang:{{ config.goVersion or '1.21' }}-alpine AS builder | |
| WORKDIR /app | |
| # Install git for go mod download | |
| RUN apk add --no-cache git | |
| # Copy go mod files | |
| COPY go.mod go.sum* ./ | |
| # Download dependencies | |
| RUN go mod download | |
| # Copy source code | |
| COPY . . | |
| # Build application with static linking and optimizations | |
| RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -ldflags="-w -s" -o main {{ config.mainPackage or '.' }} | |
| # Production stage - minimal image | |
| FROM alpine:latest | |
| # Install ca-certificates for HTTPS | |
| RUN apk --no-cache add ca-certificates | |
| WORKDIR /root/ | |
| # Copy binary from builder | |
| COPY --from=builder /app/main . | |
| {% else %} | |
| # Go {{ config.goVersion or '1.21' }} Dockerfile | |
| FROM golang:{{ config.goVersion or '1.21' }}-alpine | |
| WORKDIR /app | |
| # Install build dependencies | |
| RUN apk add --no-cache git | |
| # Copy go mod files | |
| COPY go.mod go.sum* ./ | |
| # Download dependencies | |
| RUN go mod download | |
| # Copy source code | |
| COPY . . | |
| {% if not config.developmentMode %} | |
| # Build application | |
| RUN go build -o main {{ config.mainPackage or '.' }} | |
| {% endif %} | |
| {% endif %} | |
| # Expose port | |
| EXPOSE {{ config.port or '8080' }} | |
| # Create non-root user | |
| RUN addgroup -g 1001 -S golang && \ | |
| adduser -S golang -u 1001 && \ | |
| chown -R golang:golang /root/ | |
| USER golang | |
| {% if config.developmentMode %} | |
| # Development mode with hot-reload | |
| RUN go install github.com/cosmtrek/air@latest | |
| CMD ["air", "-c", ".air.toml"] | |
| {% else %} | |
| # Production mode | |
| CMD ["./main"] | |
| {% endif %} | |