Skip
Arish's avatar

20. Docker Networking Basics


Network Types

  1. bridge - Default, isolated network
  2. host - Use host's network directly
  3. none - No networking
  4. overlay - Multi-host (Swarm)

Default Bridge Network

bash
1# Containers can communicate via IP
2docker run -d --name web nginx
3docker run -d --name api myapi
4
5# Get IP
6docker inspect web | grep IPAddress

Network Commands

bash
1# List networks
2docker network ls
3
4# Create network
5docker network create mynetwork
6
7# Inspect
8docker network inspect mynetwork
9
10# Connect container
11docker network connect mynetwork web
12
13# Disconnect
14docker network disconnect mynetwork web

Custom Bridge Network

bash
1# Create
2docker network create --driver bridge app-network
3
4# Run containers on network
5docker run -d --network app-network --name db postgres
6docker run -d --network app-network --name web myapp

On custom networks, containers can communicate by name:

bash
1# From web container
2ping db  # Works!