Docker Networking
Docker networking allows containers to communicate with each other and with the outside world.
Network Types
1. Bridge (Default)
# Default bridge network
docker run -d --name web nginx
docker run -d --name api myapp
# Both containers can communicate
docker exec web ping api
# Create a custom bridge network
docker network create myapp-net
# Run containers on the custom network
docker run -d --name web --network myapp-net nginx
docker run -d --name api --network myapp-net myapp
2. Host
# The container shares the host network
docker run -d --name web --network host nginx
# The web server runs on port 80 of the host
3. None
# No network
docker run -d --name isolated --network none myapp
4. Overlay
# For Docker Swarm
docker network create --driver overlay myapp-overlay
Network Commands
# List all networks
docker network ls
# View network details
docker network inspect bridge
# Create a new network
docker network create myapp-net
# Delete a network
docker network rm myapp-net
# Connect a container to a network
docker network connect myapp-net webcontainer
# Disconnect a container from a network
docker network disconnect myapp-net webcontainer
Service Discovery
# On a custom network, containers can resolve names
docker run -d --name api --network myapp-net myapp
docker run -d --name web --network myapp-net nginx
# The web container can access the api using the hostname "api"
curl http://api:3000
Use Case: Application Network
# docker-compose.yml
version: '3.8'
services:
frontend:
image: nginx:alpine
networks:
- frontend-net
- backend-net
ports:
- "80:80"
api:
build: ./backend
networks:
- backend-net
- db-net
environment:
DB_HOST: database
database:
image: postgres:15
networks:
- db-net
environment:
POSTGRES_DB: myapp
networks:
frontend-net:
backend-net:
db-net:
Port Mapping
# Map a host port to a container
docker run -d -p 8080:80 nginx
# Map to a specific IP
docker run -d -p 127.0.0.1:8080:80 nginx
# Map a UDP port
docker run -d -p 53:53/udp dns-server
# Map multiple ports
docker run -d -p 80:80 -p 443:443 nginx
DNS Configuration
# Custom DNS server
docker run -d --dns 8.8.8.8 myapp
# Custom DNS search
docker run -d --dns-search=example.com myapp
# Custom hosts
docker run -d --add-host=api:192.168.1.100 myapp
Everyday Use Case
Case: A developer needs to access the application from outside.
# Run the application with port mapping
docker run -d \
--name myapp-api \
-p 8080:3000 \
-e DB_HOST=database \
--network myapp-net \
myapp-api:1.0
# Access from the browser
curl http://localhost:8080
Network Security
# Isolated network
docker network create --internal internal-net
# Driver options
docker network create \
--driver bridge \
--subnet 192.168.1.0/24 \
--gateway 192.168.1.1 \
myapp-secure
Troubleshooting
Containers cannot communicate
# Make sure they are on the same network
docker network inspect myapp-net
# Check DNS resolution
docker exec web ping api
# Check the open port
docker exec web nc -zv api 3000
Port conflict
# Check which port is in use
lsof -i :8080
# Use a different port
docker run -d -p 9090:80 nginx
Best Practices
- Use a custom network: For isolation and DNS
- Separate concerns: Frontend, backend, and database on different networks
- Don't expose unneeded ports: For security
- Use internal networks: For internal services
- Monitor networks:
docker network inspect
Use Case: Microservices
# docker-compose.yml
version: '3.8'
services:
gateway:
image: nginx:alpine
networks:
- public
ports:
- "80:80"
auth-service:
build: ./auth
networks:
- public
- internal
user-service:
build: ./user
networks:
- internal
database:
image: postgres:15
networks:
- internal
networks:
public:
internal:
internal: true
Conclusion
Docker networking enables communication between containers and with the outside world. For applications, use custom networks for isolation and better DNS resolution.