refactor: Enhance all Dockerfiles with security hardening, optimization, and production best practices

- Add maintainer and description labels to all build and runtime stages
- Change builder WORKDIR from /app to /build for clarity
- Install build dependencies with --no-cache/--no-install-recommends flags
- Add build optimizations: -ldflags="-w -s -extldflags '-static'" and -trimpath for Go, strip for Rust
- Implement dependency caching strategy for Rust builds (dummy app pattern)
- Pin Alpine base
This commit is contained in:
nutcas3
2026-04-29 02:45:33 +03:00
parent 095eb0bdf3
commit 3cfd5d672c
6 changed files with 294 additions and 52 deletions
+47 -6
View File
@@ -1,22 +1,63 @@
# Build stage
FROM golang:1.26-alpine AS builder
WORKDIR /app
LABEL maintainer="nutcas3 <[email protected]>" \
description="API Server for Telecom Platform"
WORKDIR /build
# Install build dependencies
RUN apk add --no-cache git ca-certificates
# Copy dependency files
COPY apps/api-server/go.mod apps/api-server/go.sum ./
RUN go mod download
# Copy source code
COPY apps/api-server/ ./
RUN CGO_ENABLED=0 GOOS=linux go build -o api-server .
# Build with optimizations
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build \
-ldflags="-w -s -extldflags '-static'" \
-trimpath \
-o api-server .
FROM alpine:latest
# Runtime stage
FROM alpine:3.20
RUN apk --no-cache add ca-certificates
LABEL maintainer="nutcas3 <[email protected]>" \
description="API Server for Telecom Platform" \
version="1.0.0"
WORKDIR /root/
# Install runtime dependencies
RUN apk add --no-cache ca-certificates tzdata
COPY --from=builder /app/api-server .
# Create non-root user
RUN addgroup -g 1000 appuser && \
adduser -D -u 1000 -G appuser appuser
WORKDIR /app
# Copy binary from builder
COPY --from=builder /build/api-server .
# Change ownership
RUN chown -R appuser:appuser /app
# Switch to non-root user
USER appuser
# Expose port
EXPOSE 8000
# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
CMD wget --no-verbose --tries=1 --spider http://localhost:8000/v1/health || exit 1
# Set environment variables
ENV GIN_MODE=release
# Signal handling
STOPSIGNAL SIGTERM
CMD ["./api-server"]