Chapter 5: Docker Networking & Storage
Authored by syscook.dev
What are Docker Networking and Storage?
Docker networking enables communication between containers, hosts, and external networks. Docker storage provides persistent data storage for containers using volumes and bind mounts. Together, they form the foundation for containerized application data flow and persistence.
Key Concepts:
- Network: Communication layer between containers and external systems
- Volume: Named storage managed by Docker
- Bind Mount: Direct mapping of host directory to container
- Network Driver: How containers communicate (bridge, host, overlay)
- Volume Driver: How data is stored (local, nfs, cloud)
- Data Persistence: Ensuring data survives container lifecycle
Why Use Docker Networking and Storage?
1. Enable Container Communication
Networking allows containers to communicate with each other and external services.
# Create custom network
docker network create my-network
# Run containers on same network
docker run -d --name web --network my-network nginx
docker run -d --name db --network my-network postgres
# Containers can communicate using names
docker exec web curl http://db:5432
Benefits:
- Service discovery using container names
- Isolated network segments
- Load balancing and service mesh capabilities
2. Persist Application Data
Storage ensures data survives container restarts and updates.
# Create named volume
docker volume create my-data
# Mount volume to container
docker run -d --name db \
-v my-data:/var/lib/postgresql/data \
postgres:13
# Data persists even if container is removed
docker rm db
docker run -d --name db-new \
-v my-data:/var/lib/postgresql/data \
postgres:13
3. Secure and Isolate Applications
Networking and storage provide security boundaries and data isolation.
# Isolated network for sensitive services
docker network create --internal secure-network
# Encrypted volume for sensitive data
docker volume create --driver local \
--opt type=tmpfs \
--opt device=tmpfs \
--opt o=size=1g,uid=1000 \
secure-data
Docker Networking Deep Dive
1. Network Types and Drivers
Bridge Network (Default)
# Create bridge network
docker network create --driver bridge my-bridge
# Run containers on bridge network
docker run -d --name web --network my-bridge nginx
docker run -d --name api --network my-bridge node:18
# Containers can communicate using names
docker exec web curl http://api:3000
Bridge Network Features:
- Default network driver
- Containers can communicate by name
- Isolated from host network
- Automatic DNS resolution
Host Network
# Use host network
docker run -d --name web --network host nginx
# Container uses host's network directly
# No port mapping needed
curl http://localhost:80
Host Network Features:
- Container shares host's network stack
- No network isolation
- Better performance
- Direct access to host ports
Overlay Network (Swarm)
# Create overlay network for swarm
docker network create --driver overlay my-overlay
# Deploy service on overlay network
docker service create --name web \
--network my-overlay \
--replicas 3 \
nginx
Overlay Network Features:
- Multi-host networking
- Encrypted communication
- Service discovery across hosts
- Load balancing
2. Network Configuration
Custom Network with Options
# Create network with custom subnet
docker network create \
--driver bridge \
--subnet=172.20.0.0/16 \
--ip-range=172.20.240.0/20 \
--gateway=172.20.0.1 \
my-custom-network
# Run container with specific IP
docker run -d --name web \
--network my-custom-network \
--ip 172.20.0.10 \
nginx
Network with External Connectivity
# Create network with external access
docker network create \
--driver bridge \
--opt com.docker.network.bridge.enable_icc=true \
--opt com.docker.network.bridge.enable_ip_masquerade=true \
--opt com.docker.network.bridge.host_binding_ipv4=0.0.0.0 \
my-external-network
3. Service Discovery and Load Balancing
DNS-Based Service Discovery
# Create network
docker network create app-network
# Run multiple web services
docker run -d --name web1 --network app-network nginx
docker run -d --name web2 --network app-network nginx
docker run -d --name web3 --network app-network nginx
# Run load balancer
docker run -d --name lb --network app-network \
-p 80:80 \
nginx:alpine
# Configure load balancer
docker exec lb sh -c 'cat > /etc/nginx/nginx.conf << EOF
events {}
http {
upstream backend {
server web1:80;
server web2:80;
server web3:80;
}
server {
listen 80;
location / {
proxy_pass http://backend;
}
}
}
EOF'
Health Checks and Service Discovery
# Run service with health check
docker run -d --name web \
--network app-network \
--health-cmd="curl -f http://localhost:80 || exit 1" \
--health-interval=30s \
--health-timeout=10s \
--health-retries=3 \
nginx