⬡ Hub
Skip to content

Docker Real-Time Examples and Solutions

Scenario 1: Optimizing Image Size with Multi-Stage Builds

Problem: A Go application's Docker image is 800MB because it includes the entire Go compiler and build tools, even though the final binary is only 10MB. This slows down deployments and increases storage costs.

Solution: Use a multi-stage build. 1. Build Stage: Use a golang:alpine image to compile the code. 2. Final Stage: Use a minimal alpine or distroless image. Copy only the compiled binary from the build stage.

Dockerfile Example:

# Stage 1: Build
FROM golang:1.21-alpine AS builder
WORKDIR /app
COPY . .
RUN go build -o myapp main.go

# Stage 2: Run
FROM alpine:latest
WORKDIR /root/
COPY --from=builder /app/myapp .
CMD ["./myapp"]

Result: Image size reduced from 800MB to ~20MB.

Scenario 2: Local Development Environment with Docker Compose

Problem: Developers struggle to set up the local environment because they need to install Postgres, Redis, and the API dependencies manually. Versions often conflict.

Solution: Create a docker-compose.yml file that defines the entire stack.

docker-compose.yml:

version: '3.8'
services:
  api:
    build: .
    ports:
      - "8080:8080"
    environment:
      - DB_HOST=db
      - REDIS_HOST=cache
    depends_on:
      - db
      - cache

  db:
    image: postgres:15-alpine
    environment:
      POSTGRES_PASSWORD: secretpassword
    volumes:
      - db_data:/var/lib/postgresql/data

  cache:
    image: redis:alpine

volumes:
  db_data:

Benefit: Developers just run docker-compose up to start the entire environment.