Beginner LevelDocker & Deployment
What is Docker Compose and how does docker-compose.yml orchestrate multi-container apps?
❓ Interview Prompt: Explain Docker Compose services, networks, volumes, depends_on, and health checks in a multi-container application setup.
Docker Compose is a tool for defining and running multi-container applications using a single declarative YAML file. Instead of running multiple docker run commands manually, Compose orchestrates all services, networks, and volumes together.
Key Concepts:
•
services: Each service is a container (e.g.,
api, postgres, redis).•
networks: Compose automatically creates a shared network — containers reach each other by service name as hostname.
•
volumes: Named persistent storage shared across services or restarts.
•
depends_on: Declares startup ordering, with
condition: service_healthy waiting for health checks.Common Commands:
•
docker compose up -d — start all services in detached mode.•
docker compose down -v — stop and remove containers + volumes.•
docker compose logs -f api — stream logs from a specific service.•
docker compose ps — list running services.Code Implementation Example
yaml
# docker-compose.yml
version: '3.9'
services:
api:
build: .
ports:
- "3000:3000"
environment:
DATABASE_URL: postgres://user:pass@postgres:5432/appdb
REDIS_URL: redis://redis:6379
depends_on:
postgres:
condition: service_healthy # wait for DB health check
redis:
condition: service_started
postgres:
image: postgres:16-alpine
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: pass
POSTGRES_DB: appdb
volumes:
- pgdata:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U user"]
interval: 5s
retries: 5
redis:
image: redis:7-alpine
volumes:
pgdata:💡Full-stack Docker Compose file: Node API + PostgreSQL (with health check) + Redis on a shared network.
🎯 Interview Key Takeaway
Docker Compose orchestrates multi-container apps via YAML — services find each other by name on a shared network, volumes persist data, and health checks control startup order.
#Docker Compose#Multi-Container#YAML#Networking#Beginner
Back to all Docker & Deployment Questions→