🚀 Docker for DevOps – Part 3: Networking, Volumes & Docker Compose (Beginner to Advanced)

In our previous blogs, we covered:
Today, we’ll dive deeper into Docker Networking, Volumes, and Docker Compose – essential concepts for running real-world applications in containers.
📌 Why is Networking Important in Docker?
When you run multiple containers (for example, an app container and a database container), they need to communicate with each other. Docker provides virtual networking that isolates containers but also allows controlled communication.
🔹 Types of Docker Networks
Docker provides several network drivers:
1. Bridge Network (Default)
Default network for standalone containers.
Containers can communicate using container names.
IPs are private (not accessible from outside).
✅ Command to list networks:
docker network ls
✅ Create a custom bridge network:
docker network create my_bridge_network
✅ Run containers on this network:
docker run -d --name container1 --network my_bridge_network nginx
docker run -d --name container2 --network my_bridge_network alpine sleep 5000
✅ Test connectivity:
docker exec -it container2 ping container1
2. Host Network
Shares the host machine’s network stack.
No isolation from the host.
✅ Run container with host network:
docker run -d --network host nginx
3. None Network
No networking for the container.
Useful for security-sensitive workloads.
✅ Run container with no network:
docker run -d --network none nginx
4. Overlay Network
Used in Docker Swarm or multi-host setups.
Allows containers on different hosts to communicate.
✅ Create overlay network:
docker network create --driver overlay my_overlay
📌 Inspecting a Network
docker network inspect bridge
This will show connected containers, IP addresses, and subnet details.
🔍 Example: Web App + Database with Networking
# Create network
docker network create app_network
# Run MySQL container
docker run -d --name mydb --network app_network -e MYSQL_ROOT_PASSWORD=root mysql:8
# Run App container
docker run -d --name myapp --network app_network myapp-image
Now myapp
can connect to mydb
using hostname mydb
.
📌 Docker Volumes – Persisting Data
By default, when you remove a container, data is lost. Volumes solve this problem by storing data outside the container’s filesystem.
Types of Volumes
Anonymous Volumes – Created automatically by Docker.
Named Volumes – You name it and reuse it.
Bind Mounts – Mount a specific host directory into the container.
Create & Use a Named Volume
docker volume create my_volume
docker run -d --name mysql_container \
--mount source=my_volume,target=/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=root mysql:8
✅ List volumes:
docker volume ls
✅ Inspect a volume:
docker volume inspect my_volume
✅ Remove a volume:
docker volume rm my_volume
📌 Docker Compose – Multi-Container Setup Made Easy
Instead of running multiple docker run
commands, Docker Compose lets you define all services in one file.
Example docker-compose.yml
version: '3'
services:
db:
image: mysql:8
environment:
MYSQL_ROOT_PASSWORD: root
volumes:
- db_data:/var/lib/mysql
networks:
- app_network
app:
image: myapp-image
depends_on:
- db
networks:
- app_network
networks:
app_network:
volumes:
db_data:
✅ Start containers:
docker-compose up -d
✅ Stop containers:
docker-compose down
🔥 Important Docker Commands for Networking & Volumes
# List networks
docker network ls
# Create network
docker network create my_network
# Connect container to network
docker network connect my_network my_container
# List volumes
docker volume ls
# Create volume
docker volume create my_volume
# Remove volume
docker volume rm my_volume
🧠 Common Docker Interview Questions (Part 3)
1. What is the difference between bridge and host network in Docker?
Bridge: Isolated network; containers communicate using IP/hostname.
Host: Container shares host’s network; no isolation.
2. How can two containers communicate with each other?
- By connecting both to the same Docker network and using container names.
3. What is the difference between a Docker Volume and a Bind Mount?
Volume: Managed by Docker, stored in
/var/lib/docker/volumes/
.Bind Mount: Maps a host directory into a container.
4. What is Docker Compose and why is it used?
- Tool to define and manage multi-container applications using a YAML file.
5. How do you persist MySQL data in Docker?
- Use Docker Volumes or Bind Mounts.
✅ What’s Next?
In the next part, we will cover Docker Compose in depth with real-world examples.
👉 Previous Parts:
Subscribe to my newsletter
Read articles from Harshal Sonar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
