Module 3 · Project 03 · Dockerize a Real Application · Lesson 13 of 48
Docker Compose: app, database, networks and volumes
Run an app and its database together with one file, and understand container networking and persistent data.
In this lesson · about 25 minutes
- Write a compose.yaml with several services
- Explain how containers find each other on a network
- Keep data safe in volumes and pass settings with environment variables
One app, several containers
Real apps have parts: a frontend, an API, a database, maybe a cache. The rule is one process per container, so each part gets its own. Docker Compose describes them all in one file and starts them with one command.
# compose.yaml
services:
web:
image: nginx:1.27-alpine
ports:
- "8080:80"
volumes:
- ./nginx.conf:/etc/nginx/conf.d/default.conf:ro
depends_on:
- api
api:
build: .
environment:
DATABASE_URL: postgres://notes:${DB_PASSWORD}@db:5432/notes
depends_on:
db:
condition: service_healthy
db:
image: postgres:16-alpine
environment:
POSTGRES_USER: notes
POSTGRES_PASSWORD: ${DB_PASSWORD}
volumes:
- db-data:/var/lib/postgresql/data
healthcheck:
test: pg_isready -U notes
interval: 5s
volumes:
db-data:
Networking
Compose puts all services on one private network. Each service is reachable by its name: the API connects to db:5432, not an IP address. Only web publishes a port to your machine; the database is not reachable from outside at all, which is exactly what you want.
Volumes
A container's own files vanish when it's removed. A named volume (db-data) lives outside the containers, so the database survives restarts, upgrades and docker compose down. Only down -v deletes volumes, so be careful with it.
Settings and secrets
${DB_PASSWORD} is read from your shell or a .env file next to compose.yaml. Keep that .env out of Git. The same image can then run in dev, staging and production with different settings, the twelve-factor way.
Try it · watch it run
A three-container app with Docker Compose
Bring the stack up and watch the start order. Save a few notes, kill the database, then compare down with down -v.
network notes_default (services find each other by name)
web
nginx · port 8080:80
not created
api
node · depends_on db
not created
db
postgres:16 · healthcheck
not created
$ cat compose.yaml (see the lesson for the file)
Whiteboard · watch and try
How a request travels through the stack
Press Play to follow one request.
Tip: press Pause, then use the pen to work along on the board. Space plays and pauses; Ctrl+Z undoes your last mark.
Check your understanding0 of 3 correct
Key terms
Say the meaning to yourself, then tap a card to check.
Quiz score: 0 of 3
Written by Noviq Academy in its own words. The labs are simulations: no real servers are touched. When you build the projects yourself, use a practice virtual machine or a free-tier cloud account you control, watch your cloud spending, and never run cleanup or delete commands on a machine you care about. Tool names belong to their owners; versions and output are shown for learning and may differ slightly from yours. Read-aloud and the whiteboard voice use your device's built-in voices.