Sub topic 13
Running the app and a database with Docker Compose
Docker Compose describes several containers in one YAML file and starts them with one command. This chapter assumes you can build an image from a Dockerfile, publish a port with docker run, and read container logs. It also assumes you know that a volume keeps data outside a container's lifetime. The Compose plugin ships with Docker Desktop and current Docker Engine installs, so the command is docker compose.
Why Compose
Running a web app and a database by hand takes a network, a volume, several environment variables, and a start order. Typing that as separate docker run commands is error-prone. Compose records it in a file you can commit and rerun.
The Compose file
Save this as compose.yaml next to your Dockerfile.
services:
web:
build: .
ports:
- "3000:3000"
environment:
DATABASE_URL: postgres://app:secret@db:5432/app
depends_on:
db:
condition: service_healthy
db:
image: postgres:16
environment:
POSTGRES_USER: app
POSTGRES_PASSWORD: secret
POSTGRES_DB: app
volumes:
- dbdata:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U app -d app"]
interval: 5s
timeout: 3s
retries: 5
volumes:
dbdata:Each key under services becomes a container. Four parts of the file matter most.
build: .builds thewebimage from the Dockerfile in the current directory. Thedbservice uses a ready-made image instead.- Networking. Compose creates a default network for the project and attaches both services to it. Each service name is a DNS hostname on that network, so the app connects to
db:5432. Inside thewebcontainer,localhostmeans the web container itself, so a connection string using it fails. - Volume.
dbdata:/var/lib/postgresql/datamounts a named volume at the directory where Postgres stores its files. The top-levelvolumes:key declares it. The data survives when the containers are removed. - Start order. A plain
depends_ononly waits for the container to start, not for Postgres to accept connections. Thehealthcheckrunspg_isreadyevery 5 seconds.condition: service_healthyholdswebback until that check passes.
Running it
docker compose up --build -d
docker compose ps
docker compose logs -f web
docker compose downup --build -d rebuilds the web image and starts everything in the background. ps lists the services and their health. logs -f web follows one service's output. down removes the containers and the network but keeps the dbdata volume.
Common errors
- Connection refused to
localhost:5432. The app is usinglocalhostinstead of the service namedb. - Changed
POSTGRES_PASSWORD, but the old one still works. Postgres reads these variables only when it initializes an empty data directory. To start fresh, rundocker compose down -v. The-vflag deletes the volume and all its data. webexits at startup even withdepends_on. Checkdocker compose psto see whetherdbis reporting healthy.- Port already allocated. Another process on the host is using port 3000. Change the left side of the mapping, for example
"3001:3000".
Exercise
You run docker compose down and then docker compose up. Is your database data still there? What command would remove it, and why would that be needed to change POSTGRES_PASSWORD?
Show answer
Yes, the data is still there, because down keeps named volumes. docker compose down -v deletes the dbdata volume. It is needed because Postgres applies POSTGRESUSER and POSTGRESPASSWORD only when it creates a new, empty data directory.