Networking, volumes and Compose

Bridge networks, port publishing, volumes for state, and Compose for a service with Postgres and Kafka beside it.

13 min read🐳 Docker

One container is a process; a service is several — the application, its database, a cache, a broker — that must find each other, keep data past a restart, and be started together the same way on every machine. Docker's networks, volumes and Compose are the three answers, and each has one rule that the first outage teaches.

Networks: containers find each other by name

Every container is on a network, and on a user-defined bridge network Docker runs a DNS server that resolves container names to addresses. postgres in a connection string is a hostname, resolved to whatever address that container has today:

plaintext
jdbc:postgresql://postgres:5432/orders        # inside the network: the container's name
jdbc:postgresql://localhost:55432/orders      # from the host: the published port

Two things follow.

Ports inside a network are not published. The database listens on 5432 and every container on the network can reach it there. The host cannot, unless -p 55432:5432 maps a host port onto it. That mapping is for the developer's psql, not for the application, which should never reach its own database through the host.

localhost inside a container is the container. An application configured with localhost:5432 is looking for a database inside itself. The Testcontainers lesson's getHost() and getMappedPort() exist for exactly this. host.docker.internal reaches the host from a container when you genuinely need to.

The default bridge network (the one containers land on with no --network) has no DNS; give every project its own network, and Compose does so for you. A container that must not talk to anything gets --network=none, which is how this repository's runner isolates a learner's submission: no DNS, no route, nothing to exfiltrate to.

Volumes: data that outlives the container

A container's writable layer is deleted with it. A database that writes into that layer loses everything when the container is removed, which is the second thing the first outage teaches. A volume is a directory Docker manages on the host, mounted into the container at a path, and kept until you delete it:

bash
docker volume create orders-pg
docker run -d --name postgres -v orders-pg:/var/lib/postgresql/data postgres:16-alpine

A bind mount (-v /home/me/project:/src) mounts a host directory instead. Use it for development, such as live-reloading sources or a config file. Never use it for a database in production: the ownership and permission mismatch between the host's uid and the container's becomes a Saturday-night problem.

The USER 10001 from the images lesson has to be able to write the mounted path. --chown on the COPY handles the image, and an init container or an entrypoint chown handles the volume.

Backups are the volume's, not the container's: pg_dump through the running database, or a docker run --volumes-from that tars the directory while the database is stopped. This repository's nightly backup runs pg_dump from a systemd timer and copies the result off the machine; the same job, in a container, is a second container on the same network with the same schedule.

Compose: the whole service in one file

compose.yamlyaml
services:
  api:
    build: .
    ports: ["8080:8080"]
    environment:
      SPRING_DATASOURCE_URL: jdbc:postgresql://postgres:5432/orders
      SPRING_DATASOURCE_USERNAME: app
      SPRING_DATASOURCE_PASSWORD: ${DB_PASSWORD}
      SPRING_PROFILES_ACTIVE: local
    depends_on:
      postgres: { condition: service_healthy }
      kafka:    { condition: service_started }
    deploy: { resources: { limits: { memory: 1g, cpus: "2" } } }
 
  postgres:
    image: postgres:16-alpine
    environment: { POSTGRES_USER: app, POSTGRES_PASSWORD: ${DB_PASSWORD}, POSTGRES_DB: orders }
    volumes: ["pgdata:/var/lib/postgresql/data"]
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U app -d orders"]
      interval: 5s
      retries: 10
 
  kafka:
    image: apache/kafka:3.9.0
    profiles: ["messaging"]
 
volumes:
  pgdata:

docker compose up builds the image, creates the network and the volume, starts the dependencies in order, and streams every container's logs. What each line is for:

  • depends_on with condition: service_healthy waits for the health check, not just the container start. Postgres accepts connections a few seconds after the process starts, and an API that connects at second one crashes.
  • ${DB_PASSWORD} comes from the shell or a .env file beside the compose file, which is gitignored exactly as the application's .env is.
  • deploy.resources.limits are the cgroup limits the last lesson's JVM will read, so the container matches what production gives it.
  • profiles keeps the broker out of docker compose up until --profile messaging asks for it, so the common case starts fast.

docker compose down stops and removes the containers and the network and keeps the volumes; adding the volumes flag to it removes those too, which is the command for "start the database from nothing" and the one to type carefully.

Secrets, and what the environment is for

Environment variables are the right place for configuration and the wrong place for credentials in production: every process in the container can read them, they appear in docker inspect, and they are copied into any child process. Compose and Swarm have a secrets: section (a file mounted at /run/secrets/<name>), Kubernetes has its Secret objects mounted the same way, and Spring Boot reads either as a property source with spring.config.import=configtree:/run/secrets/. For local development the .env file is fine; the line to hold is that the compose file itself, committed, never contains a value — only a reference.

Under the hood: the bridge, the veth pair, and what a mount actually is

"Containers find each other by name" and "a volume survives the container" both rest on real Linux mechanisms, not Docker inventions — the same primitives the kubernetes course's pod-networking lesson uses for a different purpose. Docker's are worth seeing directly, because they are what you inspect when a container "can't reach the network" or a volume "isn't where it should be."

docker0 and the veth pair: how a container gets on the network

Every container gets its own network namespace: a private set of network interfaces, routes and iptables rules, isolated from the host's. A namespace with nothing in it can reach nothing, so Docker wires each container's namespace to the host with a veth pair. That is two virtual Ethernet interfaces created as a connected unit, like a virtual patch cable: a packet sent into one end appears on the other.

One end is moved into the container's network namespace and renamed eth0 (what ip addr inside the container shows). The other end stays on the host and is attached to a Linux bridge, a kernel-level virtual switch. The bridge is named docker0 for the default bridge network, or has a different name for each user-defined network that Compose sets up.

