By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
(Zero-fluff, hyper-practical guide for Docker & Kubernetes engineers)
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.
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.
bridge
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.
host
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.
overlay
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.
macvlan
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.
none
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
Ingress
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).
example.com/api → backend-service
docker --version
kubectl
kubectl get nodes
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).
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 overhead → faster (good for high-throughput apps like Redis).- No port isolation → only one container can use port 80.
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).
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.
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.
frontend
backend
kubectl get --raw /metrics
ClusterIP
NodePort
livenessProbe
readinessProbe
kubectl logs -n kube-system calico-node-xxxx
kubectl describe pod
docker network create
Error: listen EADDRINUSE
ContainerCreating
ip link set eth0 promisc on
NetworkPolicy
"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).
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.
web
api
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.
podSelector
ingress.from
ports
docker network create --driver bridge my_net
docker run --network host nginx
docker network create --driver overlay my_overlay
docker network create -d macvlan --subnet=192.168.1.0/24 -o parent=eth0 my_macvlan
kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml
kubectl get networkpolicy
kubectl describe pod <pod> \| grep -i "network"
NetworkNotReady
Final Tip:"If your containers can’t talk, check the network mode first. 90% of issues are bridge vs host vs overlay misconfigurations." ?
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.