Fatskills
Practice. Master. Repeat.
Study Guide: TECH **Container Networking Deep Dive: Bridge, Host, Overlay, Macvlan**
Source: https://www.fatskills.com/kubernetes/chapter/tech-container-networking-deep-dive-bridge-host-overlay-macvlan

TECH **Container Networking Deep Dive: Bridge, Host, Overlay, Macvlan**

By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.

⏱️ ~8 min read

Container Networking Deep Dive: Bridge, Host, Overlay, Macvlan

(Zero-fluff, hyper-practical guide for Docker & Kubernetes engineers)


1. What This Is & Why It Matters

Container networking is how your containers talk to each other, the host, and the outside world. Get this wrong, and your app either can’t communicate or becomes a security nightmare.

Real-World Scenario:

You’re deploying a microservices app in Kubernetes. Some services need to talk to each other (e.g., frontend → backend), some need direct internet access (e.g., API calls to Stripe), and some must stay isolated (e.g., a payment processor). If you pick the wrong network mode: - Bridge mode? Containers can talk, but performance suffers under heavy load.
- Host mode? Fast, but no isolation—containers fight over ports.
- Overlay? Great for multi-host Kubernetes, but adds complexity.
- Macvlan? Perfect for legacy apps needing direct LAN access, but tricky to debug.

This guide will teach you:
✅ When to use each network mode (and when to avoid it).
✅ How to configure them in Docker and Kubernetes.
✅ Common pitfalls that break production deployments.


2. Core Concepts & Components


1. Docker Network Drivers

  • bridge (default): Creates an internal private network for containers on the same host. Think of it like a switch inside your machine.
  • Production insight: If you don’t specify a network, Docker uses bridge. This is fine for local dev but terrible for production (no DNS, manual port mapping, no multi-host support).

  • host: Removes network isolation—containers use the host’s network stack directly. Like plugging a device directly into your router.

  • Production insight: Faster than bridge (no NAT overhead), but no port isolation—two containers can’t bind to the same port.

  • overlay: Enables multi-host networking (e.g., Docker Swarm, Kubernetes). Like a VPN for containers across multiple machines.

  • Production insight: Essential for Kubernetes clusters, but adds latency (~10-20% slower than host mode).

  • macvlan: Assigns a MAC address to each container, making them appear as physical devices on your LAN. Like giving each container its own Ethernet cable.

  • Production insight: Great for legacy apps (e.g., IoT devices, NAS), but requires promiscuous mode on the host NIC (security risk).

  • none: No networking. Container is isolated. Like a computer with no network cable.

  • Production insight: Use for security-sensitive workloads (e.g., processing PII), but you’ll need another way to get data in/out (volumes, shared files).

2. Kubernetes Networking Models

  • CNI (Container Network Interface): A plugin system for Kubernetes networking (e.g., Calico, Flannel, Cilium).
  • Production insight: You must pick a CNI plugin—Kubernetes doesn’t provide one by default.

  • ClusterIP: Internal-only Kubernetes service. Like an internal company phone directory.

  • Production insight: Default for most services—use for backend-to-backend communication.

  • NodePort: Exposes a service on a static port on each node. Like opening a door in your office building.

  • Production insight: Avoid in production—use LoadBalancer or Ingress instead.

  • LoadBalancer: Cloud-provider managed load balancer (e.g., AWS ALB, GCP LB).

  • Production insight: Expensive—only use for public-facing services.

  • Ingress: HTTP/HTTPS routing rules (e.g., example.com/api → backend-service).

  • Production insight: Always use Ingress for web apps—avoids exposing NodePorts.


3. Step-by-Step Hands-On


Prerequisites

  • Docker installed (docker --version).
  • Kubernetes cluster (Minikube, Kind, or cloud-based).
  • kubectl configured (kubectl get nodes).


Task 1: Docker Bridge Network (Default)

Goal: Create two containers that can talk to each other via DNS.


# Create a custom bridge network (better than default)
docker network create my_bridge

# Run two containers on the same network
docker run -d --name web --network my_bridge nginx
docker run -d --name api --network my_bridge alpine sleep 3600