The bridge forwards frames between every veth end attached to it, exactly the way a physical Ethernet switch forwards between ports. That is the actual mechanism behind "containers on the same network can reach each other". Their veth pairs' host-side ends sit on the same bridge, so a frame from one container's eth0 reaches the bridge, and the bridge delivers it to the veth end wired to the destination container's namespace.

Container-name resolution is a separate layer on top of that wiring. On a user-defined bridge network, the Docker daemon runs an embedded DNS server (at 127.0.0.11 inside each container's namespace, visible in /etc/resolv.conf). It answers a lookup for another container's name with that container's IP on the same network, the IP the container was assigned when its veth end was attached.

The default bridge network (docker0 itself, the one a container lands on with no --network flag) has no such DNS server. Containers there can still reach each other over the bridge by IP, but not by name. That is the concrete reason this lesson's earlier advice matters: give every project its own network. It is not a style preference. It is the difference between DNS existing and not.

A published port (-p 55432:5432) is a third mechanism again. The host adds an iptables DNAT rule (in the DOCKER chain) that rewrites a packet arriving at the host's own port 55432 to the container's IP and port 5432, before routing it across the bridge. It is the same DNAT idea the kubernetes lesson describes for a Service's ClusterIP, applied here to get a packet from outside the host's networking altogether onto the bridge in the first place.

Bind mount vs. named volume: two different things under one -v flag

Both look like the same -v host:container syntax, but they resolve completely differently once the container process actually reads a file.

A bind mount (-v /home/me/project:/src) is a Linux bind mount in the literal, kernel sense. The container's mount namespace gets an entry that says "the directory at /src is the host's /home/me/project inode". It is not a copy and it is not synced: the identical filesystem entries are visible from two paths at once, one on the host and one inside the container's mount namespace.

A write from either side is immediately visible from the other, because there is only one underlying file. The host's permission bits and ownership (uid/gid numbers) apply exactly as they do outside the container. That is the entire source of the "the container's USER 10001 can't write the mounted path" problem this lesson's main text names. Uid 10001 inside the container is uid 10001 as far as the host's filesystem permissions are concerned. If the host directory is owned by some other uid, the write is refused by the same permission check that would refuse it for a host process.

A named volume (-v orders-pg:/var/lib/postgresql/data) is different in kind, not just in syntax. Docker creates and owns a directory under its own data root (/var/lib/docker/volumes/<name>/_data with the default local driver). That directory has no meaning or path outside Docker's bookkeeping, and nothing a host process would casually find or edit. Docker mounts that into the container's mount namespace at the requested path.

Because Docker manages the backing directory itself, it can set the directory's ownership to match what the container needs at creation time. For the common case that sidesteps the bind mount's uid-mismatch problem entirely. It can also swap the backing storage driver (local, an NFS-backed driver, a cloud block-storage driver) without anything in the container's Dockerfile or run command changing, since the container only ever sees a mount point, never the mechanism behind it.

Try it yourself

Reason through a bind-mount permission failure

A Dockerfile ends with USER app where app is useradd --system --uid 10001 app (this course's images lesson). In development, the same image is run with -v $(pwd)/data:/app/data so the application can write files the developer wants to inspect on the host. The host directory ./data was created by a plain mkdir data as the developer's own user, which on their machine is uid 1000. The application inside the container tries to write a file to /app/data/output.log and gets Permission denied.

Using the bind-mount mechanism above (a bind mount is the literal host inode, not a copy), predict why the write fails. Then say what changes about the failure, if anything, if ./data were instead a Docker-managed named volume mounted at the same path.

Why the uid, not the container, is what filesystem permissions check

A bind mount does not translate or remap ownership between host and container. /app/data inside the container's mount namespace and ./data on the host are the same inode, so the same Linux permission check applies to both. The file is owned by uid 1000 (the developer's host user). The process attempting the write runs as uid 10001 (app, from the image's USER directive), with no write permission on a directory it does not own and that grants no group or other write bit.

The container boundary is irrelevant to this check. It is exactly the failure a host process running as a different uid would get trying to write into another user's directory, because that is literally what is happening.

Swapping to a named volume changes the outcome because Docker owns the backing directory. For a fresh volume it can set the ownership at creation to whatever the container's first write needs (or the image can chown the mount point during an entrypoint step). The volume's backing storage is never the developer's own ./data with the developer's own uid, so there is no pre-existing ownership to conflict with.

Misconceptions

  • "A bind mount copies files into the container." It does not copy anything — it is one filesystem entity made visible at two paths (host and container) through the container's mount namespace. A write from inside the container is a write to the real host file, immediately, with the host's own permissions applying; there is no synchronization step and no separate copy to fall out of sync.
  • "docker0 is 'the Docker network' — every network works the same way." docker0 is specifically the default bridge: DNS-less, and shared by every container that doesn't request a network. Each user-defined bridge network (what Compose creates per project) is its own separate Linux bridge with its own embedded DNS server. Containers on one bridge cannot reach containers on another, by name or by IP, unless something explicitly connects the two networks.

Sources

  • Docker documentation, "Networking overview" and "Bridge network driver" — the bridge, embedded DNS on user-defined networks, and why the default bridge lacks it.
  • Docker documentation, "Volumes" and "Bind mounts" — the mechanical difference between a Docker-managed volume and a host-path bind mount, and their ownership implications.
  • veth(4) (Linux kernel documentation) — the virtual Ethernet device pair Docker uses to connect a container's network namespace to a bridge.
Progress is saved on this device and to your account when signed in.