Docker Networking and Storage: Introduction and Setup

Docker networking and storage are fundamental concepts for building scalable, production-ready containerized applications. This guide covers everything from basic concepts to advanced patterns for managing container connectivity and data persistence.

Docker Networking Fundamentals

Network Types Overview

Docker provides several network drivers:

┌─────────────────────────────────────────────────────────┐
│                    Docker Host                          │
├─────────────────────────────────────────────────────────┤
│  ┌─────────────┐  ┌─────────────┐  ┌─────────────┐     │
│  │   Bridge    │  │    Host     │  │    None     │     │
│  │  Network    │  │  Network    │  │  Network    │     │
│  └─────────────┘  └─────────────┘  └─────────────┘     │
├─────────────────────────────────────────────────────────┤
│  ┌─────────────┐  ┌─────────────┐  ┌─────────────┐     │
│  │   Overlay   │  │   Macvlan   │  │   Custom    │     │
│  │  Network    │  │  Network    │  │  Network    │     │
│  └─────────────┘  └─────────────┘  └─────────────┘     │
└─────────────────────────────────────────────────────────┘

Basic Network Commands

# List networks
docker network ls

# Inspect network
docker network inspect bridge

# Create custom network
docker network create mynetwork

# Create network with specific driver
docker network create --driver bridge mybridge

# Create network with custom subnet
docker network create --subnet=172.20.0.0/16 mysubnet

# Connect container to network
docker network connect mynetwork mycontainer

# Disconnect container from network
docker network disconnect mynetwork mycontainer

# Remove network
docker network rm mynetwork

Bridge Networks

Default bridge network:

# Run container on default bridge
docker run -d --name web nginx

# Run container with port mapping
docker run -d --name web -p 8080:80 nginx

# Inspect default bridge
docker network inspect bridge

Custom bridge networks:

# Create custom bridge
docker network create --driver bridge \
  --subnet=172.20.0.0/16 \
  --ip-range=172.20.240.0/20 \
  --gateway=172.20.0.1 \
  custom-bridge

# Run containers on custom bridge
docker run -d --name web --network custom-bridge nginx
docker run -d --name app --network custom-bridge alpine sleep 3600

# Test connectivity
docker exec app ping web  # Works with custom bridge

Docker Storage Fundamentals

Storage Types Overview

┌─────────────────────────────────────────────────────────┐
│                 Docker Storage                          │
├─────────────────────────────────────────────────────────┤
│  ┌─────────────┐  ┌─────────────┐  ┌─────────────┐     │
│  │   Volumes   │  │ Bind Mounts │  │   tmpfs     │     │
│  │ (Managed by │  │ (Host Path) │  │ (Memory)    │     │
│  │   Docker)   │  │             │  │             │     │
│  └─────────────┘  └─────────────┘  └─────────────┘     │
└─────────────────────────────────────────────────────────┘

Volume Management

# List volumes
docker volume ls

# Create volume
docker volume create myvolume

# Create volume with driver options
docker volume create --driver local \
  --opt type=nfs \
  --opt o=addr=192.168.1.1,rw \
  --opt device=:/path/to/dir \
  nfs-volume

# Inspect volume
docker volume inspect myvolume

# Remove volume
docker volume rm myvolume

# Remove unused volumes
docker volume prune

Using Volumes

# Mount named volume
docker run -d --name db \
  -v myvolume:/var/lib/mysql \
  mysql:8.0

# Mount bind mount
docker run -d --name web \
  -v /host/path:/container/path \
  nginx

# Mount with specific options
docker run -d --name app \
  -v myvolume:/data:ro \
  alpine sleep 3600

# Mount tmpfs
docker run -d --name temp \
  --tmpfs /tmp:rw,noexec,nosuid,size=100m \
  alpine sleep 3600

Network Configuration Examples

Multi-Container Application

# Create application network
docker network create app-network

# Database container
docker run -d --name database \
  --network app-network \
  -e MYSQL_ROOT_PASSWORD=rootpass \
  -e MYSQL_DATABASE=appdb \
  -e MYSQL_USER=appuser \
  -e MYSQL_PASSWORD=apppass \
  mysql:8.0

# Application container
docker run -d --name backend \
  --network app-network \
  -e DATABASE_URL=mysql://appuser:apppass@database:3306/appdb \
  myapp:backend

# Frontend container with port exposure
docker run -d --name frontend \
  --network app-network \
  -p 3000:3000 \
  -e API_URL=http://backend:8000 \
  myapp:frontend

# Test connectivity
docker exec frontend curl http://backend:8000/health
docker exec backend mysql -h database -u appuser -p appdb

Network Isolation

# Create isolated networks
docker network create --internal backend-network
docker network create frontend-network

# Database (backend only)
docker run -d --name db \
  --network backend-network \
  postgres:13

# API server (both networks)
docker run -d --name api \
  --network backend-network \
  myapp:api

docker network connect frontend-network api

# Web server (frontend only)
docker run -d --name web \
  --network frontend-network \
  -p 80:80 \
  nginx

# Database is not accessible from web server
docker exec web ping db  # This will fail

Storage Configuration Examples

Database with Persistent Storage

