Docker Networking Basics
Docker networking connects containers to each other and to the outside. The bridge network is the default, container DNS resolves names, and port mapping opens a door to the host.
List the networks
This command lists the networks Docker manages.
docker network ls --format "{{.Name}} {{.Driver}} {{.Scope}}"See the bridge network
This command shows the bridge network's scope and driver.
docker network inspect --format "{{.Name}} {{.Driver}} {{.Scope}}" bridgeExample
docker network create g5-net
docker run -d --name g5-web --network g5-net nginx:1.27-alpine >/dev/null
docker network inspect --format "{{.Name}} {{.Driver}}" g5-netKey points
- Bridge network: the default network, which connects containers on one machine.
- Container DNS: containers on one user network reach each other by name.
- Port mapping with -p opens one container port on the host.
- A container gets its own IP address on its network.
- Use docker network ls to list networks, and docker network inspect to see one.
In this example, a user-defined network is created and nginx joins it. The inspect output shows the network name and its bridge driver. The network is the road the containers will use to reach each other by name.
Note
Containers on the default bridge cannot reach each other by name. A user-defined network fixes that.
Try it Yourself
Edit the code, then press Run.
Running is simulated · Rust Playground
Exercise
What does container DNS do on a user-defined network?