Docker Networking & Volumes
Learn how containers communicate with each other and the outside world, and how to persist data.Docker Networking
Docker provides several network drivers to control how containers communicate.1. Bridge Network (Default)
The default network driver. Containers on the same bridge network can communicate via IP address or container name (DNS).- Isolation: Containers on different bridge networks cannot communicate.
- Use Case: Standalone containers communicating on the same host.
2. Host Network
Removes network isolation between the container and the Docker host.- Performance: Best performance (no NAT).
- Port Conflicts: Container ports map directly to host ports.
3. None Network
Disables all networking.- Use Case: Security-sensitive batch jobs that don’t need network access.
DNS Resolution
Docker has an embedded DNS server.- On default
bridgenetwork: DNS by container name is NOT supported (only IP). - On custom networks: DNS by container name IS supported.
Docker Volumes
Containers are ephemeral. When they stop, their filesystem is lost. Volumes persist data.1. Named Volumes (Recommended)
Managed by Docker. Stored in/var/lib/docker/volumes/ (on Linux).
2. Bind Mounts
Maps a specific file or directory on the host to the container.- Use Case: Development (live reloading code), sharing config files.
3. Tmpfs Mounts
Stored in the host’s memory only. Never written to disk.- Use Case: Storing sensitive secrets or high-performance temporary data.
Managing Networks & Volumes
Key Takeaways
- Always use Custom Bridge Networks for inter-container communication.
- Use Named Volumes for database persistence.
- Use Bind Mounts for development (code reloading).
- Use Host Networking only when performance is critical and port conflicts aren’t an issue.
Docker Networking Deep Dive
How Bridge Networking Works
iptables Rules
Docker manipulates iptables for networking:DNS Resolution
Docker’s embedded DNS server (127.0.0.11):Overlay Networks (Multi-Host)
For containers across multiple Docker hosts (Swarm mode).How Overlay Works
Container Network Interface (CNI)
CNI is the standard for container networking plugins (used by Kubernetes).| Plugin | Features |
|---|---|
| Calico | Network policies, BGP routing |
| Cilium | eBPF-based, advanced security |
| Flannel | Simple overlay, easy setup |
| Weave | Encryption, multicast |
Volume Drivers
Beyond local storage, Docker supports pluggable volume drivers.| Driver | Description |
|---|---|
| local | Default, stores on host |
| nfs | Network File System |
| aws | AWS EBS/EFS |
| azure | Azure File/Disk |
| glusterfs | Distributed storage |
Docker Compose Networking
Network Aliases
Interview Questions & Answers
What are the Docker network drivers?
What are the Docker network drivers?
| Driver | Scope | Use Case |
|---|---|---|
| bridge | Single host | Default, container-to-container |
| host | Single host | No network isolation, max performance |
| none | Single host | No networking |
| overlay | Multi-host | Docker Swarm/K8s |
| macvlan | Single host | Container gets MAC address on LAN |
How does container-to-container communication work?
How does container-to-container communication work?
Same bridge network:
- Container A sends packet to Container B’s name
- Docker DNS resolves name to IP
- Packet goes through bridge interface
- iptables routes to Container B
- Cannot communicate directly
- Need a container connected to both networks, or use host network
What is the difference between named volumes and bind mounts?
What is the difference between named volumes and bind mounts?
| Aspect | Named Volume | Bind Mount |
|---|---|---|
| Location | Docker manages | You specify path |
| Portability | Portable | Tied to host |
| Backup | docker volume commands | Standard file tools |
| Performance | Better on non-Linux | Native |
| Use Case | Databases, persistent data | Development, configs |
How do you persist database data in Docker?
How do you persist database data in Docker?
Use named volumes:Important:
- Volume survives container deletion
- Back up with
docker run --volumes-from backup-container - For production, consider external volume drivers (NFS, cloud storage)
What is a macvlan network?
What is a macvlan network?
macvlan assigns a MAC address to the container, making it appear as a physical device on the network.Use Cases:
- Legacy apps requiring specific MAC addresses
- Apps needing to appear as physical hosts
- Direct LAN access without port mapping
How do you troubleshoot Docker networking?
How do you troubleshoot Docker networking?
Common Pitfalls
Next: Docker Compose →