Docker Volume and Network Management: Persistent Storage and Network Configuration
Docker volume types (named, bind mount, tmpfs), volume management, Docker network modes (bridge, host, overlay), creating custom networks and container communication. Comprehensive guide.
Table of Contents
Docker Volume and Network Management
Docker containers are ephemeral by default — when a container is deleted, its data is lost too. Volumes solve this problem and ensure data persists independently of the container lifecycle. Docker networks manage communication between containers and with the outside world.
Docker Volume Types
| Type | Description | Use Case |
|---|---|---|
| Named Volume | Managed by Docker | Database, application data |
| Bind Mount | Mount host directory | Development, config files |
| tmpfs Mount | Memory-based temporary | Sensitive temporary data |
Named Volume Management
# Create a volume
docker volume create myapp-data
# List volumes
docker volume ls
# Inspect volume details
docker volume inspect myapp-data
# Remove a volume
docker volume rm myapp-data
# Remove unused volumes
docker volume prune
# Remove all volumes (careful!)
docker volume prune -a
Running Containers with Named Volumes
# Run MySQL with volume
docker run -d \
--name mysql-db \
-e MYSQL_ROOT_PASSWORD=StrongPass123! \
-e MYSQL_DATABASE=myapp \
-v mysql-data:/var/lib/mysql \
-p 3306:3306 \
mysql:8.0
# Run PostgreSQL with volume
docker run -d \
--name postgres-db \
-e POSTGRES_PASSWORD=StrongPass123! \
-e POSTGRES_DB=myapp \
-v postgres-data:/var/lib/postgresql/data \
-p 5432:5432 \
postgres:15
Using Bind Mounts
# Mount current directory into container
docker run -d \
--name nginx-web \
-v /home/user/website:/usr/share/nginx/html:ro \
-p 80:80 \
nginx:latest
# Bind mount for development
docker run -it \
--name node-dev \
-v $(pwd):/app \
-w /app \
-p 3000:3000 \
node:20 \
npm run dev
# Mount config file
docker run -d \
--name nginx-custom \
-v /etc/nginx/nginx.conf:/etc/nginx/nginx.conf:ro \
-p 80:80 \
nginx:latest
Volumes with Docker Compose
# docker-compose.yml
version: '3.8'
services:
db:
image: mysql:8.0
environment:
MYSQL_ROOT_PASSWORD: StrongPass123!
MYSQL_DATABASE: myapp
volumes:
- mysql-data:/var/lib/mysql
- ./init.sql:/docker-entrypoint-initdb.d/init.sql:ro
ports:
- "3306:3306"
app:
image: myapp:latest
volumes:
- app-uploads:/app/uploads
- ./config:/app/config:ro
depends_on:
- db
volumes:
mysql-data:
driver: local
app-uploads:
driver: local
Volume Backup and Restore
# Backup volume (tar archive)
docker run --rm \
-v mysql-data:/data \
-v $(pwd):/backup \
alpine tar czf /backup/mysql-backup.tar.gz -C /data .
# Restore volume
docker run --rm \
-v mysql-data:/data \
-v $(pwd):/backup \
alpine tar xzf /backup/mysql-backup.tar.gz -C /data
# Copy volume to another host
docker run --rm \
-v mysql-data:/data \
alpine tar czf - -C /data . | ssh user@remote-host 'docker run --rm -i -v mysql-data:/data alpine tar xzf - -C /data'
Docker Network Modes
Bridge Network (Default)
# Default bridge network
docker run -d --name web nginx
# Container gets 172.17.0.x IP
# Create custom bridge network
docker network create myapp-network
# Connect container to custom network
docker run -d \
--name web \
--network myapp-network \
nginx
docker run -d \
--name db \
--network myapp-network \
-e MYSQL_ROOT_PASSWORD=StrongPass123! \
mysql:8.0
# Containers on custom network can find each other by name
docker exec web ping db # Access via 'db' hostname
Host Network
# Run in host network mode (Linux only)
docker run -d \
--name nginx-host \
--network host \
nginx
# Container uses host's network interface directly
# No port mapping needed
None Network
# Container with no network access
docker run -d \
--name isolated \
--network none \
myapp
Docker Network Management
# List networks
docker network ls
# Inspect network details
docker network inspect myapp-network
# Create network with custom subnet
docker network create \
--driver bridge \
--subnet 172.20.0.0/16 \
--gateway 172.20.0.1 \
myapp-network
# Connect container to network
docker network connect myapp-network existing-container
# Disconnect container from network
docker network disconnect myapp-network existing-container
# Remove network
docker network rm myapp-network
# Remove unused networks
docker network prune
Networks with Docker Compose
# docker-compose.yml
version: '3.8'
services:
frontend:
image: nginx:latest
networks:
- frontend-net
ports:
- "80:80"
backend:
image: myapp:latest
networks:
- frontend-net
- backend-net
db:
image: mysql:8.0
networks:
- backend-net
environment:
MYSQL_ROOT_PASSWORD: StrongPass123!
networks:
frontend-net:
driver: bridge
backend-net:
driver: bridge
internal: true # No external access
Overlay Network (Docker Swarm)
# Initialize Swarm
docker swarm init
# Create overlay network
docker network create \
--driver overlay \
--attachable \
swarm-network
# Create service on overlay network
docker service create \
--name web \
--network swarm-network \
--replicas 3 \
nginx
Troubleshooting Container Communication
# Get container IP address
docker inspect CONTAINER_NAME | grep IPAddress
# Ping test from inside container
docker exec CONTAINER_NAME ping -c 3 OTHER_CONTAINER
# Curl test from inside container
docker exec CONTAINER_NAME curl http://other-service:8080/health
# View network connections
docker exec CONTAINER_NAME netstat -tulpn
# Test DNS resolution
docker exec CONTAINER_NAME nslookup db
# Network statistics
docker stats CONTAINER_NAME
Conclusion
Docker volume and network management are the cornerstones of production-grade container infrastructure. Store your data safely with named volumes, isolate your containers with custom bridge networks, and define your entire infrastructure as code with Docker Compose. With these configurations on REXE servers, you can build reliable and scalable container environments.
Related Articles
K3s Kubernetes Setup: Lightweight Container Orchestration
Single and multi-node Kubernetes cluster setup with K3s, kubectl usage, creating deployments and services, application management with Helm. Kubernetes guide for VPS environments.
Container Logging: Docker and Kubernetes Log Management
Docker log drivers, docker logs command, Kubernetes pod logs, centralized log collection (Loki, ELK Stack), log rotation, and production log management best practices.
Docker Security: Hardening Your Container Environment
Docker security best practices: non-root user, read-only filesystem, seccomp profiles, image scanning, Docker socket security, network isolation, and secrets management.