63 lines
1.5 KiB
Docker
63 lines
1.5 KiB
Docker
|
|
# Build stage
|
||
|
|
FROM golang:1.23-alpine AS builder
|
||
|
|
|
||
|
|
# Install build dependencies
|
||
|
|
RUN apk add --no-cache git
|
||
|
|
|
||
|
|
# Set working directory
|
||
|
|
WORKDIR /app
|
||
|
|
|
||
|
|
# Copy go mod files
|
||
|
|
COPY go-app/go.mod go-app/go.sum ./
|
||
|
|
|
||
|
|
# Download dependencies
|
||
|
|
RUN go mod download
|
||
|
|
|
||
|
|
# Copy source code
|
||
|
|
COPY go-app/ .
|
||
|
|
|
||
|
|
# Install sqlc (compatible with Go 1.23+)
|
||
|
|
RUN go install github.com/sqlc-dev/sqlc/cmd/sqlc@latest
|
||
|
|
|
||
|
|
# Generate sqlc code
|
||
|
|
RUN sqlc generate
|
||
|
|
|
||
|
|
# Build the application with production optimizations
|
||
|
|
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -ldflags="-w -s" -tags production -o server cmd/server/main.go
|
||
|
|
|
||
|
|
# Runtime stage - minimal image for production
|
||
|
|
FROM alpine:latest
|
||
|
|
|
||
|
|
# Install only essential runtime dependencies
|
||
|
|
RUN apk --no-cache add ca-certificates && \
|
||
|
|
addgroup -g 1001 -S appgroup && \
|
||
|
|
adduser -u 1001 -S appuser -G appgroup
|
||
|
|
|
||
|
|
WORKDIR /app
|
||
|
|
|
||
|
|
# Copy the binary from builder
|
||
|
|
COPY --from=builder /app/server .
|
||
|
|
|
||
|
|
# Copy templates and static files
|
||
|
|
COPY go-app/templates ./templates
|
||
|
|
COPY go-app/static ./static
|
||
|
|
|
||
|
|
# Copy production environment file
|
||
|
|
COPY go-app/.env.production .env
|
||
|
|
|
||
|
|
# Create credentials directory with proper permissions
|
||
|
|
RUN mkdir -p ./credentials && \
|
||
|
|
chown -R appuser:appgroup /app
|
||
|
|
|
||
|
|
# Switch to non-root user
|
||
|
|
USER appuser
|
||
|
|
|
||
|
|
# Expose port
|
||
|
|
EXPOSE 8080
|
||
|
|
|
||
|
|
# Health check
|
||
|
|
HEALTHCHECK --interval=60s --timeout=10s --start-period=10s --retries=3 \
|
||
|
|
CMD wget --no-verbose --tries=1 --spider http://localhost:8080/api/v1/health || exit 1
|
||
|
|
|
||
|
|
# Run the application
|
||
|
|
CMD ["./server"]
|