This section covers containerizing the frontend and backend applications using Docker with multi-stage builds for optimal production images.
- Build Stage: Compile and build the application
- Production Stage: Minimal runtime environment
- Security: Non-root user, minimal attack surface
- Performance: Optimized image size
The frontend uses a two-stage build:
- Builder Stage: Node.js environment for building Vue app
- Production Stage: Nginx for serving static files
- 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;"]The backend uses Go's excellent static compilation:
- Builder Stage: Go environment for compilation
- Production Stage: Minimal Alpine Linux
- 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"]For local development, we use Docker Compose to orchestrate:
- PostgreSQL database
- Backend API server
- Frontend application
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# 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- Non-root user execution
- Minimal base images
- Security headers in Nginx
- No sensitive data in images
- Multi-stage builds for smaller images
- Gzip compression
- Static asset optimization
- Connection pooling
- Health check endpoints
- Structured logging
- Metrics collection
Continue to Local Kubernetes with Kind to learn about local Kubernetes deployment.