Docker part 3- Docker Networking
Subscribe YouTube Channel @devops-pathshala
Docker Networking
Docker networking is the way Docker containers communicate with each other, with the host system, and with the outside world (like the internet). Since containers are isolated by default, networking allows them to exchange data and services in a controlled and flexible manner.
1. Why Docker Networking is Needed
-
Each container has its own isolated environment, including its own network stack.
-
To allow communication between:
-
Containers on the same host
-
Containers across different hosts
-
Containers and the external world (internet)
-
-
Networking also allows services to be exposed securely to the outside.
2. Types of Docker Networks
Docker provides several built-in network types:
| Network Type | Description | Use Case |
|---|---|---|
| Bridge | Default network for containers on a single host. Containers can talk to each other via IP. | Ideal for standalone apps or local development. |
| Host | Containers use the host’s network directly. No isolation. | High performance networking; avoids network translation overhead. |
| None | Containers have no network interface. | For completely isolated containers. |
| Overlay | Allows communication between containers on different Docker hosts (used in Swarm). | Useful for multi-host setups in clusters. |
| Macvlan | Containers get their own MAC addresses and appear as physical devices on the network. | When you need containers to be treated like separate machines on the LAN. |
3. Key Concepts
-
Container IP Address: Each container gets a unique IP within its network.
-
Port Mapping: Exposes container ports to host ports (e.g.,
-p 8080:80). -
DNS in Docker: Containers can resolve each other by name using Docker’s internal DNS.
-
Networks Isolation: Containers on different networks cannot communicate unless connected to the same network.
Default network: If you don’t specify a network when running a container, Docker connects it to the default
bridgenetwork.-
Isolation: Containers on a bridge network are isolated from the host network and other networks unless explicitly connected.
-
Communication: Containers on the same bridge network can communicate with each other using IP addresses or container names.
-
Port Mapping: To make a container accessible from outside the host, you need to map container ports to host ports (e.g.,
-p 8080:80).
Docker creates a virtual subnet and gateway for the bridge network.
-
Each container gets its own private IP in this subnet.
-
Containers use NAT (Network Address Translation) to access external networks like the internet.
3. Example Commands
Check existing networks:
docker network ls
4. When to Use
-
For simple multi-container setups on a single host.
-
When you need container-to-container communication without exposing everything to the host network.
1. Key Features of Host Network
-
No isolation: The container shares the host’s network completely.
-
No NAT: Traffic goes directly through the host network without network address translation.
-
High performance: Since there’s no extra layer of virtual networking, it can be faster than bridge networks.
-
Port mapping not needed: You don’t need
-pto expose container ports—they are already using the host’s ports.
2. How It Works
-
Normally, containers get a separate network namespace and IP via Docker’s bridge network.
-
In host mode, Docker skips creating a new namespace; the container sees the same network interfaces as the host.
-
Any service running inside the container binds directly to the host IP.
4. When to Use Host Network
-
When you need maximum network performance.
-
When you want the container to use host IP directly for communication.
-
Useful for monitoring or networking tools that require host network access.
⚠️ Caution
-
Less secure because the container is not isolated from the host network.
-
Port conflicts can occur if the host is already using the same ports.
Comments
Post a Comment