Docker Compose
Define several services in one file and start them together.
Docker Compose describes a multi-container app in a compose.yaml file. One command starts everything, and services reach each other by service name.
services:
web:
build: .
ports:
- "8000:8000"
environment:
DATABASE_URL: postgres://app:change-me@db:5432/app
depends_on:
- db
db:
image: postgres:16
environment:
POSTGRES_USER: app
POSTGRES_PASSWORD: change-me
POSTGRES_DB: app
volumes:
- dbdata:/var/lib/postgresql/data
volumes:
dbdata:docker compose up -d # create and start everything in the background
docker compose ps # status of the services
docker compose logs -f web # logs of one service
docker compose down # stop and remove (add -v to also delete volumes)NoteInside the Compose network, the web service connects to the database at the host name db, not localhost.
CarefulThe passwords above are placeholders. Keep real ones in an .env file excluded from Git.
Test yourself
Answer all the questions, then check them. Finish with every answer right to mark the lesson as done.