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?
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.
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-netdo 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.
<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.
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
--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
| Network | Isolation | Access from outside | Names |
|---|---|---|---|
bridge (default) | Shared private network | With -p | No, IPs only |
Your own network (docker network create) | Only with its own network | With -p | Yes, embedded DNS |
host | None at the network level | Direct, without -p | The host's |
none | Total | None | No 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,hostandnone. - On
bridge, each container has a private IP and is published with-p. hostshares the host's network;noneleaves 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--linkunnecessary. docker network inspect,connectanddisconnectto troubleshoot and adjust.
In the next chapter: image registries, public and private, including your own.
Official resources
- Networking overviewdocs.docker.com
- Bridge network driverdocs.docker.com
- Host network driverdocs.docker.com
- None network driverdocs.docker.com
- docker network (CLI)docs.docker.com
- wordpress (official image)hub.docker.com
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.