59 lines
1.1 KiB
Docker
59 lines
1.1 KiB
Docker
# Dockerfile
|
|
# Production-ready multi-stage build for server deployment
|
|
|
|
# Build stage
|
|
FROM golang:1.23-alpine AS builder
|
|
|
|
# Install necessary build tools
|
|
RUN apk add --no-cache git make gcc libc-dev
|
|
|
|
# Set working directory
|
|
WORKDIR /build
|
|
|
|
# Copy go.mod and go.sum files
|
|
COPY go.mod go.sum* ./
|
|
|
|
# Download dependencies
|
|
RUN go mod download
|
|
|
|
# Copy the entire project
|
|
COPY . .
|
|
|
|
# Build the application with optimizations
|
|
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-w -s" -o /build/bin/api ./cmd/api
|
|
|
|
# Final stage
|
|
FROM alpine:3.19
|
|
|
|
# Add necessary runtime packages
|
|
RUN apk add --no-cache ca-certificates tzdata
|
|
|
|
# Set timezone
|
|
ENV TZ=Asia/Ho_Chi_Minh
|
|
|
|
# Create non-root user
|
|
RUN adduser -D -g '' appuser
|
|
|
|
# Create app directories
|
|
RUN mkdir -p /app/config /app/logs /app/storage
|
|
WORKDIR /app
|
|
|
|
# Copy binary from builder stage
|
|
COPY --from=builder /build/bin/api /app/
|
|
COPY --from=builder /build/config /app/config
|
|
|
|
# Set ownership
|
|
RUN chown -R appuser:appuser /app
|
|
|
|
# Switch to non-root user
|
|
USER appuser
|
|
|
|
# Expose port
|
|
EXPOSE 8080
|
|
|
|
# Set environment variable for production
|
|
ENV APP_ENV=production
|
|
|
|
# Command to run the application
|
|
ENTRYPOINT ["/app/api"]
|