Docker Compose basics
Real applications have several parts β a web app, a database, maybe a cache. Docker Compose describes them all in one file.
services: web: build: . ports: - "8000:8000" environment: DATABASE_URL: postgres://app:app@db:5432/app depends_on: db: condition: service_healthy
db: image: postgres:17 environment: POSTGRES_USER: app POSTGRES_PASSWORD: app POSTGRES_DB: app volumes: - pgdata:/var/lib/postgresql/data healthcheck: test: ["CMD-SHELL", "pg_isready -U app"] interval: 5s retries: 5
volumes: pgdata:docker compose up -d # build and start everythingdocker compose ps # status of each servicedocker compose logs -f webdocker compose down # stop and remove containers (volumes are kept)What Compose gives you
Section titled βWhat Compose gives youβ- A private network where each service is reachable by its name β
webtalks todb:5432, notlocalhost. - Startup order with
depends_on, and withcondition: service_healthyit waits until the database actually accepts connections. - Named volumes so data survives
downandup.