๐ŸŒ Docker Networking A2Z โ€“ Masterclass for Developers & DevOps



This content originally appeared on DEV Community and was authored by Darshan Vasani

๐ŸŒ 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


This content originally appeared on DEV Community and was authored by Darshan Vasani