Understanding Docker Networking
0 views
Understanding Docker Networking
How Docker connects containers and the host: default bridge, custom networks, and DNS.
Default bridge
Every container can join the default bridge network. Containers get an IP and can ping each other by IP, but not by container name.
docker run -d --name web nginx
docker run -d --name app myapp
docker exec app ping web # fails by name on default bridge
docker network inspect bridge
User-defined bridge (recommended)
Create a network and attach containers. They can reach each other by name (Docker's built-in DNS).
docker network create mynet
docker run -d --name web --network mynet nginx
docker run -d --name app --network mynet myapp
docker exec app ping web # works
Exposing ports
-p 8080:80: host port 8080 → container port 80.-P: publish allEXPOSEports to random host ports.
In Docker Compose, services on the same network use the service name as hostname (e.g. http://db:5432).