Volumes, networks & Compose in practice
Containers are meant to be thrown away, but your data isn't, and your containers need to find each other. This chapter covers the three ways to store data, how Docker networks and container DNS work, and a realistic Compose setup you can copy for your own projects.
Three kinds of mounts
| Type | Example | Lives | Use it for |
|---|---|---|---|
| Named volume | -v pgdata:/var/lib/postgresql/data | Managed by Docker (under /var/lib/docker/volumes on Linux) | Database files, anything the app owns |
| Bind mount | -v "$(pwd)":/app | A folder you choose on the host | Live-editing code in development, config files |
| tmpfs | --tmpfs /tmp | RAM only (Linux), gone on stop | Scratch files, things that must never hit disk |
Two differences worth knowing. An empty named volume is first filled with whatever the image has at that path; a bind mount simply hides it. And with -v, a host path that doesn't exist is silently created as an empty root-owned folder, while the longer --mount type=bind,src=...,dst=... syntax fails with an error instead. Add :ro to mount read-only: -v "$(pwd)/nginx.conf":/etc/nginx/nginx.conf:ro.
$ docker volume create pgdata
$ docker volume ls
$ docker volume inspect pgdata # shows the Mountpoint on the host
$ docker volume rm pgdata # only if no container uses it
Backing up a volume
Start a throwaway container that mounts the volume and your current folder, and tar one into the other:
# backup
$ docker run --rm -v pgdata:/data:ro -v "$(pwd)":/backup alpine \
tar czf /backup/pgdata.tar.gz -C /data .
# restore into a (new, empty) volume
$ docker run --rm -v pgdata:/data -v "$(pwd)":/backup alpine \
tar xzf /backup/pgdata.tar.gz -C /data
docker exec db pg_dump -U app app > app.sql.Networks and DNS by container name
Containers on the default network (called bridge) can only reach each other by IP. On a network you create, Docker runs a built-in DNS server, so containers find each other by name:
$ docker network create appnet
$ docker run -d --name db --network appnet -e POSTGRES_PASSWORD=example postgres:16
$ docker run --rm --network appnet alpine ping -c 2 db # "db" resolves
$ docker network ls # bridge, host, none + yours
$ docker network inspect appnet # subnet and the containers attached
$ docker network connect appnet web # attach an already-running container
Other drivers: host shares the host's network directly (no isolation, no -p needed; Linux only) and none gives no network at all.
Publishing ports
Containers on the same network talk to each other on the container port directly (db:5432); no publishing needed. -p is only for traffic from outside Docker:
$ docker run -d -p 8080:80 nginx:1.27 # host 8080 -> container 80, all interfaces
$ docker run -d -p 127.0.0.1:8080:80 nginx:1.27 # only reachable from this machine
$ docker port <container> # show what is published
-p 8080:80 listens on every interface, and Docker writes its own firewall rules, so the port can be reachable from outside even if ufw seems to block it. Never publish a database port to the world; bind it to 127.0.0.1 or don't publish it at all.A realistic compose.yaml
A web app and PostgreSQL, where the web container waits until the database actually accepts connections:
services:
web:
build: .
ports:
- "127.0.0.1:3000:3000"
env_file:
- web.env # vars injected into the container
environment:
DATABASE_URL: postgres://app:${POSTGRES_PASSWORD}@db:5432/app
depends_on:
db:
condition: service_healthy # wait for the healthcheck, not just "started"
restart: unless-stopped
db:
image: postgres:16
environment:
POSTGRES_USER: app
POSTGRES_DB: app
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
volumes:
- pgdata:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U app -d app"]
interval: 5s
timeout: 3s
retries: 5
start_period: 10s
volumes:
pgdata:
Things to notice:
- No
version:line. It's obsolete in current Compose (v2 and later), which only prints a warning if you include it. - Compose creates a network called
<project>_defaultautomatically, sowebreaches the database at hostdb. The database port isn't published at all. depends_onconditions areservice_started(the default),service_healthyandservice_completed_successfully. It only controls startup order; your app should still retry if the database restarts later.
Two kinds of env files
- A file named
.envnext tocompose.yamlis read by Compose itself to fill in${POSTGRES_PASSWORD}in the YAML. env_file: web.envpasses every line of that file into the container as environment variables.
# .env (keep out of git and out of the image)
POSTGRES_PASSWORD=change-me
# web.env
LOG_LEVEL=info
docker compose config prints the final file with every variable filled in, which is the quickest way to spot a missing one.
Everyday Compose commands
$ docker compose up -d --build # build if needed, start in the background
$ docker compose ps # state and health of each service
$ docker compose logs -f web # follow one service's logs
$ docker compose exec db psql -U app -d app
$ docker compose restart web
$ docker compose down # remove containers and network, KEEP volumes
$ docker compose down -v # also delete volumes: the database is wiped