DOCKER · DOCKER FROM SCRATCH · CHAPTER 7

Networking in Docker

How containers talk to each other and to the outside world, and how they find each other by name.

6 min readCourse: Docker from ScratchLevel: beginner

From point A to point B

Point A: you publish ports with -p and Compose connects your services for you, but you do not know which network each container uses or why some find each other by name and others do not. Point B: by the end of this chapter you will tell the bridge, host and none networks apart, create your own networks, understand how Docker resolves names and troubleshoot connectivity problems between containers.

The three networks Docker creates at install time

As soon as you install Docker, you already have three networks:

docker network ls
# NETWORK ID     NAME      DRIVER    SCOPE
# 3f1c2a9b7d10   bridge    bridge    local
# 8e44d0c51a2f   host      host      local
# c09b7e6f3a55   none      null      local

Each one answers a different question: does the container get its own isolated network, share the host's network or have no network at all?

Docker network drivers: bridge, host and none With bridge, containers get private IPs on the docker0 bridge and are published through NAT with -p. With host, the container listens directly on the host interfaces with no NAT. With none, the container only has the loopback interface and no path out. BRIDGEdefault HOST--network host NONE--network none eth0 · host 203.0.113.5 docker0 · 172.17.0.1 web172.17.0.2 api172.17.0.3 veth NAT · -p weblistens on host port 80 no NAT, no -p eth0 · host 203.0.113.5 job lo · 127.0.0.1 eth0 · host 203.0.113.5 no path out

Bridge isolates and needs -p; host shares the host's network; none leaves only the loopback interface. Diagram: The House of Routing.

Bridge: the default network

If you specify nothing, the container connects to the bridge network. Docker creates a virtual bridge on the host, docker0, and gives each container a private IP from the 172.17.0.0/16 range. Each container joins the bridge through a pair of virtual interfaces (veth): one end inside the container and the other on the bridge.

docker run -d --name web nginx:stable
docker inspect -f '{{.NetworkSettings.Networks.bridge.IPAddress}}' web
# 172.17.0.2

ip addr show docker0     # on the Linux host: 172.17.0.1, the gateway
  • Containers on this network reach each other by IP.
  • Nobody outside the host can reach them unless you publish a port with -p, as you saw in chapter 3.
  • To get out to the internet, Docker translates their addresses to the host's IP (NAT).

Host and none: no isolation or no network

host: the host's network, with no layer in between

docker run -d --name web-host --network host nginx:stable
curl http://localhost:80      # nginx answers directly on port 80 of the host

The container uses the host's interfaces as if it were just another process. There is no NAT and no -p: if the application listens on 80, it takes port 80 on the host. The consequence: you cannot have two containers in host mode listening on the same port. You use it when network performance matters or the application needs to see the real interfaces.

none: no network

docker run --rm --network none busybox:1.37 ip addr
# only lo shows up: 127.0.0.1

The container only has the loopback interface. It cannot reach other containers, the host or the internet. Useful for jobs that process files and must have no network access at all.

Good to know. In Docker Desktop (Mac and Windows), host mode exists since version 4.34, but you have to turn it on in the settings and it works with limitations, because the real "host" of the containers is a Linux virtual machine. You will see this in chapter 9.

User-defined networks

The default bridge network puts every container together and does not resolve names. For each application, it is better to create a network of its own:

docker network create --driver bridge \
  --subnet 10.50.0.0/24 --gateway 10.50.0.1 \
  app-net

docker network ls
docker run -d --name api --network app-net nginx:stable

The bridge driver is the default, so docker network create app-net is enough. Set the subnet only if you need a specific range: always use private ranges (10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16) that do not clash with your local network.

Advantages over the default network:

  • Isolation: containers on app-net do not see those on other networks.
  • Name resolution: they call each other by name, without --link.
  • Hot connection: you can connect and disconnect containers without recreating them.
Connecting to what you know. This is exactly what Compose does for you: it creates a <project>_default network and connects every service to it. The front-end and back-end networks in chapter 5 were user-defined networks.

The embedded DNS: calling containers by name

Container IPs change every time containers are recreated, so your application should not depend on them. On user-defined networks, Docker includes an embedded DNS server that listens inside each container on 127.0.0.11 and answers with each container's current IP when asked for its name.

