Introduction
Yesterday, my Python (Flask) app talked to MySQL using DB_HOST=database, and it worked automatically. Today, I pulled back the curtain on Docker Networking: testing how containers communicate by IP vs. name, why custom bridge networks unlock automatic DNS service discovery, how network isolation protects services, and how Docker Compose automates this under the hood.
1. Research: Docker's Default Network Types
Think of Docker networking like a multi-story building: each network is a separate floor where containers on the same floor can talk freely, containers on different floors are isolated by default, and port mapping (-p) acts as the building's front door for external traffic.
Running docker network ls revealed Docker's three built-in default networks:
docker network ls
Note: You will also see a minikube network in my screenshot above. That isn't one of Docker's default networks. It's a custom bridge network created automatically by an older local Kubernetes project on my machine.
-
What are the three default networks, and what is each used for?
-
bridge: Docker's default isolated virtual network for standalone containers (docker runwithout--network). Containers on it get private internal IPs and can reach the internet through NAT. -
host: Removes network isolation completely and shares the host machine's network stack directly (localhostinside the container is the host'slocalhost). -
none: Disables all networking for a container. Useful for offline batch jobs or maximum security isolation.
-
-
Why does Docker provide different network types? Different workloads need different levels of isolation and performance. For example, multi-service apps need isolated bridge networks, high-throughput monitoring tools may need
hostnetworking, and offline data processors neednone.
2. Exploring the Default Bridge Network
I launched two Alpine containers on the default bridge network, found container1's IP address, and tested pinging it by IP vs. by container name:
docker run -d --name container1 alpine sleep 3600
docker run -d --name container2 alpine sleep 3600
docker inspect container1 --format '{{.NetworkSettings.Networks.bridge.IPAddress}}'
docker exec container2 ping -c 3 <container1-ip>
docker exec container2 ping -c 3 container1
-
What happened? Pinging
container1by its IP address succeeded with0% packet loss, while pingingcontainer1by name failed withping: bad address 'container1'. -
Why can the containers communicate using an IP address but not by container name? For legacy backwards compatibility, Docker's default
bridgenetwork does not include Docker's embedded DNS server, so containers only know each other by raw IP address.
Note: Docker v29 stores the IP under
.NetworkSettings.Networks.bridge.IPAddressinstead of the older.NetworkSettings.IPAddress
After testing, I cleaned up both containers with:
docker rm -f container1 container2
3. Creating a Custom Bridge Network
Next, I created a user-defined bridge network and verified it:
docker network create --driver bridge my-custom-network
docker network ls
-
What driver is being used?
bridge. - What is the purpose of this network? To group related containers onto their own private, isolated virtual switch with automatic service discovery.
- How is it different from the default bridge network? User-defined bridge networks include Docker's built-in DNS resolution (allowing containers to reach each other by container name) and allow live attaching/detaching of running containers.
4. Testing Container-to-Container Communication on a Custom Network
I started container1 and container2 inside my-custom-network and pinged container1 by name:
docker run -d --name container1 --network my-custom-network alpine sleep 3600
docker run -d --name container2 --network my-custom-network alpine sleep 3600
docker exec container2 ping -c 3 container1
-
Why does
container1work as a hostname this time? Becausemy-custom-networkis a user-defined bridge network with automatic name resolution enabled. -
What role does Docker's internal DNS play? Docker runs an embedded DNS server (
127.0.0.11) inside custom networks that automatically maps every container's name (and aliases) to its current internal IP address.
5. Inspecting the Custom Network
Running docker network inspect my-custom-network revealed the network's internal configuration:
docker network inspect my-custom-network
-
Network subnet:
172.18.0.0/16 -
Gateway:
172.18.0.1 -
Connected containers & IP addresses: container1 (
172.18.0.2/16) and container2 (172.18.0.3/16) - How could this information help during troubleshooting? If one container cannot reach another, inspecting the network immediately confirms whether both containers are actually attached to the same subnet, checks their assigned IPs, and verifies whether a container crashed or detached.
6. Connecting a Running Container to a Network
I launched container3 on the default network and dynamically connected it to my-custom-network without restarting it:
docker run -d --name container3 alpine sleep 3600
docker network connect my-custom-network container3
docker inspect container3 --format '{{json .NetworkSettings.Networks}}' | python3 -m json.tool
-
How many networks is
container3connected to? Two networks simultaneously (bridgeandmy-custom-network). -
What changed after running
docker network disconnect my-custom-network container3?container3lost itsmy-custom-networkinterface/IP and could no longer resolve or reachcontainer1orcontainer2, remaining connected only to the defaultbridgenetwork.
7. Exploring Network Isolation
I created a second custom network (isolated-network), launched isolated-container on it, and tried to ping it from container2:
docker network create isolated-network
docker run -d --name isolated-container --network isolated-network alpine sleep 3600
docker exec container2 ping -c 3 isolated-container
-
Why can't the containers communicate?
container2is onmy-custom-networkwhileisolated-containeris onisolated-network. Docker strictly isolates traffic between separate bridge networks by default. - How can network isolation be useful when designing an application? It enforces defense-in-depth. For example, placing a public reverse proxy on a frontend network and a database on a private backend network ensures external-facing services cannot accidentally or maliciously reach sensitive internal databases unless explicitly bridged.
8. Connecting This to Docker Compose
Looking back at yesterday's Python + MySQL docker-compose.yml (app and database services):
cd ~/Day5
docker compose up -d
docker network ls
docker network inspect day5_default
-
What network did Compose create?
day5_default(a custom bridge network). -
Which containers joined it? Both
day5-app-1andday5-database-1. -
How could your Python application use
databaseas the MySQL hostname? Because Docker Compose automatically creates a user-defined bridge network for the project and registers each service name (database) in Docker's internal DNS. -
Why didn't you need the database container's IP address? Docker's internal DNS dynamically resolves the hostname
databasetoday5-database-1's current container IP even if the container restarts.
9. Clean Up
Finally, I stopped Day5 and removed all test containers and custom networks:
docker compose down
cd ~
docker rm -f container1 container2 container3 isolated-container
docker network rm my-custom-network isolated-network
docker network ls
Summary & Key Takeaways
- What is a Docker network? A virtual software-defined network that controls how containers discover and communicate with each other, the host machine, and the outside internet.
- Default bridge vs. custom bridge: The default bridge only supports communication via IP addresses, whereas a custom bridge provides automatic DNS name resolution, better isolation, and dynamic attachment of running containers.
-
What are
hostandnonenetworks? host shares the host machine's network directly with zero isolation, while none completely disables networking for a container. -
Why does name resolution work on custom networks? Custom networks enable Docker's embedded DNS server (
127.0.0.11), which maps container and service names to their IPs automatically. -
How does Docker Compose help containers communicate? It automatically creates an isolated project bridge network (
<project>_default), attaches all services to it, and exposes every service name as a DNS hostname. - What is network isolation and why is it useful? It prevents containers on separate networks from communicating by default, protecting sensitive backend services (like databases) from unauthorized access.












Top comments (0)