Audit 2026-05-02 (Tâche 6 Phase A) had identified 3 inconsistent formats across the ADR corpus : - F1 list bullets : `* Status:` / `* Date:` / `* Deciders:` (11 ADRs) - F2 bold fields : `**Status:**` / `**Date:**` / `**Authors:**` (9 ADRs) - F3 dedicated section : `## Status\n**Value** ✅` (5 ADRs) Mixed metadata names (Authors / Deciders / Decision Date / Implementation Date / Implementation Status / Last Updated) and decorative emojis on status values made the corpus hard to scan or template against. Canonical format adopted (see adr/README.md for full template) : # NN. Title **Status:** <Proposed|Accepted|Implemented|Partially Implemented| Approved|Rejected|Deferred|Deprecated|Superseded by ADR-NNNN> **Date:** YYYY-MM-DD **Authors:** Name(s) [optional **Field:** ... lines] ## Context... Transformations applied (via /tmp/homogenize-adrs.py) : - F1 list bullets → bold fields - F2 cleanup : `**Deciders:**` → `**Authors:**`, strip status emojis - F3 sections : `## Status\n**Value** ✅` → `**Status:** Value` - Strip decorative emojis from `**Status:**` and `**Implementation Status:**` - Convert any `* Implementation Status:` / `* Last Updated:` / `* Decision Drivers:` / `* Decision Date:` to bold equivalents - Date typo fix : `2024-04-XX` → `2026-04-XX` for ADRs 0018, 0019 (already noted in PR #17 but here re-applied since branch starts from origin/main pre-PR17) - Normalize multiple blank lines after header (max 1) 21 / 23 ADRs modified. 0010 and 0012 were already conform. 0011 and 0014 do not exist in the repo (cf. README index update). Body content of each ADR is preserved unchanged. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
17 KiB
ADR 0020: Docker Build Strategy - Traditional vs Buildx
Status: Accepted
Context
The dance-lessons-coach CI/CD pipeline initially used Docker Buildx (docker buildx build --push) for building and pushing Docker cache images. However, this approach encountered several issues:
Issues with Buildx Approach
- TLS Certificate Problems: Buildx had difficulty with self-signed certificates, requiring complex workaround steps
- Performance Concerns: Buildx setup and execution was significantly slower than expected
- Complexity: Buildx introduced additional complexity without providing immediate benefits
- Reliability Issues: Buildx builds were less reliable in the GitHub Actions environment
Working Solution Analysis
The working webapp CI/CD pipeline uses traditional docker build + docker push approach:
# Working approach from webapp
- name: Build and push image to Gitea Container Registry
run: |-
docker build -t app .
docker tag app gitea.arcodange.lab/${{ github.repository }}:$TAG
docker push gitea.arcodange.lab/${{ github.repository }}:$TAG
This approach is simpler, more reliable, and works consistently with self-signed certificates.
Decision
Replace Docker Buildx with traditional docker build + push for the CI/CD pipeline and implement a two-stage Docker build strategy.
Implementation
1. Build Cache Strategy
# Build cache using traditional docker build
- name: Build and push Docker cache image
if: steps.check_cache.outputs.cache_hit == 'false'
run: |
IMAGE_NAME="${{ env.CI_REGISTRY }}/${{ env.GITEA_ORG }}/${{ env.GITEA_REPO }}-build-cache:${{ steps.calculate_hash.outputs.deps_hash }}"
echo "Building cache image: $IMAGE_NAME"
# Build the image using traditional docker build
docker build \
--file Dockerfile.build \
--tag "$IMAGE_NAME" \
.
# Push the image
docker push "$IMAGE_NAME"
echo "✅ Build cache image pushed successfully"
2. Production Build Strategy
# Production build using Dockerfile.prod
- name: Build and push Docker image
if: github.ref == 'refs/heads/main'
run: |
source VERSION
IMAGE_VERSION="$MAJOR.$MINOR.$PATCH${PRERELEASE:+-$PRERELEASE}"
TAGS="$IMAGE_VERSION latest ${{ github.sha }}"
echo "Building Docker image with tags: $TAGS"
# Use the production Dockerfile that leverages the build cache
docker build -t dance-lessons-coach -f Dockerfile.prod .
for TAG in $TAGS; do
IMAGE_NAME="${{ env.CI_REGISTRY }}/${{ env.GITEA_ORG }}/${{ env.GITEA_REPO }}:$TAG"
echo "Tagging and pushing: $IMAGE_NAME"
docker tag dance-lessons-coach "$IMAGE_NAME"
docker push "$IMAGE_NAME"
done
3. Dockerfile Structure
Dockerfile.build - Build environment with all dependencies:
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
# Install Go tools
RUN go install github.com/swaggo/swag/cmd/swag@latest
# Copy and verify dependencies
COPY go.mod go.sum ./
RUN go mod download && go mod verify
WORKDIR /workspace
Dockerfile.prod - Minimal production image:
# Use the build cache image as base
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 and entrypoint
RUN chmod +x /app/dance-lessons-coach
ENV TZ=UTC
EXPOSE 8080
ENTRYPOINT ["/app/dance-lessons-coach"]
docker/Dockerfile - Development Dockerfile (kept for local development):
# Multi-stage build for development
FROM golang:1.26.1-alpine AS builder
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY . ./
RUN go build -o /dance-lessons-coach ./cmd/server
FROM alpine:3.18
WORKDIR /app
RUN apk add --no-cache ca-certificates tzdata
COPY --from=builder /dance-lessons-coach /app/dance-lessons-coach
COPY config.yaml /app/config.yaml
RUN chmod +x /app/dance-lessons-coach
ENV TZ=UTC
EXPOSE 8080
ENTRYPOINT ["/app/dance-lessons-coach"]
File Organization
All Dockerfiles are now organized in the docker/ directory:
docker/Dockerfile- Development Dockerfiledocker/Dockerfile.build- Build cache Dockerfiledocker/Dockerfile.prod- Production Dockerfile (development only, uses latest)docker/Dockerfile.prod.template- Template for reference
This organization keeps the root directory clean and makes it clear which files are for development vs production.
Benefits
CI/CD Pipeline Benefits
- Simplicity: Traditional approach is easier to understand and debug
- Reliability: Consistent behavior across different environments
- Certificate Handling: Works seamlessly with self-signed certificates
- Performance: Faster execution without Buildx overhead
- Compatibility: Better compatibility with GitHub Actions environment
Two-Stage Build Benefits
- Separation of Concerns: Clear separation between build environment and production runtime
- Optimized Production Image: Minimal Alpine-based image with only necessary dependencies
- Reusable Build Cache: Build environment can be reused across multiple CI runs
- Faster CI Execution: Pre-built build cache reduces CI execution time
- Consistent Builds: All builds use the same build environment
Development vs Production Clarity
- Development Dockerfile: Full build environment for local development
- Production Dockerfile: Minimal runtime environment for deployment
- Build Cache Dockerfile: Optimized build environment for CI/CD
- Clear Documentation: Each Dockerfile has a specific purpose
Trade-offs
What We Lose
- Multi-platform builds: Cannot build for multiple architectures simultaneously
- BuildKit caching: Less sophisticated caching mechanism
- Advanced features: No secret mounting, SSH agents, etc.
- Parallel processing: Slower builds without Buildx optimizations
What We Gain
- Stability: More reliable CI/CD pipeline
- Simplicity: Easier to maintain and troubleshoot
- Consistency: Matches proven patterns from working projects
- Faster feedback: Quicker build times in practice
- Clear Separation: Better distinction between development and production builds
- Optimized Production: Smaller, more secure production images
Rationale
- Current Needs: We don't need multi-platform builds or advanced BuildKit features
- Simple Dockerfile: Our
Dockerfile.builddoesn't require Buildx-specific features - Proven Pattern: Traditional approach works reliably in production (webapp project)
- CI Stability: Reliability is more important than advanced features for CI/CD
- Build Strategy: Two-stage build provides better separation of concerns
- Maintenance: Simpler approach is easier to maintain and debug
Critical Bug Fix: Dependency Hash Usage
Issue Identified
The initial implementation had a critical bug where Dockerfile.prod used latest tag instead of the specific dependency hash:
# ❌ WRONG - this would never work
FROM gitea.arcodange.lab/arcodange/dance-lessons-coach-build-cache:latest AS builder
This approach would never work because:
- The build cache images are tagged with specific dependency hashes
- No image is ever tagged as
latest - The CI/CD workflow would fail to find the cache image
Solution Implemented
- Dynamic Dockerfile Generation: The CI/CD workflow now generates
Dockerfile.proddynamically with the correct dependency hash - Dependency Hash Calculation: Added
scripts/calculate-deps-hash.shfor consistent hash calculation - Template Approach: Created
Dockerfile.prod.templatefor reference
CI/CD Workflow Fix
# ✅ CORRECT - generate Dockerfile.prod with proper hash
- name: Build and push Docker image
if: github.ref == 'refs/heads/main'
run: |
# Generate Dockerfile.prod with correct dependency hash
DEPS_HASH="${{ needs.build-cache.outputs.deps_hash }}"
# Create Dockerfile.prod with the correct cache image tag
cat > Dockerfile.prod << EOF
FROM gitea.arcodange.lab/arcodange/dance-lessons-coach-build-cache:$DEPS_HASH AS builder
# ... rest of Dockerfile
EOF
# Build using the generated Dockerfile
docker build -t dance-lessons-coach -f Dockerfile.prod .
CI/CD Pipeline Optimization
Changes Made
- Removed Buildx Setup: Eliminated
docker/setup-buildx-action@v3from CI/CD workflow - Removed Go Build Steps: Removed
actions/setup-go@v4,go mod tidy, and individual Go tool installations - Added Docker Cache Usage: All build steps now use the pre-built Docker cache image
- Updated Production Build: Production Docker build now generates
Dockerfile.proddynamically with correct dependency hash
CI/CD Workflow Structure
# CI Pipeline Job Structure
jobs:
build-cache:
# Builds Docker cache image if needed
# Note: No certificate configuration needed with traditional docker
ci-pipeline:
needs: build-cache
steps:
- name: Set up build environment
# Sets CACHE_IMAGE variable with proper tag
# No Buildx setup, no Go installation, no certificate configuration
- name: Generate Swagger Docs using Docker cache
# Uses: docker run ${{ env.CACHE_IMAGE }} sh -c "cd pkg/server && go generate"
- name: Build all packages using Docker cache
# Uses: docker run ${{ env.CACHE_IMAGE }} sh -c "go build ./..."
- name: Run tests with coverage using Docker cache
# Uses: docker run ${{ env.CACHE_IMAGE }} sh -c "go test ./..."
- name: Build and push Docker image
# Uses: docker build -t dance-lessons-coach -f Dockerfile.prod .
# No Buildx, no certificate issues
Key Improvements
- Faster Execution: No need to set up Go environment for each job
- Consistent Environment: All builds use the same Docker cache image
- Reduced Complexity: Simpler workflow with fewer steps
- Better Error Handling: Docker cache handles dependency management
- No Certificate Configuration: Traditional docker works seamlessly with self-signed certificates
- Improved Reliability: Elimination of Buildx-related failures
Future Considerations
When to Reconsider Buildx
- Multi-platform needs: If we need ARM/AMD64 builds simultaneously
- Complex builds: If Dockerfile requires BuildKit-specific features
- Performance optimization: If build times become unacceptable
- Certificate issues resolved: If Docker Buildx improves self-signed certificate handling
Migration Path
If we need to reintroduce Buildx in the future:
- Fix certificate issues properly at the Docker daemon level
- Test thoroughly in staging environment
- Monitor performance impact
- Document benefits clearly for the specific use case
Alternatives Considered
Option 1: Keep Buildx with Certificate Workaround
- ❌ Complex setup with questionable reliability
- ❌ Slow performance in GitHub Actions
- ❌ Ongoing maintenance burden
Option 2: Use Insecure Registry Flag
docker buildx build --allow security.insecure --push .
- ❌ Security concerns
- ❌ Not recommended for production
- ❌ Temporary workaround, not solution
Option 3: Traditional Docker Build + Push ✅ CHOSEN
- ✅ Simple and reliable
- ✅ Proven in production
- ✅ Better performance in practice
- ✅ Easy to maintain
Decision Outcome
Chosen Option: Traditional docker build + push (Option 3)
This decision prioritizes CI/CD reliability and simplicity over advanced features we don't currently need. The traditional approach has been proven to work consistently in our environment and matches the successful pattern from the webapp project.
Success Metrics
CI/CD Pipeline Metrics
- CI/CD reliability: No TLS certificate failures
- Build consistency: Predictable build times
- Maintenance: Reduced complexity and debugging time
- Compatibility: Works across all target environments
Build Strategy Metrics
- Cache hit rate: Percentage of CI runs using existing cache
- Build time reduction: Comparison of build times with vs without cache
- Image size: Production image size vs development image size
- CI execution time: Total CI pipeline duration
Quality Metrics
- Build reproducibility: Consistent builds across different environments
- Error rate: Reduction in CI/CD failures
- Recovery time: Time to recover from cache misses
- Resource utilization: Memory and CPU usage during builds
Implementation Checklist
- Create
Dockerfile.prodfor production builds - Update
Dockerfile.buildfor build cache - Keep
Dockerfilefor development use - Remove Docker Buildx from CI/CD workflow
- Remove Go build steps from CI/CD workflow
- Remove certificate configuration step (no longer needed)
- Add Docker cache usage to all build steps
- Fix Dockerfile.prod to use proper dependency hash (not latest)
- Create dependency hash calculation script
- Create build cache environment test script
- Update CI/CD workflow to generate Dockerfile.prod dynamically
- Update ADR 0020 with comprehensive documentation
- Test changes locally
- Push changes to trigger CI/CD workflow
- Monitor workflow execution
- Verify successful completion
- Document results and metrics
Testing and Validation
Build Cache Environment Testing
A comprehensive test script is provided to validate the build cache environment:
# Test the build cache environment (simulates Gitea act runner)
./scripts/test-build-cache-environment.sh
This script tests:
- Dependency hash calculation
- Build cache image creation
- Go environment inside container
- Swagger generation
- Go build and test
- Binary build
- Production Dockerfile with cache
- Production container runtime
Dependency Hash Calculation
# Calculate dependency hash (used for cache image tagging)
./scripts/calculate-deps-hash.sh
# Export to file for use in scripts
./scripts/calculate-deps-hash.sh deps_hash.env
source deps_hash.env
echo "Hash: $DEPS_HASH"
Workflow Monitoring
# Monitor the workflow
./scripts/gitea-client.sh monitor-workflow arcodange dance-lessons-coach 420 30
# Check job status
./scripts/gitea-client.sh job-status arcodange dance-lessons-coach 420
# List workflow jobs
./scripts/gitea-client.sh list-workflow-jobs arcodange dance-lessons-coach 420
Validation Commands
# Verify CI/CD changes
./scripts/verify-cicd-changes.sh
# Test new CI/CD workflow
./scripts/test-new-cicd.sh
# Check Dockerfile syntax
docker run --rm -i hadolint/hadolint < Dockerfile.prod
Cleanup and Organization
Files Removed
- docker-compose.cicd-test.yml: Unused Docker Compose file
- scripts/cicd/: Old CI/CD test scripts (replaced by main test scripts)
Files Organized
All Dockerfiles moved to docker/ directory:
docker/Dockerfile- Developmentdocker/Dockerfile.build- Build cachedocker/Dockerfile.prod- Production (dev only)docker/Dockerfile.prod.template- Template
Utility Scripts
scripts/calculate-deps-hash.sh- Consistent hash calculationscripts/test-local-ci-cd.sh- Main local testingscripts/test-build-cache-environment.sh- Build cache testing
Expected Outcomes
- Successful workflow execution: Workflow completes without errors
- Cache image created: Build cache image pushed to registry
- Production image built: Final Docker image built using generated
docker/Dockerfile.prod - Faster CI execution: Reduced build times compared to previous approach
- No certificate errors: No TLS certificate verification failures
- Clean organization: No clutter in root directory
References
- Docker Buildx Documentation
- Docker Build Documentation
- GitHub Actions Docker Examples
- webapp CI/CD Pipeline
- Docker Multi-stage Builds
- Alpine Linux Docker Images
Approved by: @arcodange Date: 2026-04-07 Updated: 2026-04-07 Supersedes: None Superseded by: None