Embedded DNS in user-defined networks In a user-defined network, the api container asks the embedded DNS server at 127.0.0.11 for db, gets its current IP 10.50.0.3 and connects to it. On the default bridge network there is no name resolution and containers reach each other by IP only. USER-DEFINED NETWORK · red-app api10.50.0.2DB_HOST=db db10.50.0.3 embedded DNS · 127.0.0.11db → 10.50.0.3 1 · who is db? 2 · 10.50.0.3 3 DEFAULT BRIDGE web172.17.0.2 no name resolutionreach others by IP only

On your own network, "db" resolves to the container's current IP; on the default bridge network, it does not. Diagram: The House of Routing.

docker run -d --name db --network app-net -e POSTGRES_PASSWORD=changeme postgres:17-alpine
docker run --rm --network app-net busybox:1.37 nslookup db
# Server:    127.0.0.11
# Name:      db
# Address:   10.50.0.3

# On the default network, the same attempt fails
docker run --rm busybox:1.37 nslookup web
# ** server can't find web

That is how an application can use DB_HOST=db without knowing any IP. Underneath, Docker still uses what you saw in chapter 6: one network namespace per container and veth pairs to join them to the bridge.

Inspecting and connecting networks

# Subnet, gateway and connected containers
docker network inspect app-net

# A container's networks and IPs
docker inspect -f '{{json .NetworkSettings.Networks}}' api

# Connect an existing container to another network (it stays on both)
docker network connect app-net web
docker network disconnect bridge web

# Remove a network with no containers, or every unused one
docker network rm app-net
docker network prune

Lab: WordPress and MySQL on their own network

Two containers that talk by name, on an isolated network, with only WordPress published to the outside.

docker network create wp-net

docker run -d --name mysql-db --network wp-net \
  -e MYSQL_ROOT_PASSWORD=changeme \
  -e MYSQL_DATABASE=wordpress -e MYSQL_USER=wp -e MYSQL_PASSWORD=wp-changeme \
  -v wp-db:/var/lib/mysql \
  mysql:8.4

docker run -d --name wordpress --network wp-net -p 8080:80 \
  -e WORDPRESS_DB_HOST=mysql-db \
  -e WORDPRESS_DB_USER=wp -e WORDPRESS_DB_PASSWORD=wp-changeme \
  -e WORDPRESS_DB_NAME=wordpress \
  wordpress:6

Open http://localhost:8080: if the WordPress installer shows up, it found the database by its name, mysql-db. Then check:

docker network inspect wp-net -f '{{range .Containers}}{{.Name}} {{.IPv4Address}}{{"\n"}}{{end}}'
docker port mysql-db          # empty: the database is not published to the outside

# Clean-up
docker rm -f wordpress mysql-db && docker volume rm wp-db && docker network rm wp-net
An update compared with older courses. Classic labs combine --network with --link mysql-db:mysql-db. On a user-defined network, --link is unnecessary: the embedded DNS already resolves the name. It is a legacy option and you should not use it.

Networking cheat sheet

NetworkIsolationAccess from outsideNames
bridge (default)Shared private networkWith -pNo, IPs only
Your own network (docker network create)Only with its own networkWith -pYes, embedded DNS
hostNone at the network levelDirect, without -pThe host's
noneTotalNoneNo network

Challenge

Answer before you open the solution. Getting it wrong here is the cheapest way to learn.

1. Two containers are on the default bridge network. Can one run ping web to reach the other?

No. The default network has no name resolution. They can reach each other by IP.

2. You launch two nginx containers with --network host. What happens to the second one?

It cannot listen on port 80, because the first one already holds it on the host. In host mode there is no port mapping.

3. What address does the DNS server that answers inside a container on your own network have?

127.0.0.11, Docker's embedded DNS.

4. A job processes confidential files and must not be able to send anything over the network. Which network do you use?

--network none. The container only has the loopback interface.

5. Your web container is already running and you need it to talk to db, which is on app-net. Do you have to recreate it?

No. docker network connect app-net web connects it on the fly.

What you take away

  • Docker comes with three networks: bridge, host and none.
  • On bridge, each container has a private IP and is published with -p.
  • host shares the host's network; none leaves the container with no network.
  • Create one network per application: it isolates and gives you name resolution.
  • The embedded DNS (127.0.0.11) makes --link unnecessary.
  • docker network inspect, connect and disconnect to troubleshoot and adjust.

In the next chapter: image registries, public and private, including your own.

Official resources

Source and credits. Chapter written from Jesús Espinoza's study notes on KodeKloud's Docker for Absolute Beginners course (December 2024). Text drafted with AI assistance; original diagrams by The House of Routing. Technical details were checked against the official Docker documentation.

Navigate