🐳 feat: implement Docker multi-stage build with caching optimization

Added Docker build infrastructure:
- Multi-stage build (builder, cache, production)
- Dependency hashing for cache invalidation
- GNU tar support for cache compatibility
- Production and development Dockerfiles
- Docker Compose for local development

Build Optimization:
- Dependency-based cache keys
- Layer caching strategy
- Cross-platform compatibility
- Gitea Actions cache integration

Files Added:
- docker/Dockerfile.build - Build environment
- docker/Dockerfile.prod - Production image
- docker/Dockerfile.prod.template - Template-based generation
- docker-compose.yml - Development setup
- scripts/calculate-deps-hash.sh - Cache key calculation
- scripts/test-docker-cache.sh - Cache testing

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
This commit is contained in:
2026-04-09 00:25:53 +02:00
parent a17eebc8f2
commit e2adb3bc9f
8 changed files with 449 additions and 1 deletions

57
docker/Dockerfile Normal file
View File

@@ -0,0 +1,57 @@
# dance-lessons-coach Docker Image
# Multi-stage build for production deployment
# Stage 1: Build binary
FROM golang:1.26.1-alpine AS builder
WORKDIR /app
# Copy go mod files
COPY go.mod go.sum ./
RUN go mod download
# Copy source code
COPY . ./
# Install swag and generate Swagger docs only if they don't exist
RUN if [ ! -f pkg/server/docs/swagger.json ]; then \
echo "📝 Generating Swagger documentation..." && \
go install github.com/swaggo/swag/cmd/swag@latest && \
cd pkg/server && go generate && \
echo "✅ Swagger documentation generated"; \
else \
echo "✅ Swagger documentation already exists, skipping swag installation and generation"; \
fi
# Build binary
RUN CGO_ENABLED=0 GOOS=linux go build -o /dance-lessons-coach ./cmd/server
# Stage 2: Final image
FROM alpine:3.18
WORKDIR /app
# Install dependencies
RUN apk add --no-cache ca-certificates tzdata
# Copy binary from builder
COPY --from=builder /dance-lessons-coach /app/dance-lessons-coach
# Copy configuration
COPY config.yaml /app/config.yaml
# Set permissions
RUN chmod +x /app/dance-lessons-coach
# Set timezone
ENV TZ=UTC
# Expose port
EXPOSE 8080
# Health check
HEALTHCHECK --interval=30s --timeout=3s \
CMD wget -q --spider http://localhost:8080/api/health || exit 1
# Entry point
ENTRYPOINT ["/app/dance-lessons-coach"]

43
docker/Dockerfile.build Normal file
View File

@@ -0,0 +1,43 @@
# Build environment Dockerfile with pre-installed Go tools and dependencies
# Optimized for CI/CD pipeline speed
# Updated to include Node.js for GitHub Actions compatibility
FROM golang:1.26.1-alpine AS builder
# Install build dependencies
RUN apk add --no-cache \
git \
bash \
curl \
make \
gcc \
musl-dev \
bc \
grep \
sed \
jq \
ca-certificates \
nodejs \
npm \
postgresql-client \
tar # Add GNU tar for cache compatibility
# Set up Go environment
ENV GOPATH=/go
ENV PATH=$GOPATH/bin:/usr/local/go/bin:/usr/local/bin:/usr/bin:/bin
WORKDIR /go/src/dance-lessons-coach
# Install common Go tools
RUN go install github.com/swaggo/swag/cmd/swag@latest && \
go install golang.org/x/tools/cmd/goimports@latest && \
go install honnef.co/go/tools/cmd/staticcheck@latest
# Copy only go.mod and go.sum first for dependency caching
COPY go.mod go.sum ./
RUN go mod download && go mod verify
# Simple build environment - source code is mounted at runtime
WORKDIR /workspace
# Pre-download common Go tools (already installed in base)
# RUN go install github.com/swaggo/swag/cmd/swag@latest

37
docker/Dockerfile.prod Normal file
View File

@@ -0,0 +1,37 @@
# dance-lessons-coach Production Docker Image
# ⚠️ DEVELOPMENT ONLY - This file uses 'latest' tag for local testing
# ⚠️ CI/CD generates the correct Dockerfile.prod with proper dependency hash
# ⚠️ For production use, see the CI/CD workflow which generates the correct file
# Use the build cache image as base (latest for local dev only)
FROM gitea.arcodange.lab/arcodange/dance-lessons-coach-build-cache:latest AS builder
# Final minimal image
FROM alpine:3.18
WORKDIR /app
# Install minimal dependencies
RUN apk add --no-cache ca-certificates tzdata
# Copy binary from builder
COPY --from=builder /workspace/dance-lessons-coach /app/dance-lessons-coach
# Copy configuration
COPY config.yaml /app/config.yaml
# Set permissions
RUN chmod +x /app/dance-lessons-coach
# Set timezone
ENV TZ=UTC
# Expose port
EXPOSE 8080
# Health check
HEALTHCHECK --interval=30s --timeout=3s \
CMD wget -q --spider http://localhost:8080/api/health || exit 1
# Entry point
ENTRYPOINT ["/app/dance-lessons-coach"]

View File

@@ -0,0 +1,36 @@
# dance-lessons-coach Production Docker Image
# Minimal image using pre-built binary from CI cache
# Template: Replace {{DEPS_HASH}} with actual dependency hash
# Use the build cache image as base
FROM gitea.arcodange.lab/arcodange/dance-lessons-coach-build-cache:{{DEPS_HASH}} AS builder
# Final minimal image
FROM alpine:3.18
WORKDIR /app
# Install minimal dependencies
RUN apk add --no-cache ca-certificates tzdata
# Copy binary from builder
COPY --from=builder /workspace/dance-lessons-coach /app/dance-lessons-coach
# Copy configuration
COPY config.yaml /app/config.yaml
# Set permissions
RUN chmod +x /app/dance-lessons-coach
# Set timezone
ENV TZ=UTC
# Expose port
EXPOSE 8080
# Health check
HEALTHCHECK --interval=30s --timeout=3s \
CMD wget -q --spider http://localhost:8080/api/health || exit 1
# Entry point
ENTRYPOINT ["/app/dance-lessons-coach"]