Day 18 - Docker Networking
Hi there! I'm Dinesh, a passionate Cloud and DevOps enthusiast. I love to dive into the latest new technologies and sharing my journey through blog.
Search for a command to run...
Hi there! I'm Dinesh, a passionate Cloud and DevOps enthusiast. I love to dive into the latest new technologies and sharing my journey through blog.
No comments yet. Be the first to comment.
1. Difference between Docker and Kubernetes Docker → Builds and runs containers.Kubernetes → Orchestrates containers across multiple nodes. Key points: Docker = container runtime. Kubernetes = container orchestration tool. Kubernetes provides auto...
In this session, we learn how to monitor a Kubernetes cluster using Prometheus and Grafana.This is not just theory — there is a GitHub repository containing all installation commands and demo steps.The repo will also be enhanced later with advanced K...
1. What is a ConfigMap in Kubernetes? A ConfigMap is used to store non-sensitive configuration data that your application needs — such as: Database port Connection type Any general configuration values In normal applications (non-Kubernetes), de...
Kubernetes normally supports built-in resources like: Deployment Service Pod ConfigMap Secret Ingress These are called native resources. Sometimes companies (Istio, ArgoCD, Prometheus Operator, Kyverno, etc.) want to add new features that Kub...
1. Why Kubernetes Services Are Needed When a Pod is created in Kubernetes, it receives a dynamic IP address.If the Pod dies and restarts, its IP changes.So other Pods (like checkout → payments) cannot rely on Pod IP because it changes, creating issue...
Docker Networking (or container networking) enables communication:
Between containers.
Between containers and the host system.
Every Docker container requires networking to send and receive data.
Networking in containers is similar in concept to traditional networking in virtual machines but lighter and more flexible.
Example:
These containers must exchange data (e.g., API calls, responses).
Networking allows this communication using IPs or service names.
Example:
Payment container stores sensitive information (credit cards, user data).
We need logical isolation — login users must not access the payment container.
So, Docker networking helps achieve both:
Connectivity, and
Isolation.
| Feature | Virtual Machine | Docker Container |
| OS | Each VM has its own OS | Containers share the host OS |
| Subnet | Can have separate subnets | Use Docker-managed subnets |
| Isolation | Built-in via hypervisor | Achieved via Docker networks |
Every host (server or laptop) has a network interface like:
eth0 → 192.168.1.10
Each container also gets its own interface:
eth0 → 172.17.0.2
These two belong to different subnets — so, direct ping fails.
Docker automatically creates a virtual bridge called docker0.
This bridge acts like a router between the host and containers.
When you create a container, Docker links its virtual ethernet (veth) to this bridge.
✅ Result: Containers can now communicate with the host and each other.
A bridge connects containers to the host through a virtual switch (docker0).
It provides:
Communication between containers.
Communication between container and host.
Internet access (via NAT).
docker network ls
Shows:
NETWORK ID NAME DRIVER SCOPE
abcd1234 bridge bridge local
Containers connected to the same bridge can ping each other.
All containers share the same subnet.
This network is created automatically by Docker.
Virtual bridge (docker0) created automatically.
Containers communicate using internal IPs.
Suitable for single-host setups.
The container shares the host’s network stack.
No separate IP; it uses the host’s IP.
Example:
docker run -d --network=host nginx
Pros: Faster, direct access.
Cons: No isolation; insecure (container = host access).
Used for multi-host communication (in Docker Swarm or Kubernetes).
Creates a network that spans across multiple Docker hosts.
Allows containers on different machines to communicate securely.
Common in container orchestration platforms.
Example setup:
Host eth0: 192.168.1.5
docker0 (bridge): 172.17.0.1
Container 1 eth0: 172.17.0.2
Container 2 eth0: 172.17.0.3
Both containers use the same bridge (docker0).
Hence:
They can ping each other.
They share the same communication channel.
⚠️ Problem:
All containers use the same bridge.
A security risk — if one container is compromised, others are accessible.
To isolate sensitive containers, you can create custom bridge networks.
The default bridge (docker0) allows all containers to communicate.
A custom bridge provides:
Network segmentation.
Security boundaries.
Controlled communication.
docker network create secure_network
docker network ls
Output:
bridge
host
none
secure_network
docker run -d --name login nginx
docker run -d --name logout nginx
Both use default bridge network.
Can ping each other.
docker network create secure_network
docker run -d --name finance --network=secure_network nginx
docker inspect finance
You’ll see:
"Networks": {
"secure_network": {
"IPAddress": "172.19.0.2"
}
}
✅ Result:
login → bridge → 172.17.x.x
finance → secure_network → 172.19.x.x
They cannot ping each other.
Finance container isolated successfully.
| Container | Network Type | Communication |
| login | default bridge | Can talk to logout |
| logout | default bridge | Can talk to login |
| finance | custom bridge | Isolated from others |
This achieves network-level security while staying within Docker itself.
docker run -d --name host_demo --network=host nginx
Container uses host’s IP (192.168.1.5).
docker inspect host_demo shows:
"NetworkMode": "host"
No separate IP address.
⚠️ No isolation — directly exposed on host’s interface.
| Network Type | Description | Use Case | Security |
| Bridge | Default virtual network via docker0 | Single-host apps | Medium |
| Host | Shares host network | Performance-critical or testing | Low |
| Overlay | Cross-host networking | Multi-node clusters (Swarm/K8s) | High |
| Custom Bridge | User-created network | Secure container isolation | High |
Docker networking lets containers communicate or isolate as needed.
Bridge Network → Default communication method.
Host Network → Shares host network; faster but insecure.
Overlay Network → For multi-host clusters (Docker Swarm/Kubernetes).
Custom Bridge Network → Best way to isolate secure containers on a single host.