Skip to main content

Docker Network Cheatsheet — Bridge, Host, Overlay & Port Mapping

Docker networking connects containers to each other and to the outside world. This guide covers all network drivers, how to expose ports, link containers, and manage custom networks.

Updated: 2026-07-15·26 commands

Quick Reference

ScenarioCommand
List networksdocker network ls
Inspect a networkdocker network inspect bridge
Create a custom bridge networkdocker network create my-net
Connect container to networkdocker network connect my-net my-container
Disconnect container from networkdocker network disconnect my-net my-container
Remove a networkdocker network rm my-net
Run container with port mappingdocker run -p 8080:80 nginx
Run container on specific networkdocker run --network my-net nginx
Prune unused networksdocker network prune

Network Drivers

DriverIsolationScopeUse Case
bridge✅ Per-networkLocal hostDefault. Container-to-container on same host
host❌ NoneLocal hostMax performance, no NAT (e.g., monitoring tools)
overlay✅ Per-networkMulti-hostSwarm services, multi-host communication
noneFull isolationLocal hostNo network at all (loopback only)
macvlanPer containerLocal hostAssign MAC addresses, appear as physical devices

Core Commands

### Managing Networks

``bash # List all networks docker network ls # NETWORK ID NAME DRIVER SCOPE # abc12345 bridge bridge local # def67890 host host local

# Create a custom bridge network docker network create --driver bridge my-app-network

# Create with custom subnet docker network create --subnet 172.20.0.0/16 my-custom-net

# Inspect network details docker network inspect my-app-network

# Remove a network docker network rm my-app-network

# Clean up unused networks docker network prune `

### Connecting Containers

`bash # Run container on a specific network docker run -d --name api --network my-app-network my-api:latest

# Connect an existing container docker network connect my-app-network my-database

# Disconnect a container docker network disconnect my-app-network old-container

# Verify connectivity docker exec api ping my-database # Container name resolves automatically on user-defined networks `

### Port Mapping

`bash # Map host port 8080 to container port 80 docker run -p 8080:80 nginx

# Map multiple ports docker run -p 8080:80 -p 443:443 nginx

# Bind to specific host IP docker run -p 127.0.0.1:8080:80 nginx

# Random host port (let Docker choose) docker run -P nginx

# UDP port mapping docker run -p 53:53/udp dns-server `

### Inspecting Networks

`bash # Inspect default bridge docker network inspect bridge

# Inspect custom network (shows connected containers) docker network inspect my-app-network

# Show detailed container network config docker inspect --format '{{json .NetworkSettings}}' my-container | jq

# List containers on a network docker network inspect -f '{{range .Containers}}{{.Name}} {{end}}' my-net `

Port Mapping vs Network Connect

Both approaches expose containers to other services, but they serve different purposes.

AspectPort Mapping (-p)Network Connect (--network)
Access from outside host✅ Yes (via host IP)❌ No (internal only)
Container-to-container DNS❌ Manual IP needed✅ Automatic name resolution
Port conflicts❌ Conflicts on same host port✅ No port conflicts
SecurityExposed to host networkIsolated on virtual network
Multiple networks❌ Single host port binding✅ Container on many networks
Load balancing❌ Manual✅ DNS round-robin (user-defined)
When to use each:

- Port mapping: Expose a container to the outside world — web apps, APIs, databases accessed from outside the host. - Network connect: Enable inter-container communication — microservices talking to each other, backend containers that don't need external access.

Example — combining both:

`bash # Create a shared network docker network create backend-net

# Database accessible only to other containers docker run -d --name db --network backend-net postgres:13

# API exposed to the world, also talks to DB docker run -d --name api --network backend-net -p 8080:80 my-api # api can reach db via hostname "db"; api is also available on host:8080 ``

Network(26)

CommandLevel
docker network create
Create a new network
Basic
docker network ls
List all networks
Basic
docker network inspect
Inspect network details
Basic
docker network connect
Connect a container to a network
Intermediate
docker network disconnect
Disconnect a container from a network
Intermediate
docker network rm
Remove one or more networks
Basic
docker network prune
Remove all unused networks
Expert
docker network --help
Display docker network help information
Basic
docker network ls -q
Display only network IDs
Basic
docker network ls --filter
List networks filtered by criteria
Intermediate
docker network create --driver
Create a network with a specific driver
Intermediate
docker network create --driver overlay
Create an overlay network driver (Swarm mode)
Expert
docker network create --subnet
Create a network with a custom subnet
Intermediate
docker network create --gateway --ip-range
Create a network with gateway and IP range
Expert
docker network create --internal
Create an internal network (no external access)
Intermediate
docker network create --attachable
Create an attachable overlay network
Expert
docker network create --ipv6
Create a network with IPv6 enabled
Expert
docker network create --label
Create a network with metadata labels
Intermediate
docker network create -o
Create a network with driver-specific options
Expert
docker network connect --ip
Connect a container with a specific IP address
Intermediate
docker network connect --alias
Connect a container with a network alias
Intermediate
docker network inspect --verbose
Inspect network configuration with verbose details
Intermediate
docker network inspect --format
Custom format network inspection output
Expert
docker network disconnect --force
Force disconnect a container from a network
Intermediate
docker network prune -f
Force remove all unused networks without confirmation
Basic
docker network prune --filter
Prune unused networks with filter
Expert

FAQ

This cheatsheet is compiled from official tool documentation. Last updated: 2026-07-15.