Container Networking
Intermediate
Overview
By default Docker creates a bridge network. Containers on the same user-defined network can reach each other by container name as a hostname.
Publishing a port with -p host:container makes a container reachable from outside. Without it, the service is only available inside Docker's network.
Create a network with docker network create and attach containers with --network for clean service-to-service communication.
Cheatsheet
docker network create app-netCreate a networkdocker network lsList networks--network app-netAttach a container to it-p 8080:80Publish container port 80 as host 8080docker network inspect app-netInspect a networkTry it
A safe, simulated terminal. Run the suggested commands to see typical output.
simulated terminal
Type a command and press Enter, or click a suggestion below to run it.
Quick quiz
1. How do containers on the same user-defined network reach each other?
2. What does -p 8080:80 do?