Thuta Learning
ExercisesDevOps & Toolsintermediate

Docker Practice: Compose, Volume & Network

Relax. We'll talk through this in plain words — no textbook voice.

What you'll walk away with

  • Practice Docker: Compose, Volume & Network hands-on yourself
  • Practice what you've learned so far and make those skills solid
  • Learn to find mistakes, fix them, and check your own work

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

yaml
# 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:
You should see
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.

Easy traps

  • Forgetting to mount a volume in the compose file, so the database data disappears completely every time the container restarts
  • Getting a connection refused error because you used localhost instead of the service name (db) when connecting services to each other

Now Try It Yourself

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.

You'll know it worked when: 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.

Docker Practice: Compose, Volume & Network | Thuta Learning