Skip to content

πŸ“‹ 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:

Terminal window
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

ConceptWhat it means
ServiceOne container defined in docker-compose.yml
NetworkAll services share a network automatically
Service name as hostnameServices reach each other by name (e.g. db, backend)
depends_onControls startup order
Named volumesPersist data across container restarts/removals
buildBuild image from a local Dockerfile
imagePull a pre-built image from Docker Hub

πŸš€ Essential Commands

CommandWhat it does
docker compose upStart all services
docker compose up --buildRebuild images then start
docker compose up -dStart in background (detached mode)
docker compose downStop and remove containers
docker compose down -vAlso remove named volumes
docker compose psList running services
docker compose logs -fFollow live logs
docker compose exec <svc> shOpen a shell in a service container
docker compose buildRebuild 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-failure on your backend β€” the DB might not be ready immediately
  • Add .env files and reference them with env_file: to keep secrets out of your YAML
  • Use volumes to persist database data β€” without it, data is lost when the container stops
  • docker compose down -v is 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 .env files with Compose
  • Docker Compose Watch β€” hot reload for development
  • Production deployments β€” deploying your Compose stack to a cloud server