πŸš€ Docker Networking: A Complete Guide

1️⃣ Introduction to Docker Networks

Docker provides different types of networks to enable communication between containers, host systems, and external networks.

Containers can connect to each other using IP addresses or container names (DNS resolution depends on the network type).


2️⃣ Types of Docker Networks

Network Type Scope Default? Container-to-Container Communication External Connectivity Use Case
bridge Local βœ… Yes βœ… Via container name βœ… (via -p flag) Isolated container networking
host Local ❌ No ❌ Uses host’s network βœ… Uses host’s IP Performance optimization
none Local ❌ No ❌ No networking ❌ No networking Security, isolated workloads
overlay Multi-Host ❌ No βœ… Across hosts βœ… Swarm services
macvlan Local ❌ No βœ… Uses unique MAC addresses βœ… Acts as a physical device Direct access to LAN

3️⃣ Detailed Explanation of Each Network Type

πŸ“Œ 1. Bridge Network (Default)

  • What Happens?
  • A virtual bridge (docker0) is created.
  • Containers get an IP from a private subnet (e.g., 172.17.0.0/16).
  • Containers can communicate only within the same bridge network.

  • How Containers Connect?
    βœ… By IP Address: Example β†’ ping 172.17.0.2
    βœ… By Name: DNS resolves names β†’ ping container2

  • Use Case:

  • Running standalone containers that need isolated networking.
  • Example: A database container communicating with an app container.

  • Commands:

    docker network create my_bridge
    docker run --network=my_bridge --name=app nginx
    docker run --network=my_bridge --name=db mysql
    

πŸ“Œ 2. Host Network

  • What Happens?
  • The container shares the host’s network stack.
  • No separate IP address for the container.

  • How Containers Connect?

  • Directly using the host’s IP.

  • Use Case:

  • Low-latency applications that need better network performance (e.g., gaming servers).
  • Avoiding port conflicts in monitoring tools.

  • Commands:

    docker run --network=host -d nginx
    

  • Difference from Bridge:

  • No isolation β†’ Containers share the same network as the host.

πŸ“Œ 3. None Network

  • What Happens?
  • No network is assigned.
  • No external or internal connectivity.

  • Use Case:

  • Security-sensitive applications (e.g., forensic analysis).
  • Data-processing containers that don’t need networking.

  • Commands:

    docker run --network=none busybox ifconfig
    

πŸ“Œ 4. Overlay Network (Multi-Host Networking)

  • What Happens?
  • Connects containers across multiple Docker hosts.
  • Uses an internal VXLAN for encrypted communication.

  • Use Case:

  • Docker Swarm deployments (multi-container, multi-host networking).
  • Microservices that need cross-host communication.

  • Commands:

    docker network create -d overlay my_overlay
    docker service create --network=my_overlay nginx
    

  • Example:

  • Containers running on host-1 and host-2 can communicate.

πŸ“Œ 5. Macvlan Network

  • What Happens?
  • Assigns real MAC addresses to containers.
  • Containers appear as physical devices in the network.

  • Use Case:

  • Running containers as network devices (e.g., a firewall, router).
  • Bypassing the host's IP stack.

  • Commands:

    docker network create -d macvlan --subnet=192.168.1.0/24 --gateway=192.168.1.1 -o parent=eth0 my_macvlan
    docker run --network=my_macvlan --ip=192.168.1.100 nginx
    

  • Example:

  • The container is now directly accessible from the local network.

4️⃣ When to Use Which Network?

Scenario Best Network Choice
Default single-host networking Bridge
High-performance applications Host
Isolated, security-critical workloads None
Multi-host microservices Overlay
Direct LAN access (e.g., DHCP, firewalls) Macvlan

5️⃣ How Containers Connect?

Network Type Connection by IP Connection by Name
Bridge βœ… Yes βœ… Yes
Host βœ… Yes (Host IP) ❌ No (No DNS resolution)
None ❌ No ❌ No
Overlay βœ… Yes βœ… Yes (Multi-host DNS resolution)
Macvlan βœ… Yes (LAN IP) ❌ No

6️⃣ Real-World Use Cases

πŸ”Ή Use Case 1: Web App + Database in a Bridge Network

Scenario: Running a Python Flask app with a MySQL database.

Steps:

docker network create my_bridge
docker run -d --network=my_bridge --name=db mysql
docker run -d --network=my_bridge --name=app my-flask-app

Communication:

mysql -h db -uroot -p

πŸ”Ή Use Case 2: Nginx Reverse Proxy on Host Network

Scenario: Running Nginx as a reverse proxy with low latency.

Command:

docker run --network=host -d nginx

πŸ”Ή Use Case 3: Multi-Host Swarm with Overlay Network

Scenario: Running a multi-container app across multiple Docker hosts.

Commands:

docker network create -d overlay my_overlay
docker service create --network=my_overlay my-service

πŸ”Ή Use Case 4: Assigning Static IP with Macvlan

Scenario: Running a container that acts like a physical device.

Commands:

docker network create -d macvlan --subnet=192.168.1.0/24 -o parent=eth0 my_macvlan
docker run --network=my_macvlan --ip=192.168.1.100 nginx

Container now accessible at: 192.168.1.100


7️⃣ Summary of Key Differences

Feature Bridge Host None Overlay Macvlan
Scope Single Host Single Host Single Host Multi-Host Single Host
Container-to-Container βœ… Yes ❌ No ❌ No βœ… Yes (Multi-Host) βœ… Yes
External Access βœ… Yes (-p) βœ… Yes (Host IP) ❌ No βœ… Yes βœ… Yes
Performance πŸ”Ή Normal πŸ”₯ High πŸ”Ή Secure πŸ”Ή Distributed πŸ”Ή Direct Access
Use Case Multi-container apps Low-latency apps Secure workloads Swarm LAN Access