# Test connectivity (DNS resolution works!)
docker exec -it api ping web
# Output: PING web (172.20.0.2): 56 data bytes
#         64 bytes from 172.20.0.2: seq=0 ttl=64 time=0.123 ms

# Clean up
docker rm -f web api
docker network rm my_bridge

Why this matters:
- Default bridge network doesn’t support DNS—you’d have to use IPs.
- Custom bridge networks are the minimum for production (DNS, better isolation).


Task 2: Docker Host Network (Performance Boost)

Goal: Run a container that binds directly to the host’s network.


# Run Nginx on port 80 (no port mapping needed)
docker run -d --name web --network host nginx

# Verify (curl from host)
curl http://localhost
# Output: <!DOCTYPE html>... (Nginx welcome page)

# Clean up
docker rm -f web

Why this matters:
- No NAT overheadfaster (good for high-throughput apps like Redis).
- No port isolationonly one container can use port 80.


Task 3: Docker Overlay Network (Multi-Host)

Goal: Create a network that spans multiple Docker hosts (e.g., for Swarm/Kubernetes).


# Initialize Docker Swarm (if not already)
docker swarm init

# Create an overlay network
docker network create --driver overlay my_overlay

# Run services on different nodes (simulate with two terminals)
docker service create --name web --network my_overlay --replicas 2 nginx
docker service create --name api --network my_overlay --replicas 2 alpine sleep 3600

# Test connectivity (DNS works across hosts!)
docker exec -it $(docker ps -q --filter name=web) ping api
# Output: PING api (10.0.1.3): 56 data bytes
#         64 bytes from 10.0.1.3: seq=0 ttl=64 time=0.456 ms

# Clean up
docker service rm web api
docker network rm my_overlay

Why this matters:
- Kubernetes uses overlay networks by default (e.g., Flannel, Calico).
- Slower than host mode (~10-20% latency overhead).


Task 4: Docker Macvlan (Legacy Apps)

Goal: Assign a container a MAC address on your LAN (like a physical device).


# Find your host's network interface (e.g., eth0, enp0s3)
ip link show

# Create a macvlan network (replace eth0 with your interface)
docker network create -d macvlan \
  --subnet=192.168.1.0/24 \
  --gateway=192.168.1.1 \
  -o parent=eth0 \
  my_macvlan

# Run a container with a MAC address
docker run -d --name web --network my_macvlan --ip=192.168.1.100 nginx

# Verify (access from another machine on the LAN)
curl http://192.168.1.100
# Output: <!DOCTYPE html>... (Nginx welcome page)

# Clean up
docker rm -f web
docker network rm my_macvlan

Why this matters:
- Legacy apps (e.g., IoT, NAS) often need direct LAN access.
- Security risk: Containers are visible to the entire LAN.


Task 5: Kubernetes Networking (Calico CNI)

Goal: Deploy a Kubernetes cluster with Calico for network policies.


# Install Calico (if using Minikube)
minikube start --network-plugin=cni --cni=calico

# Verify Calico is running
kubectl get pods -n kube-system | grep calico

# Deploy two pods
kubectl apply -f - <<EOF
apiVersion: v1
kind: Pod
metadata:
  name: web
spec:
  containers:
  - name: nginx
image: nginx --- apiVersion: v1 kind: Pod metadata: name: api spec: containers: - name: alpine
image: alpine
command: ["sleep", "3600"] EOF # Test connectivity (DNS works!) kubectl exec -it api -- ping web # Output: PING web (192.168.123.45): 56 data bytes # 64 bytes from 192.168.123.45: seq=0 ttl=63 time=0.123 ms # Clean up kubectl delete pod web api

Why this matters:
- Kubernetes requires a CNI plugin (Calico, Flannel, Cilium).
- Network policies (e.g., "only allow frontend to talk to backend") are critical for security.


4. ? Production-Ready Best Practices


Security

  • Never use host mode in production (no isolation).
  • Use network policies (Kubernetes) to restrict pod-to-pod traffic.
  • Avoid macvlan unless absolutely necessary (security risk).
  • Encrypt overlay networks (e.g., WireGuard in Cilium).

