Lesson 5 of 24Containers
Volumes and Networking
Keep data alive across containers with volumes, and connect containers to each other by name on a network.
A real app is never one container. Your API needs a database whose data survives restarts, and the API has to find that database on the network. Those two jobs belong to volumes and networks, and they are where most "it worked yesterday" Docker bugs live.
Three ways to store data outside the writable layer
You learned that a container's writable layer is deleted with docker rm. Docker gives you three kinds of mounts that put data somewhere else.
A bind mount maps a specific host path into the container. Edit a file on your laptop and the container sees it instantly. Perfect for development.
| Bind mount | Named volume | tmpfs | |
|---|---|---|---|
| Syntax | -v ./src:/app/src | -v pgdata:/var/lib/postgresql/data | --tmpfs /app/tmp |
| Lives in | A path you choose on the host | Docker's storage area | Host RAM |
Survives docker rm | Yes | Yes | No |
| Best for | Live code in development, config files | Database files, uploads | Scratch space, sensitive temp files |
| Watch out for | File permissions, slower on macOS | Back it up; docker volume rm deletes it | Uses memory |
The -v syntax tells them apart by the left side: a path (starts with /, ./, or ~) is a bind mount; a plain name is a named volume. The longer --mount flag spells it out and is clearer in scripts: --mount type=volume,source=pgdata,target=/var/lib/postgresql/data.
Pick the right storage for each job.
Persisting a database
Run Postgres with a named volume, delete the container, and start a new one on the same volume. The data is still there.
docker volume create pgdatadocker run -d --name db \ -e POSTGRES_PASSWORD=devpass \ -v pgdata:/var/lib/postgresql/data \ postgres:17docker rm -f db # container gone...docker run -d --name db -e POSTGRES_PASSWORD=devpass \ -v pgdata:/var/lib/postgresql/data postgres:17 # ...data backdocker volume lsdocker volume inspect pgdata # shows where Docker keeps it on the hostThe target path matters: it must be the folder where the program actually writes. For the official postgres:17 image that is /var/lib/postgresql/data. Mount the wrong path and Postgres writes to the writable layer, and you lose everything on the next docker rm.
-v pgdata:/var/lib/postgresql/data. To apply a patch release, you docker rm -f db and docker run the newer postgres:17 image with the same -v flag. What happens to your tables?Networks: containers finding each other
Each container has its own network namespace and IP address. Those IPs change every time a container is recreated, so you never hard-code them. Instead, put containers on a user-defined bridge network. Docker runs a small DNS server on it, so each container can reach the others by name.
docker network create appnetdocker run -d --name db --network appnet -e POSTGRES_PASSWORD=devpass postgres:17docker run -d --name cache --network appnet redis:7-alpinedocker run -d --name api --network appnet -p 8000:8000 \ -e DATABASE_URL=postgresql://postgres:devpass@db:5432/postgres \ -e REDIS_URL=redis://cache:6379 \ myapi:1.0Look at the connection strings: the host is db and cache, the container names.
Only api publishes a port, so it is the only container your browser can reach.
Here is a model of that DNS lookup. Names only resolve between containers on the same network.
Press Run (⌘ Enter) to see what happens.What does this print?
networks = {"front": {"web": "10.0.1.2", "api": "10.0.1.3"},
"back": {"api": "10.0.2.3", "db": "10.0.2.4"}}
def resolve(src, name):
for members in networks.values():
if src in members and name in members:
return members[name]
return "fail"
print(resolve("web", "api"))
print(resolve("web", "db"))
print(resolve("api", "db"))Published ports vs internal ports
Two different things are easy to confuse:
- An internal port is the port a process listens on inside its container. Other containers on the same network use it directly:
db:5432. - A published port (
-p) opens a port on the host and forwards it in. Only needed for traffic from outside Docker: your browser,psqlon your laptop, the internet.
Publishing a database to the world is a classic leak. If you want to use a GUI client locally, bind to loopback only: -p 127.0.0.1:5432:5432. On Linux, Docker writes its own firewall rules for published ports, which can bypass tools like ufw, so an unintended -p 5432:5432 on a server is exposed to the internet.
api container, the app connects to postgresql://...@localhost:5432/... and gets connection refused. Both containers are on appnet. Why?-p flag for the app to work?Reaching the host, and host networking
Sometimes a container needs something running on your laptop, like a local model server on port 11434. Docker Desktop provides the name host.docker.internal for that. On Linux, add --add-host=host.docker.internal:host-gateway to get the same name.
With --network host, a container skips network isolation and shares the host's network stack: no -p needed, and the app's port 8000 is the host's port 8000. It is fastest, but you lose isolation and port conflicts come back. It is mainly a Linux feature and you will rarely need it.
Order the steps to run an API that talks to Postgres by name.
- Tap the steps below in the right order.
- bind mount
- A host path mapped into a container. Changes show up on both sides instantly.
- named volume
- Docker-managed storage referenced by name. Outlives containers. Use it for databases.
- tmpfs
- A mount backed by host memory that disappears when the container stops.
- user-defined bridge
- A network you create with
docker network create. Containers on it resolve each other by name. - published port
- A host port forwarded into a container with
-p. Only needed for traffic from outside Docker. - host.docker.internal
- A hostname that lets a container reach services running on the host machine.