Network Types
- bridge - Default, isolated network
- host - Use host's network directly
- none - No networking
- 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 IPAddressNetwork 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 webCustom 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 myappOn custom networks, containers can communicate by name:
bash
1# From web container
2ping db # Works!