Build a Realistic Multi-Container Application
You will assemble a production-shaped app: a reverse proxy in front of an API, a background worker, and a database with persistent storage. By the end you will run a realistic topology with config via environment and secrets.
Learning Objectives
- Compose a 4-tier app (proxy, API, worker, DB).
- Inject configuration with environment and secrets.
- Scale a stateless service horizontally.
- Time: ~4 hours · Difficulty: Intermediate · Prereqs: the Compose lab completed.
Architecture Overview
graph LR
U[User] -->|443| Px[Nginx proxy]
Px -->|HTTP| API[API service]
API --> DB[(PostgreSQL)]
API -->|enqueue| Q[(Redis)]
Q --> W[Worker]
W --> DB
Environment Setup
You will need: Docker Compose v2 and the proxy/API/worker images (or simple placeholders).
Before you begin: never bake secrets into images; pass them at runtime.
Step-by-Step Execution
01
Define the multi-service stack
Each tier is a service; the proxy is the only one exposed to the host.
docker compose -f compose.yaml configValidates and renders the merged configuration.
02
Provide secrets at runtime
printf 'supersecret' | docker secret create db_password - 2>/dev/null || echo "use env_file for compose"03
Bring it up and scale the API
$ docker compose up -d --scale api=3
[+] Running 6/6
proj-api-1 Started proj-api-2 Started proj-api-3 Started
Progress So Far
graph LR
A[01 Define stack] -->|done| B[02 Secrets]
B -->|done| C[03 Up + scale]
style A fill:#1a4a1a,stroke:#00ff00,color:#fff
style B fill:#1a4a1a,stroke:#00ff00,color:#fff
style C fill:#1a4a1a,stroke:#00ff00,color:#fff
Testing & Validation
for i in 1 2 3; do curl -s http://localhost/api/whoami; doneYou should see responses from different API replicas, confirming load distribution behind the proxy.
Troubleshooting
- Only one replica answers: ensure the proxy load-balances across the service name, not a fixed container.
- Worker idle: confirm it consumes the same Redis queue the API publishes to.
- Secret not found: Compose uses
secrets:with files; Swarm usesdocker secret.
Extension Ideas
- Translate this topology to Kubernetes Deployments and Services.
- Add autoscaling and resource limits.
- Front it with the Cloudflare Tunnel pattern.
Key Results
- Ran a 4-tier app with only the proxy exposed.
- Injected configuration and secrets at runtime, not in images.
- Scaled the stateless API to 3 replicas behind the proxy.
- Verified load distribution across replicas.