5 min read

Docker - Networks and Volumes in Practice

1. Recap: From Well-Built Images to Containers That Talk to Each Other

The previous articles in this series covered how to build efficient images and run isolated containers. But a real application is rarely a single container: usually there’s an API, a database, a cache, maybe a message queue — each in its own container, needing to communicate. And containers are ephemeral by default: any data written inside them disappears when they’re removed. This article covers the two pieces that solve this: networks (communication between containers) and volumes (data persistence).

2. The Problem of Network Isolation by Default

Each container gets its own network namespace, isolated from the others and from the host. This is a security feature, not a bug — but it means that two containers started independently can’t automatically find each other:

docker run -d --name api my-api
docker run -d --name db postgres

From inside the api container, trying to reach db by that name simply fails — each isolated container only sees localhost as itself. Docker’s solution is to create a network and connect both containers to it.

3. User-Defined Networks

docker network create my-network

docker run -d --name db --network my-network postgres
docker run -d --name api --network my-network my-api

From here on, inside the api container, the hostname db automatically resolves to the IP of the db container — Docker runs an internal DNS for every user-defined network, resolving containers by name (or by the alias set with --network-alias, if there’s more than one). That’s why connection strings in containerized applications usually use the service name instead of a fixed IP:

DATABASE_URL=postgresql://user:password@db:5432/mydb

Useful commands for inspecting networks:

docker network ls                  # list all networks
docker network inspect my-network  # details: connected containers, subnet, gateway
docker network connect my-network other-container   # connect a running container
docker network disconnect my-network other-container

4. The Most Common Network Drivers

  • bridge (default): each container gets an internal IP on an isolated private subnet; it’s the driver used in the examples above and the right one for the vast majority of single-machine cases.
  • host: the container shares the host’s network stack directly, with no isolation — used when the extra latency of bridge NAT is unacceptable, at the cost of losing port isolation.
  • none: disables networking completely — useful, for security, for containers that only process local data and should have no external access at all.
  • overlay: connects containers running on different hosts, used in multi-machine clusters (Docker Swarm, and a concept similar to what Kubernetes solves in its own way) — outside the scope of a single machine, but good to know it exists.

For most projects running on a single machine — the case covered by this series — bridge with user-defined networks is almost always the right answer.

5. Volumes: Persisting Data Beyond the Container’s Life

By default, everything a process writes inside a container lives in its writable layer — and that layer is destroyed along with the container when it’s removed (docker rm). For a database, that’s unacceptable: losing all the data on every docker rm db is not an option. The solution is volumes, which exist outside the lifecycle of any specific container.

docker volume create db-data

docker run -d --name db \
  -v db-data:/var/lib/postgresql/data \
  postgres

Even if you remove and recreate the db container, the data in db-data survives — a new container pointing at the same volume sees exactly the same data as before:

docker stop db && docker rm db
docker run -d --name db -v db-data:/var/lib/postgresql/data postgres
# the previous data is still there

Management commands:

docker volume ls                # list volumes
docker volume inspect db-data   # where the volume lives on the host, among other metadata
docker volume rm db-data        # remove (fails if a container still uses it)
docker volume prune             # remove every volume not used by any container

6. Named Volumes vs Bind Mounts

There are two ways to “mount” something from the host inside a container, and the difference matters:

  • Named volume (-v db-data:/path): managed entirely by Docker, which decides where on the host the data is actually stored. It’s the right option for data an application produces and consumes (databases, queues, persistent caches) — portable across environments, because it doesn’t depend on a specific host path.
  • Bind mount (-v /path/on/host:/path/in/container): points directly at an existing path on the host filesystem. It’s the right option for local development — mounting the project’s source code inside the container to see changes reflected without rebuilding the image on every edit:
# Development: host code reflected live inside the container
docker run -d -v $(pwd)/src:/app/src -p 3000:3000 my-app-dev

Bind mounts are not recommended for production data because they tie the container to a specific host path, which breaks the portability Docker exists to guarantee.

7. A Complete Scenario: API + Database Talking with Persistent Data

Putting network and volume together in the same example — an API and a Postgres, isolated on their own network, with data that survives restarts:

docker network create app-network
docker volume create pg-data

docker run -d --name db \
  --network app-network \
  -v pg-data:/var/lib/postgresql/data \
  -e POSTGRES_PASSWORD=secret \
  postgres:16

docker run -d --name api \
  --network app-network \
  -p 8000:8000 \
  -e DATABASE_URL=postgresql://postgres:secret@db:5432/postgres \
  my-api:1.0

This is exactly the kind of setup that, in the next article, Docker Compose replaces with a single declarative file — instead of two long docker run commands to remember (and type) every time.

8. Conclusion and Next Steps

User-defined networks solve communication between containers through internal DNS by name, and volumes solve data persistence beyond the lifecycle of any individual container — the two missing pieces for modeling a real application with multiple services. In the next article, Docker Compose comes into play to describe all of this — network, volumes and multiple containers — in a single declarative file, versioned alongside the code.


References:

  1. Docker Documentation — Networking Overview
  2. Docker Documentation — Volumes