Skip to content

Latest commit

 

History

History
150 lines (118 loc) · 3.14 KB

File metadata and controls

150 lines (118 loc) · 3.14 KB

Docker Configuration

This section covers containerizing the frontend and backend applications using Docker with multi-stage builds for optimal production images.

Docker Strategy

Multi-Stage Builds

  • Build Stage: Compile and build the application
  • Production Stage: Minimal runtime environment
  • Security: Non-root user, minimal attack surface
  • Performance: Optimized image size

Frontend Dockerfile

The frontend uses a two-stage build:

  1. Builder Stage: Node.js environment for building Vue app
  2. Production Stage: Nginx for serving static files

Key Features

  • Nginx configuration with API proxying
  • Gzip compression enabled
  • Security headers configured
  • Client-side routing support
# Build stage
FROM node:18-alpine AS builder
WORKDIR /app
COPY frontend/package*.json ./
RUN npm ci --only=production
COPY frontend/ ./
RUN npm run build

# Production stage
FROM nginx:alpine
COPY --from=builder /app/dist /usr/share/nginx/html
COPY docker/nginx.conf /etc/nginx/conf.d/default.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

Backend Dockerfile

The backend uses Go's excellent static compilation:

  1. Builder Stage: Go environment for compilation
  2. Production Stage: Minimal Alpine Linux

Key Features

  • Static binary compilation (CGO_ENABLED=0)
  • Minimal Alpine base image
  • CA certificates for HTTPS calls
  • Non-root execution
# Build stage
FROM golang:1.21-alpine AS builder
WORKDIR /app
RUN apk add --no-cache git
COPY backend/go.mod backend/go.sum ./
RUN go mod download
COPY backend/ ./
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main .

# Production stage
FROM alpine:latest
RUN apk --no-cache add ca-certificates
WORKDIR /root/
COPY --from=builder /app/main .
EXPOSE 8080
CMD ["./main"]

Docker Compose

For local development, we use Docker Compose to orchestrate:

  • PostgreSQL database
  • Backend API server
  • Frontend application

Services Configuration

services:
  postgres:
    image: postgres:15-alpine
    environment:
      POSTGRES_DB: tutorial_db
      POSTGRES_USER: user
      POSTGRES_PASSWORD: password
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U user -d tutorial_db"]
      
  backend:
    build:
      context: .
      dockerfile: docker/Dockerfile.backend
    depends_on:
      postgres:
        condition: service_healthy
        
  frontend:
    build:
      context: .
      dockerfile: docker/Dockerfile.frontend
    depends_on:
      - backend

Development Commands

# Build images
make build-images

# Start development environment
make dev-up

# View logs
docker-compose logs -f

# Stop environment
make dev-down

# Clean up
make clean

Production Considerations

Security

  • Non-root user execution
  • Minimal base images
  • Security headers in Nginx
  • No sensitive data in images

Performance

  • Multi-stage builds for smaller images
  • Gzip compression
  • Static asset optimization
  • Connection pooling

Monitoring

  • Health check endpoints
  • Structured logging
  • Metrics collection

Next Steps

Continue to Local Kubernetes with Kind to learn about local Kubernetes deployment.