Networking Basics
Different Snack Cupboards
Imagine you’re hosting a party. Each guest (container) is in their own room, but they need hallways (networks) to talk to each other and doors (ports) to connect with the outside world. Without proper networking, containers remain isolated and can’t collaborate.
Docker provides a powerful, flexible networking model that makes it easy to connect containers together, expose services, and control communication.
Networking Foundations
1. Why Networking Matters
- Containers often run microservices that must communicate.
- Networking allows containers to talk to each other and external clients.
- Docker abstracts networking so developers don’t need to configure complex setups manually.
2. Default Networking Modes
- Bridge Network (default):
- Containers connect to a private internal network.
- They can communicate with each other using IP addresses or container names.
- External access requires port mapping.
- Host Network:
- Containers share the host’s network stack.
- No isolation; useful for performance‑sensitive applications.
- None Network:
- Containers have no network connectivity.
- Useful for security or testing isolation.
3. Port Mapping
- Containers don’t expose ports to the outside world by default.
- Use
-p host_port:container_portto map ports.- Maps port 80 inside the container to port 8080 on the host.
docker run -d -p 8080:80 nginx
4. Container Name Resolution
- Docker provides built‑in DNS for containers.
- Containers in the same network can communicate using names instead of IPs.
- Example:
ping mydbworks ifmydbis another container in the same network.
5. Creating Custom Networks
- Custom networks provide better isolation and flexibility.
- Now
webcan talk todbusing the namedb.
docker network create mynetwork
docker run -d --network=mynetwork --name web nginx
docker run -d --network=mynetwork --name db redis
Things to Remember
- Bridge is the default network mode; host and none are special cases.
- Port mapping connects containers to the outside world.
- Custom networks enable name‑based communication and isolation.
Hands‑On Lab
Step 1: Run a Container with Port Mapping
docker run -d -p 8080:80 nginx
- Access Nginx at
http://localhost:8080.
Step 2: Create a Custom Network
docker network create appnet
Step 3: Run Two Containers in the Network
docker run -d --network=appnet --name web nginx
docker run -d --network=appnet --name db redis
Step 4: Test Communication
docker exec -it web ping db
- Shows that containers can resolve each other by name.
Practice Exercise
- Create a custom network called
testnet. - Run a
mysqlcontainer and aphpmyadmincontainer intestnet. - Configure
phpmyadminto connect tomysqlusing the container name. - Expose
phpmyadminon port 8081 and test it in your browser.
Visual Learning Model
Host Machine
↓
Bridge Network
├── Container A (web) → Port 8080
└── Container B (db) → Internal only
The Hackers Notebook
Container networking allows containers to communicate with each other and the outside world. The bridge network is the default, host shares the host’s stack, and none isolates completely. Port mapping exposes services externally, while custom networks enable name‑based communication and isolation. Mastering these basics is essential for building multi‑container applications.
