Docker Networks
Docker networks provide connectivity between containers, the Docker host, and external network services.
Key Topics
- • Container connectivity
- • Network isolation
- • IP addressing
- • Network communication
Learn how Docker containers communicate with each other and the outside world. Understand bridge networks, container DNS, port publishing, network drivers, custom networks, and Docker Compose networking.
Docker Networking Path
Docker Networks
Bridge Networking
Container DNS
Port Publishing
Network Drivers
Docker Compose Networking
Introduction
Docker networking allows containers to communicate with other containers, Docker hosts, and external network services.
Every container can have network interfaces, IP addresses, gateways, routing information, and DNS configuration depending on the networks attached to it.
Docker networking also provides network isolation, allowing different groups of containers to communicate only when they are connected to appropriate networks.
Core Concepts
Learn the essential concepts required to build connected and isolated container environments.
Docker networks provide connectivity between containers, the Docker host, and external network services.
Key Topics
Bridge networks are commonly used for containers running on the same Docker host and provide isolation between network groups.
Key Topics
Containers connected to a user-defined network can communicate using supported DNS name resolution such as container names.
Key Topics
Port publishing makes selected container ports available through the Docker host.
Key Topics
Docker provides different network drivers for local containers, host networking, isolated containers, and multi-host environments.
Key Topics
Applications with multiple services can connect components such as web applications, APIs, databases, and caches through Docker networks.
Key Topics
Network Drivers
Docker provides multiple network drivers for different networking requirements, including local container communication and multi-host networking.
A bridge network connects containers running on the same Docker host while providing network isolation from containers on other networks.
docker network create my-networkHost networking removes network isolation between the container and Docker host by allowing the container to use the host networking stack.
docker run --network host nginxThe none network driver provides a container with network isolation and no external network connectivity.
docker run --network none alpineOverlay networks support communication across multiple Docker hosts and are used with Docker Swarm networking.
docker network create -d overlay my-overlayPractical Example
User-defined networks provide a useful way to group related containers and isolate them from containers on other networks.
Containers connected to the same user-defined network can communicate using supported container name resolution.
Example Commands
# Create a network docker network create my-app-network # Run an application container docker run -d \ --name web-app \ --network my-app-network \ nginx # Run another container docker run -d \ --name api-service \ --network my-app-network \ nginx
Container Communication
Multi-container applications often include separate services for web applications, APIs, databases, caches, and background workers.
When related containers are connected to the same user-defined Docker network, services can communicate across that network using network addresses and supported name resolution.
Example Application Architecture
┌──────────────┐
│ Web App │
└──────┬───────┘
│
▼
┌──────────────┐
│ API Service │
└──────┬───────┘
│
▼
┌──────────────┐
│ Database │
└──────────────┘
All services connected to:
my-app-networkPort Publishing
Containers can run services on internal ports. Port publishing maps selected container ports to ports on the Docker host.
This allows users or external systems to access containerized applications through the host address and published port.
Port Mapping Example
docker run -d \ --name web-server \ -p 8080:80 \ nginx
In this example, port 8080 on the Docker host is mapped to port 80 inside the container.
Network Management
Docker provides commands for creating, inspecting, connecting, disconnecting, and removing networks.
docker network lsList Docker networks available on the host.
docker network create my-networkCreate a new user-defined Docker network.
docker network inspect my-networkDisplay configuration and connection information for a Docker network.
docker network connect my-network my-containerConnect an existing container to a Docker network.
docker network disconnect my-network my-containerDisconnect a container from a Docker network.
docker network rm my-networkRemove a Docker network that is no longer required.
Docker Compose
Docker Compose can define networks for multi-container applications and connect services to one or more networks.
This allows you to separate application components and define which services should be able to communicate with each other.
compose.yaml Example
services:
frontend:
image: nginx
networks:
- app-network
backend:
image: node:22
networks:
- app-network
networks:
app-network:
driver: bridgeBest Practices
Create dedicated networks for related application services instead of placing unrelated containers on the same network.
Separate frontend, backend, database, and other components when network isolation is required.
Expose only the ports that need to be accessible outside the Docker host.
Use meaningful container or service names to simplify service communication and configuration.
Host networking removes normal network isolation, so use it only when its behavior is specifically required.
Use Docker network inspection tools to understand connected containers, IP addressing, and network configuration.
Learning Roadmap
Follow these steps to build a practical understanding of Docker networking and container communication.
Learn how containers communicate with other containers, hosts, and external services.
Understand bridge, host, none, and overlay networking models.
Create user-defined bridge networks and connect containers to them.
Run multiple containers on the same network and test communication between services.
Expose container services using published host ports.
Build multi-container applications with services communicating through Docker networks.
Continue Learning
Next Step
Practice creating Docker networks, connecting containers, publishing ports, and building multi-container applications with Docker Compose.