Docker Volumes, Networking and Compose: Running Multi-Container Applications

DevOps Certification 22 min min read Updated: Nov 17, 2026 Intermediate
Docker Volumes, Networking and Compose: Running Multi-Container Applications
Intermediate Topic 3 of 4

Storage Options

TypeCommandBest for
Volume-v pgdata:/var/lib/postgresql/dataDatabases and persistent data, managed by Docker
Bind mount-v $(pwd)/src:/app/srcLocal development: edit code and see changes in the container
tmpfs--tmpfs /tmpTemporary in-memory data, nothing written to disk
bash docker volume create pgdata docker volume ls docker volume inspect pgdata docker run -d -v pgdata:/var/lib/postgresql/data postgres:17 # Back up a volume to a tar file docker run --rm -v pgdata:/data -v $(pwd):/backup alpine tar czf /backup/pgdata.tgz -C /data .

Docker Networking

DriverUse
bridge (default)Containers on one host. Create your own bridge to get DNS names
hostContainer shares the host network (Linux), no port mapping
noneNo network
overlayMulti-host networks (Swarm)
bash docker network create appnet docker run -d --name db --network appnet -e POSTGRES_PASSWORD=secret postgres:17 docker run -d --name api --network appnet -e DB_HOST=db my-api:1.0 # Inside "api", the hostname "db" resolves to the database container docker exec api getent hosts db docker network inspect appnet
Key idea: On a user-defined network containers find each other by name. On the default bridge they do not. -p publishing is only needed for traffic from outside Docker, not between containers.

Why Docker Compose?

Real apps have several parts: a web server, an API, a database and a cache. Starting them by hand with long docker run commands is slow and error-prone. Docker Compose describes the whole stack in one YAML file.

A Complete compose.yaml

yaml ame: shop services: db: image: postgres:17 environment: POSTGRES_USER: shop POSTGRES_PASSWORD: ${DB_PASSWORD} POSTGRES_DB: shop volumes: - pgdata:/var/lib/postgresql/data healthcheck: test: ["CMD-SHELL", "pg_isready -U shop -d shop"] interval: 5s timeout: 3s retries: 10 restart: unless-stopped cache: image: redis:7-alpine restart: unless-stopped api: build: context: ./api environment: DATABASE_URL: postgres://shop:${DB_PASSWORD}@db:5432/shop REDIS_URL: redis://cache:6379 depends_on: db: condition: service_healthy cache: condition: service_started ports: - "8000:8000" restart: unless-stopped web: image: nginx:1.27-alpine volumes: - ./web/nginx.conf:/etc/nginx/conf.d/default.conf:ro ports: - "80:80" depends_on: - api volumes: pgdata:
bash # .env (same folder, never commit real secrets) DB_PASSWORD=change-me docker compose up -d --build # create network, volumes and start everything docker compose ps # status docker compose logs -f api # follow logs of one service docker compose exec db psql -U shop -d shop docker compose stop # stop but keep containers docker compose down # remove containers and network (keeps volumes) docker compose down -v # also remove volumes (deletes data!)

Useful Compose Features

Override files for development

yaml # compose.override.yaml is loaded automatically on top of compose.yaml services: api: volumes: - ./api:/app # live code reload environment: DEBUG: "1"

Profiles

yaml services: adminer: image: adminer profiles: ["debug"] ports: ["8080:8080"]
bash docker compose --profile debug up -d

Scaling and config check

bash docker compose up -d --scale api=3 # three API containers (remove fixed host port first) docker compose config # print the final merged file and catch errors docker compose pull && docker compose up -d # update images

Secrets and Configs

yaml services: api: secrets: - db_password secrets: db_password: file: ./secrets/db_password.txt # mounted at /run/secrets/db_password

Common Problems

ProblemFix
API starts before the database is readyUse depends_on with condition: service_healthy and a healthcheck
Cannot connect to localhost from a containerlocalhost inside a container is the container itself. Use the service name (db)
Port already in useChange the host port: "8081:80" or stop the other process (ss -tulpn)
Data lost after downUse named volumes, and avoid down -v unless you want to delete data
Changes to code not visibleRebuild with --build, or use a bind mount for development
Permission denied on a bind mountMatch the container user and file owner, or fix with chown

Practice Tasks

  1. Write a Compose file for a web app with PostgreSQL and Redis, and bring it up with one command
  2. Add a healthcheck and make the app wait for it
  3. Use an override file for development with live reload
  4. Back up and restore the database volume
  5. Run three replicas of the API behind nginx

Conclusion

Volumes keep data, user-defined networks give containers DNS names, and Compose turns a long list of docker run commands into one reviewable file for the whole stack.

Quick Summary: volumes for data; user network = DNS by name; compose.yaml; healthcheck + depends_on; down -v deletes data.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators