๐ Docker Networking A2Z โ Masterclass for Developers & DevOps
๐ฆ What is Docker Networking?
๐ง Docker networking allows containers to communicate with:
- Each other ๐
- The host machine ๐ฅ๏ธ
- The external internet ๐
Docker automatically creates networks and connects containers based on mode.
๐ง Key Terms
Term | Meaning | Emoji |
---|---|---|
Network | Virtual connection b/w containers | ๐ฃ๏ธ |
Bridge | Default, isolated internal network | ๐ |
Host | Shares hostโs network stack | ๐ |
None | No network access | ๐ซ |
Overlay | Cross-host communication (Swarm) | ๐ธ๏ธ |
๐ Bridge Mode (Default)
๐งฑ Bridge network is like a private switch where containers talk to each other.
๐งต Created Automatically:
docker network ls
Look for: bridge
๐ How it works:
- Containers get private IPs (like
172.17.0.x
) - They can access the internet via NAT
- But cannot be accessed from outside without
-p
port mapping
๐งช Try it:
docker run -d --name container1 nginx docker run -d --name container2 busybox sleep 9999 # Ping container1 from container2 by IP docker exec -it container2 ping 172.17.0.x
โ By default, they canโt talk by name unless in custom network
๐งฑ Custom Bridge Network (Recommended)
๐ฏ Custom networks support container name resolution (DNS)!
๐ฆ Create a custom bridge:
docker network create my-network
๐ Launch containers into it:
docker run -d --name app1 --network my-network nginx docker run -it --name app2 --network my-network busybox sh
Now, inside app2
:
ping app1
โ Works! ๐ Containers can ping by name!
โ Why Use Custom Bridge?
Feature | Benefit |
---|---|
๐ง DNS | Resolve container names |
๐ Isolation | Only containers in same network can talk |
๐ Flexibility | Use multiple networks |
โ๏ธ Control | Inspect with docker network inspect |
๐ Host Network Mode
Shares the hostโs network stack directly.
๐ Use:
docker run --network host nginx
โ Pros:
- ๐ฅ Super fast โ no NAT or port mapping
- ๐งช Useful for monitoring tools (Prometheus, Grafana)
โ Cons:
- โ ๏ธ No isolation
- ๐งฑ Cannot run 2 containers on same port!
๐ซ None Network Mode
Container has no networking at all.
docker run --network none busybox
- ๐ Fully isolated
- Useful for security testing or offline compute jobs
๐ Overlay Network (Advanced โ Docker Swarm)
๐ธ๏ธ Enables containers on different hosts to communicate
Use Case:
- Docker Swarm
- Distributed Microservices
docker network create --driver overlay my-overlay
Requires Swarm mode:
docker swarm init
๐ Connect Containers to Multiple Networks
docker network create frontend docker network create backend docker run -d --name api \ --network frontend \ --network-alias api \ nginx
Then attach to another network:
docker network connect backend api
๐ง Inspect a Network
docker network inspect my-network
Shows:
- Container list
- IPs
- Aliases
- Subnets
๐งฐ CLI Recap
Command | Purpose |
---|---|
docker network ls | List networks |
docker network create <name> | Create a custom network |
docker run --network <name> | Connect to specific network |
docker network inspect <name> | Inspect config and members |
docker network rm <name> | Delete a network |
docker network connect | Connect running container |
docker network disconnect | Disconnect container |
๐ฏ Best Practices for Docker Networking
Practice | Why Itโs Great |
---|---|
โ Use custom bridge networks | Enable name resolution + isolation |
๐ซ Avoid host mode unless needed | Can expose host stack |
๐งฑ Use none mode for compute-only jobs | Maximum isolation |
๐ Limit network access | Avoid connecting everything together |
๐ Use inspect to debug IPs | Know who talks to whom |
๐งช Use busybox or alpine to test ping | Lightweight network testing tools |
๐งพ Summary Table
Mode | Isolated? | Can Use DNS? | Host Access? | Notes |
---|---|---|---|---|
bridge | โ Yes | โ No (unless custom) | โ
via -p | Default |
custom bridge | โ Yes | โ Yes | โ
via -p | Best for local |
host | โ No | โ Yes | Direct | No port mapping |
none | โ Yes | โ No | โ No | For isolation |
overlay | โ Cross-host | โ Yes | Swarm only | For multi-node |
๐ง Final Analogy
Bridge network = Private Wi-Fi router ๐
Custom bridge = Guest Wi-Fi with name tags ๐ท๏ธ
Host mode = Ethernet cable directly into host ๐ป
None mode = Airplane mode โ๏ธ
Overlay = Corporate VPN connecting multiple offices ๐ข๐ข
๐ Docker Networking Overview
๐ฆ Container networking is the foundation of container communication. Every container is equipped with a network interface, IP address, routing table, DNS config, etc.
โ By default, containers can:
- Make outbound connections (internet)
- Be connected to default or custom networks
- Be isolated or exposed, depending on the configuration
๐ฆ Types of Docker Network Drivers
Network Driver | Description | Use Case |
---|---|---|
bridge | ๐งฑ Default isolated network for single-host | Local development |
host | ๐ Shares hostโs network namespace | Low-latency, host-level apps |
none | ๐ซ No networking at all | Offline or compute-only tasks |
overlay | ๐ Enables multi-host (Swarm) communication | Distributed systems |
macvlan | ๐ชช Assigns MAC + IP from hostโs network | IoT, legacy systems |
ipvlan | Similar to macvlan, IP only | More control over routing |
custom plugins | ๐ Extendable third-party solutions | SDN, advanced use |
๐งฑ Default Bridge Network vs ๐งฉ User-Defined Bridge Network
Both are based on the bridge
driver, but they differ significantly in features & behavior.
๐งฑ Default bridge
Network
Created automatically by Docker when installed.
docker network ls # OUTPUT will contain: # bridge bridge local
Example:
docker run -d --name app1 nginx docker run -d --name app2 busybox sleep 999
๐ Limitations:
โ Limitation | Explanation |
---|---|
๐ซ No DNS | Can't resolve container names |
๐ต Not isolated | All containers using default bridge are technically on the same LAN |
๐ Poor discoverability | Need to link manually or use IPs |
๐ Less secure | Containers can talk across networks by default if not isolated |
๐งฉ User-Defined Bridge Network
Created with:
docker network create my-custom-net
โ Advantages:
โ Feature | Benefit |
---|---|
๐ง Built-in DNS | Containers can resolve each other by name |
๐ Isolated environment | Containers only talk to others on the same network |
๐ Auto service discovery | Works like microservices |
๐ง Fine-grained control | Inspect, attach, detach easily |
๐ Easier multi-container orchestration | Compose, Swarm, or manually |
โ๏ธ Comparison: Default vs User-Defined Bridge
Feature | Default bridge ๐ | User-Defined Bridge ๐งฉ |
---|---|---|
DNS support | โ No | โ Yes |
Service name resolution | โ No | โ Yes |
Isolation | โ Less secure | โ Scoped per network |
Compose support | ๐ซ Not recommended | โ Fully supported |
Security | Basic | Scoped & controlled |
Container-to-container name access | โ Only via IP | โ
Via name (ping app1 ) |
Preferred for production/dev | โ No | โ Yes |
๐ Inspecting Networks
docker network inspect my-custom-net
๐ Output includes:
- Subnet
- Gateway
- Connected containers
- DNS aliases
๐งช Example Test
1. Default Bridge (no DNS):
docker run -d --name alpha nginx docker run -it --rm busybox # ping alpha โ โ fails
2. User-defined Bridge:
docker network create testnet docker run -d --name alpha --network testnet nginx docker run -it --rm --network testnet busybox # ping alpha โ โ
works
๐งฑ Other Drivers โ Quick Overview
Driver | Emoji | Key Use |
---|---|---|
host | ๐ | High-perf apps (no NAT), not isolated |
none | ๐ซ | Secure offline or processing containers |
overlay | ๐ | Multi-host Swarm networking |
macvlan | ๐ชช | Assign physical IPs from hostโs LAN |
ipvlan | ๐งญ | Fine-grained routing control |
custom plugin | ๐งฐ | CNI integrations, SDNs (like Calico) |
๐ When to Use What?
Use Case | Best Network Driver |
---|---|
Local dev, isolated apps | ๐งฉ User-defined bridge |
Multi-container orchestration | ๐งฉ Custom bridge + Docker Compose |
High-speed, low-latency app (e.g. Prometheus) | ๐ Host network |
No internet access container | ๐ซ None |
Containers across multiple hosts | ๐ Overlay (Swarm mode) |
Assign IPs from LAN for legacy systems | ๐ชช Macvlan |
๐ฏ Key Docker Networking Commands Cheat Sheet ๐ณ
๐ง Command | ๐ฌ Description |
---|---|
docker network ls | ๐ List all available networks |
docker network create <name> | ๐ ๏ธ Create a custom bridge network |
docker network rm <name> | ๐๏ธ Remove a network |
docker network inspect <name> | ๐ View detailed info about a network |
docker run --network <name> <image> | ๐ Create container in a specific network (unnamed) |
docker run -d --name <container> --network <network> <image> | โ Create named container in a custom network |
docker network connect <network> <container> | ๐ Attach an existing container to a network |
docker network disconnect <network> <container> | โ Detach a container from a network |
โ Real-World Example: Create and Use a Custom Bridge Network
# 1๏ธโฃ Create a custom bridge network docker network create my-bridge # 2๏ธโฃ Run a container (nginx) attached to that network docker run -d --name my-app --network my-bridge nginx # 3๏ธโฃ Run another container (busybox) in the same network docker run -it --name client --network my-bridge busybox # 4๏ธโฃ Inside 'client', you can ping 'my-app' by name ping my-app
๐ Result: client
can resolve and communicate with my-app
using container name thanks to Dockerโs internal DNS in custom bridge networks.
๐ง Bonus Tip: Disconnect & Reconnect
docker network disconnect my-bridge my-app # Disconnect from network docker network connect my-bridge my-app # Reconnect to same or new network
๐ง Final Takeaways
- ๐งฑ Default bridge is basic โ no name resolution, low security
- ๐งฉ User-defined bridge is preferred for real-world apps
- ๐ Use overlay for distributed microservices
- ๐งฐ Know when to use each driver to optimize performance & security
- ๐งช Always test communication with tools like
ping
,curl
,netcat
๐ Docker Networking Logic โ Container Communication
โ 1. User-Defined Bridge Network = Container DNS Heaven ๐ง
๐งฉ When you create a user-defined bridge network, Docker automatically enables an internal DNS service.
๐ฆ That means containers can talk to each other by name! ๐
Example:
docker network create my-net docker run -d --name db --network my-net mongo docker run -d --name web --network my-net node-app
๐ฏ Now, inside web
, you can:
ping db
โ
Boom! It works because Docker auto-resolves db
โ container's IP inside the same network.
โ 2. Default Bridge Network = Only IP-Based Access ๐ข
Containers in the default bridge cannot resolve each other by name.
Example:
docker run -d --name app1 nginx # default bridge docker run -it --name app2 busybox # default bridge
Inside app2
:
ping app1 # โ FAILS
Why? Because DNS resolution doesnโt work in the default bridge
without using the old --link
option (now deprecated ๐ซ).
๐ Legacy --link
(Avoid Using It)
docker run -d --name db mongo docker run -d --name web --link db node-app
โ ๏ธ Works โ but deprecated & removed in newer Docker versions.
๐ 3. Containers in Different Networks = ๐ซ No Communication
Example:
docker network create net-a docker network create net-b docker run -d --name app1 --network net-a nginx docker run -d --name app2 --network net-b busybox
Inside app2
:
ping app1 # โ FAILS โ isolated networks
๐งฑ Networks are isolated by default. Containers on different bridge networks cannot talk to each other unless you connect one container to both using:
docker network connect net-a app2
Now, app2
belongs to both networks!
๐ง Real-World Analogy
Concept | Analogy |
---|---|
๐งฑ Default Bridge | Talking in a crowd with no names, only IPs (๐ค192.168.x.x) |
๐งฉ User-defined Bridge | Talking in a chatroom where everyone has a username (๐ @web, @db) |
๐ Multi-Network | Having one foot in two rooms ๐ฆถ๐ฝ๐ช |
๐งช Recap โ Container Communication Matrix
Scenario | Communicate by name? | Communicate by IP? | Notes |
---|---|---|---|
Same user-defined bridge | โ Yes | โ Yes | Best practice |
Same default bridge | โ No | โ Yes | No name resolution |
Different networks | โ No | โ No | Unless manually connected |
With --link (legacy) | โ ๏ธ Yes | โ Yes | Deprecated, avoid |
โ Summary
- ๐งฉ User-defined networks allow name-based communication via Docker's built-in DNS.
- ๐งฑ Default bridge networks don't support DNS โ only IPs.
- ๐ Each network is isolated โ containers inside a network can talk, but can't reach containers in other networks unless manually connected.
- ๐ก Use
docker network connect
to join a container to multiple networks if needed.
๐ Overview of Advanced Docker Network Drivers
Driver | Purpose | Host-to-Container | Container-to-Host | Cross-Host Support |
---|---|---|---|---|
overlay | Cross-host container communication (Swarm) | โ Yes | โ Yes | โ Yes |
macvlan | Assign real MAC & IP from LAN to container | โ No (by default) | โ Yes | โ No |
ipvlan | IP-level control without creating MAC addresses | โ Yes | โ Yes | โ No |
1๏ธโฃ ๐ Overlay Network
๐ What It Is:
Allows containers on different Docker hosts to securely communicate as if they were on the same LAN.
๐ก Requires Docker Swarm (or other orchestrators).
Creates an encrypted VXLAN tunnel between hosts.
๐ง Real-world Analogy:
Like a VPN that connects branch offices (containers) across cities (hosts).
โ Use Cases:
- Multi-host microservices
- Docker Swarm services
- HA + distributed architecture
๐ How to Use (with Swarm):
# 1. Initialize Swarm docker swarm init # 2. Create overlay network docker network create --driver overlay my-overlay # 3. Deploy service to use overlay docker service create \ --name webapp \ --replicas 3 \ --network my-overlay \ nginx
๐ Key Features:
Feature | Benefit |
---|---|
๐ Encrypted traffic | Uses IPSEC tunneling |
๐ Auto service discovery | Works across nodes |
๐ฆ Built-in load balancing | Container-to-service routing |
๐ง Works with Docker Compose (v3+) | Great for multi-host orchestration |
2๏ธโฃ ๐ชช Macvlan Network
๐ What It Is:
Allows containers to appear as physical devices on the hostโs network, each with their own IP and MAC.
๐ก The container bypasses Dockerโs NAT, appearing directly on your LAN.
๐ง Real-world Analogy:
Like plugging a new computer (container) directly into your office switch with its own IP.
โ Use Cases:
- Legacy apps that require static IPs or MACs
- IoT, embedded, or bare metal simulation
- When containers must be reachable from the LAN directly
๐ How to Use:
# 1. Create macvlan network docker network create -d macvlan \ --subnet=192.168.1.0/24 \ --gateway=192.168.1.1 \ -o parent=eth0 \ macvlan-net # 2. Run a container on that network docker run -d --name myrouter \ --network macvlan-net \ busybox sleep 3600
๐ parent=eth0
: your physical hostโs network interface
โ ๏ธ Limitations:
โ Limitation | Description |
---|---|
๐ซ No container-to-host | Can't ping container from host by default |
๐ Can bypass Docker security stack | Be cautious in shared infra |
๐ Requires IP address planning | Must avoid IP conflicts |
๐ Tip to Enable Host โ Container Communication (Workaround):
Create a dummy interface on the host:
ip link add macvlan-shim link eth0 type macvlan mode bridge ip addr add 192.168.1.200/24 dev macvlan-shim ip link set macvlan-shim up
3๏ธโฃ ๐งฌ IPvlan Network
๐ What It Is:
Similar to macvlan, but no extra MACs per container.
All containers share hostโs MAC, just get different IPs.
โ๏ธ More compatible with cloud and DHCP setups where duplicate MACs are not allowed.
๐ง Analogy:
Multiple workers using one ID card (MAC) but different phone numbers (IP).
โ Use Cases:
- Performance-sensitive systems
- Cloud infra with MAC restrictions
- Advanced network routing with minimal overhead
๐ How to Use:
docker network create -d ipvlan \ --subnet=192.168.1.0/24 \ --gateway=192.168.1.1 \ -o parent=eth0 \ ipvlan-net docker run -it --rm --network ipvlan-net alpine
๐งช IPvlan supports two modes:
-
l2
: Same subnet, like macvlan -
l3
: Different subnet, routing via host
โ Benefits:
Feature | Benefit |
---|---|
๐ง Efficient | No MAC duplication |
๐ More predictable IP rules | Good for secured environments |
๐งฐ Custom routing support | Great for advanced network setups |
๐ Advanced Network Drivers Comparison Table
Feature/Driver | overlay ๐ | macvlan ๐ชช | ipvlan ๐งฌ |
---|---|---|---|
Cross-host support | โ | โ | โ |
Requires Swarm? | โ | โ | โ |
Uses physical IP/MAC | โ | โ | โ (IP only) |
Host โ container communication | โ | โ (manual fix) | โ (limited) |
Best for | Microservices on multiple hosts | LAN-level communication | Custom IP routing or cloud infra |
Security model | Swarm-controlled | Exposes real IP on LAN | More controlled than macvlan |
Complexity | ๐ก Medium | ๐ด High | ๐ Medium-High |
๐ Security Considerations
Driver | Security Tip |
---|---|
Overlay | Isolated per service; enable encryption |
Macvlan | Bypasses Dockerโs firewall โ isolate via VLAN |
IPvlan | Good firewall compatibility; still isolate with subnet rules |
๐ When to Use What?
Situation | Use Driver |
---|---|
๐ Multi-host Swarm deployments | overlay |
๐ก Direct LAN access required | macvlan |
๐งฌ Controlled IP mapping, no MAC exposure | ipvlan |
๐ง Summary
-
overlay
: Best for Swarm, cross-node services, scalable infra -
macvlan
: Best for LAN visibility, legacy hardware, IP-bound apps -
ipvlan
: Best for performance and controlled environments (e.g., cloud)
๐ซ Docker none
Network Driver โ Ultimate Guide
โ What is the none
Network?
The
none
network is a special Docker network driver that completely disables networking for a container.
๐ซ No IP address
๐ซ No routing
๐ซ No DNS
๐ซ No internet access
๐ซ No communication with host or other containers
๐ Use Case:
When you want your container to run in complete isolation, especially for:
- โ CPU-intensive or file-only tasks
- โ Secure environments with no external communication
- โ Containers that interact only via volume sharing or IPC
- โ Avoiding network-related attacks (like SSRF, port scanning, etc.)
๐ง Real-World Analogy
Itโs like putting a person in a soundproof, windowless room ๐งโโ๏ธ๐
They can compute, read, or write files โ but cannot talk or hear the outside world.
๐งช How to Use It
docker run -it --rm --network none alpine
Then inside the container:
ping google.com # โ Fails ip addr # Shows no IP
โ The container runs, but itโs completely cut off from any kind of networking.
๐ Check from the Host
docker inspect <container-id> | grep -i "NetworkMode" # Output: "NetworkMode": "none"
๐งฐ Useful Scenarios
Use Case | Why none Works |
---|---|
๐งฎ Data processing / math computation | Doesn't need the internet |
๐ Security sandboxing | No attack vector via networking |
๐งช Testing firewall rules | Simulate โno internetโ condition |
๐ง Build-only stages | Avoid leaking credentials over network |
๐ DevOps CI/CD | Run isolated build/test tasks with no exposure |
๐ Warning
- You cannot
ping
,curl
,apt update
, or download anything inside containers using--network none
- Any tools that require internet or inter-container access will fail
- It's not usable for most microservices or web APIs
๐ง Tip: Combine with Volumes or IPC
If you want to exchange data without a network:
# Create a shared volume docker volume create shared-data # Use it with the isolated container docker run -it --rm --network none -v shared-data:/data alpine
โ This lets you read/write to shared storage without needing any network access.
๐งพ Summary Table
Feature | none Driver |
---|---|
IP address | โ None |
DNS | โ None |
Host access | โ None |
Container-to-container | โ None |
Internet | โ None |
Use case | Security, sandboxing, isolated compute |
๐ Final Verdict
If you need absolute network isolation, --network none
is your zero-trust go-to option.
Itโs perfect for:
- ๐ Security-first workloads
- ๐งช Testing internal-only logic
- ๐ซ Disabling all remote calls
Top comments (0)