Chapter 1 · Lesson 18
Intermediate
2 min read
Create a Small Multi-Service Application
Overview
Docker Compose describes several containers and their shared configuration in a YAML file, usually named compose.yaml. YAML organises data with indentation and key/value pairs. YAML also expects consistent spaces; tabs are not indentation.
Detailed Notes
A small Compose file
yaml
services:
backend:
build: ./backend
ports:
- "8080:8080"
environment:
DATABASE_URL: postgres://app:learn@database:5432/app
depends_on:
database:
condition: service_healthy
database:
image: postgres:16
environment:
POSTGRES_DB: app
POSTGRES_USER: app
POSTGRES_PASSWORD: learn
volumes:
- database-data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U app -d app"]
interval: 5s
timeout: 3s
retries: 10
volumes:
database-data:
services:: top-level list of containers.backend:: service name and DNS name.build: ./backend: Dockerfile location.ports:: publish host 8080 to container 8080.environment:: key/value runtime variables.volumes:: named persistent volume.healthcheck:: command and retry policy.
Compose commands
bash
docker compose up
docker compose up -d
docker compose ps
docker compose logs -f backend
docker compose build
docker compose down
up creates and starts services. down removes containers and networks by default, but does not remove named volumes unless -v is supplied. logs -f follows service output.
Compose creates a shared network and persistent volume for the services.
Common Interview Questions
Finished this lesson?
0 of 27 lessons done · 0% of the course

