Home/Learn/Docker/Docker Networking

Docker Networking

Intermediate
Networking

Docker provides isolated virtual networks for containers. Understanding bridge, host, and overlay networks — and how containers discover each other by name — is essential for multi-container apps.

Overview

When you install Docker, it creates three default networks: bridge (default for standalone containers), host (container shares the host network stack), and none (no network). When you create a user-defined bridge network, Docker provides built-in DNS: containers can resolve each other by container name. This is how Docker Compose works — every service gets a DNS name equal to its service name. Overlay networks span multiple Docker hosts (used in Docker Swarm and Kubernetes). Port mapping (-p host:container) is how you expose a container port to the outside world.

Bridge Network — Container-to-Container Communication

The default bridge network does not provide DNS resolution by name. Always create a user-defined bridge network — containers on the same user-defined bridge can reach each other by service/container name. This is automatically done by Docker Compose.

bash — user-defined bridge network
# Default bridge: containers communicate by IP only (fragile)

docker network ls

# NETWORK ID   NAME      DRIVER   SCOPE

# 3b8c9f1a2d   bridge    bridge   local   ← default

# 8e4f7c1b3d   host      host     local

# 9a2b5e0c8f   none      null     local



# ✅ Create user-defined bridge (containers get DNS by name)

docker network create my-app-net



# Run containers on the same network

docker run -d --name postgres --network my-app-net postgres:16

docker run -d --name backend  --network my-app-net \

  -e DATABASE_URL=postgres://postgres:5432/mydb \   # resolve 'postgres' by name!

  my-backend:latest



# Test DNS resolution inside a container

docker exec -it backend sh

# Inside: ping postgres   → resolves to postgres container IP

Port Mapping & Host Network

Containers are isolated — no inbound traffic reaches them unless you explicitly publish a port with -p. The host network mode gives a container direct access to all host ports (no isolation, best for performance-sensitive workloads that need full network access).

bash — port mapping and host network
# -p HOST_PORT:CONTAINER_PORT

docker run -d -p 8080:80 nginx      # localhost:8080 → container:80

docker run -d -p 127.0.0.1:3000:3000 my-api  # bind to localhost only



# Multiple ports

docker run -d \

  -p 8080:80 \

  -p 8443:443 \

  nginx



# Host network mode (Linux only, not macOS/Windows)

docker run --network host nginx     # uses host port 80 directly, no mapping needed

# ⚠️ no isolation — container can bind any host port



# Inspect network

docker inspect my-container | grep -A 20 '"Networks"'



# See container's IP

docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' my-container

Network Diagram

Visualising how containers, networks, and the host relate helps debug connectivity issues.

Docker network topology
                    Internet

                        │

                   Host Machine

                   eth0: 192.168.1.10

                        │

              Docker Network: my-app-net

              172.18.0.0/16

              ┌────────────────────────────┐

              │                            │

       ┌──────â”´──────┐          ┌──────────â”´──────┐

       │  postgres   │          │    backend      │

       │ 172.18.0.2  │◄─────────│ 172.18.0.3      │

       │  port 5432  │ by name! │  port 3000      │

       â””─────────────┘          â””─────────────────┘

                                        │

                               -p 8080:3000

                                        │

                              host port 8080 (public)

Key Points to Remember

  • 1User-defined bridge networks provide automatic DNS: containers resolve each other by name.
  • 2Default bridge network only allows IP-based communication.
  • 3Port mapping (-p host:container) is required to expose container services externally.
  • 4Host network mode removes network isolation — container uses host's network stack directly.
  • 5Overlay networks connect containers across multiple hosts (Swarm/Kubernetes).
  • 6Docker Compose automatically creates a user-defined network for all services in the file.

Interview Questions

Sign in to ask Aria
1

How do two containers on the same Docker network communicate?

2

What is the difference between -p 8080:80 and --network host?

Ask Aria about Docker Networking

Your personal AI tutor — ask anything about this concept

Revision Status

Personal Notes

Sign in to save personal notes for this topic.

Discussion

Sign in to join the discussion.

Loading discussion…