# Create volume for database
docker volume create postgres-data

# Run PostgreSQL with persistent storage
docker run -d --name postgres \
  -v postgres-data:/var/lib/postgresql/data \
  -e POSTGRES_DB=myapp \
  -e POSTGRES_USER=user \
  -e POSTGRES_PASSWORD=password \
  postgres:13

# Backup database
docker exec postgres pg_dump -U user myapp > backup.sql

# Restore database
docker exec -i postgres psql -U user myapp < backup.sql

Application with Configuration

# Create volumes
docker volume create app-data
docker volume create app-config

# Run application with multiple mounts
docker run -d --name myapp \
  -v app-data:/app/data \
  -v app-config:/app/config \
  -v /host/logs:/app/logs \
  -v /etc/localtime:/etc/localtime:ro \
  myapp:latest

# Initialize configuration
docker exec myapp cp /app/config.template.json /app/config/config.json

Shared Storage Between Containers

# Create shared volume
docker volume create shared-data

# Writer container
docker run -d --name writer \
  -v shared-data:/data \
  alpine sh -c 'while true; do echo "$(date)" >> /data/log.txt; sleep 10; done'

# Reader container
docker run -d --name reader \
  -v shared-data:/data:ro \
  alpine sh -c 'while true; do tail -f /data/log.txt; sleep 1; done'

# Monitor shared data
docker logs reader

Docker Compose Integration

Network and Storage in Compose

version: '3.8'

services:
  web:
    image: nginx
    ports:
      - "80:80"
    volumes:
      - ./html:/usr/share/nginx/html:ro
      - web-logs:/var/log/nginx
    networks:
      - frontend
    depends_on:
      - api

  api:
    build: ./api
    volumes:
      - api-data:/app/data
      - ./config:/app/config:ro
    networks:
      - frontend
      - backend
    environment:
      - DATABASE_URL=postgresql://user:pass@db:5432/myapp
    depends_on:
      - db

  db:
    image: postgres:13
    volumes:
      - postgres-data:/var/lib/postgresql/data
      - ./init.sql:/docker-entrypoint-initdb.d/init.sql:ro
    networks:
      - backend
    environment:
      - POSTGRES_DB=myapp
      - POSTGRES_USER=user
      - POSTGRES_PASSWORD=pass

volumes:
  postgres-data:
  api-data:
  web-logs:

networks:
  frontend:
    driver: bridge
  backend:
    driver: bridge
    internal: true

Advanced Network Configuration

version: '3.8'

services:
  app:
    image: myapp
    networks:
      app-network:
        ipv4_address: 172.20.0.10
        aliases:
          - api-server
          - backend

  db:
    image: postgres:13
    networks:
      app-network:
        ipv4_address: 172.20.0.20

networks:
  app-network:
    driver: bridge
    ipam:
      config:
        - subnet: 172.20.0.0/16
          gateway: 172.20.0.1
    driver_opts:
      com.docker.network.bridge.name: app-bridge
      com.docker.network.bridge.enable_icc: "true"
      com.docker.network.bridge.enable_ip_masquerade: "true"

Troubleshooting Common Issues

Network Connectivity

# Check container networking
docker exec container ip addr show
docker exec container ip route show
docker exec container netstat -tlnp

# Test connectivity between containers
docker exec container1 ping container2
docker exec container1 telnet container2 port
docker exec container1 nslookup container2

# Inspect network configuration
docker network inspect network-name
docker inspect container-name | grep -A 20 NetworkSettings

Storage Issues

# Check volume mounts
docker inspect container-name | grep -A 10 Mounts

# Check volume usage
docker system df
docker volume ls
docker volume inspect volume-name

# Check file permissions
docker exec container ls -la /mount/point
docker exec container id  # Check user/group

# Debug storage issues
docker run --rm -v volume-name:/data alpine ls -la /data
docker run --rm -v /host/path:/data alpine ls -la /data

Performance Monitoring

# Monitor network traffic
docker exec container netstat -i
docker exec container ss -tuln

# Monitor storage I/O
docker exec container iostat -x 1
docker exec container df -h

# Container resource usage
docker stats
docker exec container top

Summary

In this introduction, you’ve learned:

Networking Fundamentals

  • Network Types: Bridge, host, overlay, and custom networks
  • Network Commands: Creating, managing, and troubleshooting networks
  • Container Connectivity: Service discovery and inter-container communication

Storage Fundamentals

  • Storage Types: Volumes, bind mounts, and tmpfs
  • Volume Management: Creating, mounting, and managing persistent storage
  • Data Persistence: Database storage and application data management

Practical Applications

  • Multi-Container Apps: Network isolation and service communication
  • Docker Compose: Declarative network and storage configuration
  • Troubleshooting: Common issues and debugging techniques

Key Concepts Mastered

  • Network Isolation: Separating frontend and backend services
  • Data Persistence: Ensuring data survives container restarts
  • Service Discovery: Container-to-container communication
  • Configuration Management: Mounting config files and secrets

Next Steps: Part 2 explores core concepts including advanced networking patterns, storage drivers, and performance optimization techniques that form the foundation of production-ready Docker deployments.