docker

Docker Networking Fundamentals

Docker Networking Fundamentals

By Shubhankar Tripathi • • 5 min read

Docker networking is the mechanism that allows containers to communicate with each other, communicate with the Docker host, access external networks, and expose applications to external clients.

Understanding Docker networking is essential before working with Docker Compose, microservices, databases, APIs, Kubernetes, and production container deployments.

Docker containers have networking enabled by default. A container sees a network interface, IP address, gateway, routing information, and DNS configuration, just as a networked system would.


What Is Docker Networking?

Docker networking provides connectivity between:

docker2

ConceptRemember
Docker NetworkProvides connectivity and isolation for containers
BridgeCommon networking driver for containers on the same host
User-defined BridgeProvides isolation and automatic DNS-based container-name resolution
Container IPAssigned from the Docker network subnet
Container NameCan be used for service discovery on user-defined networks
-pPublishes a container port through the Docker host
EXPOSEDeclares a container port; it does not itself publish it
Host NetworkContainer shares the host's network namespace
None NetworkDisables normal container networking
OverlayEnables networking across multiple Docker hosts
NAT/MasqueradingUsed by default for external connectivity from bridge networks

The key questions when troubleshooting Docker networking are:

  1. Which network is the container connected to?
  2. Can the source and destination containers communicate on that network?
  3. Can the destination be resolved by DNS/container name?
  4. Is the application listening on the expected container port?
  5. Does the host need a published port?
  6. Is the port published on the expected host address?
  7. Are firewall/NAT/routing rules affecting the connection?

Docker Commands

CommandPurpose
docker network lsList Docker networks
docker network create my-netCreate a network
docker network inspect my-netInspect network configuration
docker network rm my-netRemove a network
docker network connect my-net containerConnect a container to a network
docker network disconnect my-net containerDisconnect a container
docker inspect containerInspect container configuration
docker port containerDisplay published ports

NAT and Masquerading : Docker bridge networks commonly use NAT/masquerading for outbound container traffic.

Container
172.18.0.2
     │
     â–¼
Docker Host
192.168.1.10
     │
     â–¼
Internet

EXPOSE vs -p

EXPOSE-p / --publish
Declares/documentation for a container portPublishes a port to the host
Does not itself publish the portCreates the host-to-container mapping
Usually appears in DockerfileUsed with docker run / docker create
Example: EXPOSE 80Example: -p 8080:80

Default Bridge vs User-Defined Bridge

FeatureDefault bridgeUser-defined bridge
Automatically createdYesNo
Container communicationYesYes
DNS by container nameNot normallyYes
Better isolationLimitedYes
Can connect/disconnect containers dynamicallyLimitedYes
Recommended for application stacksNoYes

Bridge Networks
│
├── Default bridge
│
└── User-defined bridge

Docker recommends user-defined bridge networks rather than relying on the default bridge for application stacks. User-defined bridges provide better isolation and automatic DNS-based container-name resolution. 


Bridge Network

The bridge driver is the default Docker network driver.

When Docker Engine starts on Linux, it creates a default bridge network. Containers started without specifying another network are connected to this default bridge.

Docker Host
│
├── docker0 / bridge
│
├── Container A
│      └── 172.x.x.x
│
├── Container B
│      └── 172.x.x.x
│
└── Container C
       └── 172.x.x.x

The bridge provides a virtual network through which containers can communicate.


Docker Network Drivers
Docker provides several built-in network drivers.


Network DriverPurposebridgeCommonly used for containers running on the same Docker hosthostRemoves network isolation between the container and Docker hostnoneCompletely disables container networkingoverlayConnects containers/services across multiple Docker hosts, particularly with SwarmmacvlanMakes containers appear as physical devices on the networkipvlanProvides control over IPv4/IPv6 addressing without assigning unique MAC addresses

docker3


Docker networking is primarily about connecting containers while controlling isolation, service discovery, routing, and access to external networks.