Sub topic 12
Connecting containers with user-defined networks
This chapter assumes you can run containers with docker run, publish ports with -p, and know what a hostname and an IP address are. Docker Engine or Docker Desktop must be installed.
Why a user-defined network
Publishing a port exposes a container to the host. Containers also need to talk to each other, and they should not depend on IP addresses that change each time a container restarts.
A user-defined bridge network solves this. Containers attached to the same network can reach each other by container name, because Docker runs an embedded DNS server for them. The default bridge network does not provide this name lookup.
Create a network and attach containers
Create the network, start a Redis container on it, then run a second container that connects to the first by name.
docker network create appnet
docker run -d --name cache --network appnet redis:7
docker run --rm --network appnet redis:7 redis-cli -h cache pingThe last command prints PONG. The name cache resolved to the first container's IP address on appnet. The cache container has no published port, and it did not need one.
Inspect and manage networks
Use these commands to see what exists and who is connected.
docker network ls
docker network inspect appnet
docker network connect appnet web
docker network disconnect appnet webdocker network inspect lists each attached container with its IP address. connect and disconnect change membership of a running container, so you can attach an existing container without recreating it. A container can be on several networks at once.
Networks and published ports
Published ports and networks serve different readers.
| Need | Use |
|---|---|
| Reach a container from the host or outside | `-p host:container` |
| Reach a container from another container | A shared user-defined network and the container name |
Containers on the same network can use any port the service listens on, published or not. Inside the network, use the container port (cache:6379), not a host port.
Containers on different user-defined networks cannot reach each other. This makes networks a simple way to isolate groups of containers.
Common errors
could not translate host nameorName or service not known. The containers are not on the same network, or one is on the defaultbridge. Check withdocker network inspect.- Connecting to a host port from another container. Use the container's own port and its name.
localhostinside a container refers to that container itself. network appnet has active endpointswhen removing. Stop or disconnect the containers first, then rundocker network rm appnet.- Name collision. Container names must be unique. Reusing
cachefails until the old container is removed.
Clean up
docker stop cache
docker rm cache
docker network rm appnetdocker network prune removes all networks that have no containers attached.
Exercise
You run docker run -d --name api --network appnet myapi and want a second container to call it. The service listens on port 3000 inside api, and no -p flag was used. What host name and port should the second container use, and what must be true about the second container?
Show answer
Use api:3000. The second container must be attached to the same network, appnet, for example with --network appnet. No published port is needed for container-to-container traffic.
Exercise
A container started without --network cannot resolve the name cache, which runs on appnet. Which command fixes this without recreating the container?
Show answer
docker network connect appnet <container-name> attaches the running container to appnet, after which it can resolve cache.