Take a moment to think about this
This exercise set steps up from the basic exercises, combining three concepts — Compose, Volumes, and Networking — into one practice. There's no new teaching here; it's about applying what you learned in the compose-intro, compose-file, volumes, and networking lessons together in a single project setup. You'll define two services in one docker-compose.yml, persist database data with a volume, and hands-on test how services reach each other by service name over the network. This kind of hands-on practice makes using Compose in a real project setup much more familiar.
Exercises
Task 1 - Write your own docker-compose.yml with an app service (like nginx) and a db service (postgres), and place both services under a custom network. Task 2 - Add a named volume for the postgres service, then run docker compose down and docker compose up again to verify the data persists. Task 3 - Run docker compose exec app ping db (or curl) to confirm the services can reach each other using just the service name (db). Task 4 - Add an environment variable (e.g. POSTGRES_PASSWORD) to docker-compose.yml, and use depends_on to control the startup order.
Code Example
# docker-compose.yml
version: "3.9"
services:
app:
image: nginx
ports:
- "8080:80"
networks:
- app-net
depends_on:
- db
db:
image: postgres:16
environment:
POSTGRES_PASSWORD: example
volumes:
- db-data:/var/lib/postgresql/data
networks:
- app-net
networks:
app-net:
volumes:
db-data:After docker compose up, both the app and db services should be running, you should be able to reach db by its service name, and the db data should survive even after bringing the containers down and back up.5-Minute Try
Within 5 minutes, compare docker compose down -v (deletes volumes too) against docker compose down (keeps volumes) on your docker-compose.yml, and notice when the data disappears and when it survives.
A Quick Warning
Running docker compose down -v deletes all the volumes too, so be careful with the -v flag on projects that hold real data.