1/3
What is Docker Compose?
+10 XP on completion
#What is Docker Compose?
After this lesson you'll know:
- why you need docker compose
- how a docker-compose.yml is structured
- the most important compose commands
#The problem with docker run
Your app needs a database. Maybe Redis. A message broker.
Sure, you could type three docker run commands. Every time. In the right order. With the right networks.
Or you define everything in one file.
#docker-compose.yml
services:
web:
image: nginx
ports:
- "8080:80"
db:
image: postgres:16-alpine
environment:
POSTGRES_PASSWORD: secret
POSTGRES_DB: myapp
docker compose up -d # Start everything
docker compose down # Stop everything
#Key Concepts
- Services = your containers
- Networks = how services communicate (automatically created!)
- Volumes = persistent data
- Environment = environment variables
Services in the same Compose file are automatically on the same network. You reach db simply as db (hostname = service name).
#Important Commands
docker compose up -d # Start
docker compose down # Stop + remove network
docker compose ps # Status
docker compose logs -f # Follow logs
docker compose exec web sh # Shell into container
docker compose build # Build images (when using build: .)
#✋ Try it out
- Create a docker-compose.yml with a
webservice (nginx) and start it withdocker compose up -d. Open http://localhost:80 - Add a
dbservice (postgres), restart, check withdocker compose ps docker compose logs -f— follow the logs of both services
#📌 Summary
- docker compose defines all services in one YAML file
- One command starts/stops everything
- Services automatically find each other by name
← → to navigate