π Docker Compose β Summary
Youβve reached the end of the Docker Compose & Multi-Container section. Hereβs everything youβve learned, condensed into a quick reference.
π What is Docker Compose?
A tool that lets you define and run multiple containers as one application using a single docker-compose.yml file.
One command to start everything:
docker compose upποΈ The docker-compose.yml Structure
services: service-name: build: ./path-to-dockerfile # or use 'image:' for pre-built images ports: - "host:container" environment: - KEY=value depends_on: - other-service volumes: - named-volume:/path/in/container
volumes: named-volume:π Core Concepts at a Glance
| Concept | What it means |
|---|---|
| Service | One container defined in docker-compose.yml |
| Network | All services share a network automatically |
| Service name as hostname | Services reach each other by name (e.g. db, backend) |
depends_on | Controls startup order |
| Named volumes | Persist data across container restarts/removals |
build | Build image from a local Dockerfile |
image | Pull a pre-built image from Docker Hub |
π Essential Commands
| Command | What it does |
|---|---|
docker compose up | Start all services |
docker compose up --build | Rebuild images then start |
docker compose up -d | Start in background (detached mode) |
docker compose down | Stop and remove containers |
docker compose down -v | Also remove named volumes |
docker compose ps | List running services |
docker compose logs -f | Follow live logs |
docker compose exec <svc> sh | Open a shell in a service container |
docker compose build | Rebuild images without starting |
ποΈ Multi-Container App Pattern
A typical full-stack setup:
Frontend (React) β Backend (Node.js) β Database (PostgreSQL) :80 :4000 :5432- Each layer is its own service in
docker-compose.yml - They talk to each other using service names as hostnames
- You access them from your browser via the mapped host ports
π‘ Tips to Remember
- Use
restart: on-failureon your backend β the DB might not be ready immediately - Add
.envfiles and reference them withenv_file:to keep secrets out of your YAML - Use
volumesto persist database data β without it, data is lost when the container stops docker compose down -vis a full clean reset β use with caution in production!
π― Whatβs Next?
Now that you know Docker Compose, youβre ready to explore:
- Environment-specific configs β using
.envfiles with Compose - Docker Compose Watch β hot reload for development
- Production deployments β deploying your Compose stack to a cloud server