Ravindra BagaleCourses & study guides

Chapter 7: Docker Volumes, Networks and Compose

7.4 Compose file — API + database together

In short: File name is usually compose.yaml or docker-compose.yml.

Docker Compose reads a YAML file and starts the stack as one unit. File name is usually compose.yaml or docker-compose.yml.

# compose.yaml — badge API lab stack (example)
services:
  db:
    image: postgres:16
    volumes:
      - badge-api_pgdata:/var/lib/postgresql/data
    environment:
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
    networks:
      - badge-api_net
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 5s
      timeout: 5s
      retries: 5

  api:
    build: .
    ports:
      - "8080:8080"
    environment:
      DATABASE_URL: postgres://postgres:${POSTGRES_PASSWORD}@db:5432/postgres
      APP_ENV: lab
    depends_on:
      db:
        condition: service_healthy
    networks:
      - badge-api_net

volumes:
  badge-api_pgdata:

networks:
  badge-api_net:
Key Role
services Containers Compose will manage
build / image Build from Dockerfile or pull an image
ports Publish published ports (host:container)
volumes Named volume declarations + mounts
networks Private lanes
depends_on Start order (with healthcheck = wait until ready)
environment / ${VAR} Config from shell or .env file

Service name db becomes the DNS name on the Compose network — same idea as --name badge-api-db earlier.

Compose v1 vs v2

Modern Docker uses docker compose (plugin, space). Older docs show docker-compose (hyphen). Prefer docker compose if your install supports it.