Performance

  • Use host mode for high-throughput apps (e.g., Redis, Kafka).
  • Avoid bridge for production (use custom bridge or overlay).
  • Monitor network latency (e.g., kubectl get --raw /metrics).

Reliability

  • Use ClusterIP for internal services (avoid NodePort).
  • Always define livenessProbe and readinessProbe (prevents traffic to unhealthy pods).
  • Use Ingress for HTTP/HTTPS traffic (avoids exposing NodePorts).

Observability

  • Monitor CNI plugin logs (kubectl logs -n kube-system calico-node-xxxx).
  • Use kubectl describe pod to debug networking issues.
  • Set up Prometheus + Grafana for network metrics.


5. ⚠️ Common Mistakes & Traps

Mistake Symptom Fix/Prevention
Using default bridge network in production Containers can’t resolve each other via DNS Always create a custom bridge network (docker network create).
Running multiple containers in host mode on the same port Port conflicts (Error: listen EADDRINUSE) Use bridge or overlay for multi-container apps.
Forgetting to install a CNI plugin in Kubernetes Pods stuck in ContainerCreating Install Calico/Flannel before deploying workloads.
Using macvlan without promiscuous mode Containers can’t communicate with the host Enable promiscuous mode on the host NIC (ip link set eth0 promisc on).
Exposing NodePort in production Security risk (publicly accessible) Use Ingress or LoadBalancer instead.


6. ? Exam/Certification Focus


Docker Certified Associate (DCA)

  • Question: "Which Docker network mode allows containers to communicate across multiple hosts?"
  • Answer: overlay (not bridge or host).
  • Trap: host mode is not multi-host—it only works on a single machine.

Certified Kubernetes Administrator (CKA)

  • Question: "How do you restrict traffic between pods in Kubernetes?"
  • Answer: Use NetworkPolicy (requires a CNI plugin like Calico).
  • Trap: Kubernetes does not enforce network policies by default—you need a CNI plugin.

Scenario-Based Question

"You need to deploy a legacy app that must appear as a physical device on the LAN. Which Docker network mode should you use?" - Answer: macvlan (but warn about security risks).


7. ? Hands-On Challenge

Challenge:
Deploy two Kubernetes pods (web and api). Use NetworkPolicy to only allow web to talk to api on port 80, and block all other traffic.

Solution:


# network-policy.yaml
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-web-to-api
spec:
  podSelector:
matchLabels:
app: api ingress: - from:
- podSelector:
matchLabels:
app: web
ports:
- protocol: TCP
port: 80

Why it works:
- podSelector targets the api pod.
- ingress.from allows only web to connect.
- ports restricts traffic to port 80.


8. ? Rapid-Reference Crib Sheet

Command/Concept Usage Default/Notes
docker network create --driver bridge my_net Create a custom bridge network Default network is bridge (no DNS).
docker run --network host nginx Run container in host mode ⚠️ No port isolation.
docker network create --driver overlay my_overlay Create multi-host overlay network Requires Docker Swarm.
docker network create -d macvlan --subnet=192.168.1.0/24 -o parent=eth0 my_macvlan Create macvlan network ⚠️ Requires promiscuous mode.
kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml Install Calico CNI Required for NetworkPolicy.
kubectl get networkpolicy List network policies ⚠️ No policies by default.
kubectl describe pod <pod> \| grep -i "network" Debug pod networking Look for NetworkNotReady.
ip link set eth0 promisc on Enable promiscuous mode (macvlan) ⚠️ Security risk.


9. ? Where to Go Next

  1. Docker Networking Docs – Official reference.
  2. Kubernetes Networking – CNI plugins, NetworkPolicy.
  3. Calico Networking – Best for Kubernetes network policies.
  4. Cilium (eBPF-based CNI) – High-performance networking.

Final Tip:
"If your containers can’t talk, check the network mode first. 90% of issues are bridge vs host vs overlay misconfigurations." ?



ADVERTISEMENT