G
GuideDevOps
Lesson 7 of 13

Docker Networking

Part of the Docker tutorial series.

Docker Networking allows containers to talk to each other, to the host, and to the outside world.

1. Default Network (Bridge)

By default, Docker uses the bridge network. All containers on this network can reach each other via their IP addresses.

List Networks

Action:

docker network ls

Result:

NETWORK ID     NAME      DRIVER    SCOPE
1a2b3c4d5e6f   bridge    bridge    local
7g8h9i0j1k2l   host      host      local
3m4n5o6p7q8r   none      null      local

2. User-Defined Bridge Networks (Recommended)

Containers on the same user-defined bridge can talk to each other using their names (automatic DNS resolution). This is how you connect a Web app to a Database.

Create and Use a Network

Action:

# 1. Create the network
docker network create app-net
 
# 2. Start the database on the network
docker run -d --name db --network app-net redis
 
# 3. Start the app on the same network
docker run -d --name web --network app-net nginx

Result:

app-net
f5e6d7c8b9a0...
a1b2c3d4e5f6...

Verify Connectivity

Action:

# Ping the 'db' container from the 'web' container using its name
docker exec web ping -c 2 db

Result:

PING db (172.18.0.2): 56 data bytes
64 bytes from 172.18.0.2: icmp_seq=0 ttl=64 time=0.082 ms
64 bytes from 172.18.0.2: icmp_seq=1 ttl=64 time=0.104 ms

3. Host Networking

In host mode, the container shares the host's network stack. There is no isolation—if the container listens on port 80, the host listens on port 80.

Best for: High-performance applications (no bridge overhead).


4. Port Mapping

To make a container reachable from outside the host, you must map a port.

Action:

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

Result: (Nginx is now accessible at http://localhost:8080)


Summary

  • Bridge: Standard isolation. Containers talk via IP or Name (if custom).
  • Host: No isolation. Container uses host ports directly.
  • None: No network access (secure/isolated).
  • Port Mapping: -p [HOST_PORT]:[CONTAINER_PORT]