---
title: "Container-to-Container Communication on User-Defined Networks"
date: "2026-11-19T08:00:00+07:00"
description: "How containers reach each other over a user-defined bridge network: veth pairs, bridge forwarding, embedded DNS, aliases, and a debugging checklist for the real fault."
author: "AI Assistant"
image: "https://blog.redlinesoft.net/images/post/docker-container-to-container-user-defined-networks.jpg"
alt: "Diagram showing two containers connected through veth pairs to a Docker bridge network"
tags: ['docker', 'networking', 'dns', 'containers', 'debugging']
---

Put two containers on the same user-defined network and `wget http://web1` just works. Do the same on the default bridge and you get `ping: bad address 'alpine-nonexistent'`. Nothing about the containers changed — only the network they joined. Container-to-container communication looks like magic until you know exactly which four mechanisms carry it: a **veth pair**, a **Linux bridge**, an **embedded DNS server**, and a couple of firewall rules. Once you can name each one, every "container can't reach container" bug becomes a five-minute diagnosis.

## The path a packet actually takes

Start a web container on a user-defined network and the plumbing appears immediately:

```bash
docker network create nat-net
docker run -d --name web1 --network nat-net nginx:alpine
docker exec web1 ip -4 addr show eth0
```

```text
2: eth0@if13: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP
    inet 172.29.0.2/16 brd 172.29.255.255 scope global eth0
```

That `eth0` is one end of a **veth pair**; the other end lives on the host, plugged into a bridge device created for `nat-net` (`br-<12 hex chars>`). When a second container on the same network sends a packet to `172.29.0.2`, the sequence is:

1. The sender's kernel routes the destination into its `eth0`.
2. The veth pair carries the frame into the host namespace, onto the bridge.
3. The bridge does plain L2 forwarding — it learned `web1`'s MAC on another port, so it forwards the frame out the peer veth.
4. `web1`'s kernel receives it on `eth0`. No NAT, no DNAT, no iptables involved: **same-network traffic is a local bridge hop**, exactly like two VMs on a virtual switch.

That's why same-network communication needs no `-p` flag and adds no measurable latency — the packets never leave the host kernel.

## Name resolution: the embedded DNS interplay

Reachability by IP is boring; reachability by *name* is where user-defined networks differ from the default bridge. Check the resolver inside a container on a user-defined network:

```bash
docker exec web1 cat /etc/resolv.conf
```

```text
nameserver 127.0.0.11
options ndots:0
# ExtServers: [host(192.168.65.7)]
```

The `127.0.0.11` server is Docker's **embedded DNS**, living inside the container's own network namespace. It knows every container and alias on the network, answers those queries itself, and forwards anything else upstream to the host's resolvers (the `ExtServers` line). The `ndots:0` option matters too: it stops the resolver from appending search suffixes to names that already look fully qualified, so short names hit the embedded server first instead of being mangled.

This is the whole story behind name-based discovery — and behind its most famous failure:

```bash
docker run --rm alpine ping -c 1 alpine-nonexistent
```

```text
ping: bad address 'alpine-nonexistent'
```

On the **default bridge**, Docker deliberately does not attach containers to the embedded DNS for name lookup. Containers there can only be reached by IP (or via the legacy `--link` mechanism). On a user-defined network the same lookup resolves instantly. The rule is simple: *same user-defined network = names work; default bridge = names don't*.

## Ports, refusals, and what "reachable" really means

A name resolving doesn't mean the service answers. Both of these were run against the same container:

```bash
docker exec web1 wget -qO- http://web1      # same container, port 80
# <!doctype html><title>nginx</title>... (works)

docker exec web1 wget -qO- http://web1:8080
```

```text
Connecting to web1 (172.29.0.2:8080)
wget: can't connect to remote host: Connection refused
```

`web1:8080` resolved correctly — the embedded DNS did its job — but `8080` is the *published host port*, a NAT concept that only exists outside the network. Inside the network, nginx listens on `:80`. "Resolved", "connected", and "application responded" are three different checkpoints; conflating them is how debugging spirals.

The same distinction explains cross-network reachability. A container on a different network cannot resolve `web1` at all (the embedded DNS only answers for networks it's attached to), but give it the address explicitly and routing takes over:

```bash
docker run --rm --add-host hg:host-gateway alpine wget -qO- http://hg:8080
```

```text
<!doctype html><title>nginx</title>...
```

That worked because `host-gateway` resolves to the host, the published port DNATs into `web1`, and the return path is masqueraded — the full NAT path, not the bridge hop. Same destination, completely different machinery.

## Aliases and multi-network attachments

Every container joins a network with a **name** (its container name) and can carry additional **aliases**:

```bash
docker network create frontend
docker network create backend
docker run -d --name api --network frontend --network-alias api-gw --network backend nginx:alpine
docker network inspect backend --format '{{range .Containers}}{{.Name}} {{end}}'
```

```text
api
```

Aliases are the tool for stable hostnames: point clients at `api-gw` and it doesn't matter which replica currently answers. They're also the reason a container attached to two networks behaves sensibly — it gets a per-network address, and the embedded DNS answers with the address *on the network the query arrived from*. When a name resolves to a surprising IP, `docker network inspect <net>` is ground truth, not `getent hosts`.

## Debugging checklist

When container-to-container traffic fails, walk this list in order — each step eliminates one layer:

1. **Same network?** `docker network inspect <net>` and read the `Containers` section. Half of all failures end here: one container is on `network_a`, the other on Compose's default `project_default`.
2. **Name or target?** `nslookup`/`getent hosts` tests DNS separately from connectivity. Resolution working + `Connection refused` means the app isn't listening on the port you think — probably the published port instead of the container port.
3. **What's in resolv.conf?** No `127.0.0.11` means the default bridge (no name resolution) or an overridden DNS config.
4. **Listening where you expect?** `docker exec web1 ss -ltn` (or `netstat`) beats guessing; a bind to `127.0.0.1` inside the container is unreachable even from its own network.
5. **Crossing networks?** Names don't resolve across networks by design. Use an attached second network, an alias, or the IP — and remember published ports only exist on the host side.

## The bottom line

Container-to-container communication on a user-defined network is a veth pair, a bridge, and an embedded DNS server — no NAT, no magic. Names resolve because `127.0.0.11` answers for network-local names and forwards the rest upstream; packets flow because the bridge forwards frames locally. Learn which layer is which, and every failed `wget` tells you exactly where to look: the network membership, the resolver, the listen address, or the port.

## Further reading

- [How Docker's Embedded DNS Resolves Container Names](https://blog.redlinesoft.net/posts/docker-embedded-dns-name-resolution)
- [Docker User-Defined Bridge Networks](https://blog.redlinesoft.net/posts/docker-user-defined-bridge-networks)
- [Docker docs: Networking overview](https://docs.docker.com/engine/network/)
- [Docker docs: Legacy links](https://docs.docker.com/engine/network/links/)
