🌐 Docker Networks Explained – A Complete Beginner’s Guide

Table of contents
- 🚀 1. What is a Docker Network?
- 💡 2. Why Do We Need Docker Networks?
- 🏗 3. Where Do We Use Docker Networks?
- 🧩 4. Types of Docker Networks
- 🌐 5. Working with Docker Networks
- 📚 6. Real-World Example – Web App + Database
- ⚡ 7. Pro Tips for Docker Networking
- 📌 8. Docker Networks in Docker Compose
- 🎯 Conclusion
- 🔗 What’s Next?
- 📝 Additional Resources
- ✍️ Author’s Note
- 💬 Have Questions or Suggestions?

When you first start working with Docker, you might think of it simply as a tool to package and run applications in containers. But if you ever want your containers to talk to each other or connect to the outside world, you’ll need to understand Docker Networking.
In this blog, we’ll cover everything you need to know about Docker networks — from what they are to how to use them like a pro. We’ll also explore real-world examples, commands, and best practices to help you become confident in networking your containers.
🚀 1. What is a Docker Network?
A Docker network is a virtual network created and managed by Docker to allow communication:
- Between containers
- Between containers and the host machine
- Between containers and the outside world (internet)
Think of it as a private bridge that connects your containers so they can exchange data securely and efficiently.
💡 2. Why Do We Need Docker Networks?
Docker containers are isolated by default. That’s great for security — but it also means containers can’t talk to each other unless we explicitly connect them.
Docker networks let us:
- Enable communication between multiple containers (e.g., app + database).
- Expose services to the outside world (e.g., make a web app accessible via a browser).
- Isolate environments (e.g., dev, test, and prod networks).
- Control traffic flow and security.
🏗 3. Where Do We Use Docker Networks?
Here are some common scenarios:
Web Application + Database
- Your backend container needs to connect to your database container.
Microservices Architecture
- Multiple services need to communicate internally without exposing them to the internet.
Local Development
- Simulating real-world environments where apps and databases interact.
🧩 4. Types of Docker Networks
Docker offers several network drivers (types), each serving a different purpose.
1. bridge (default)
- Purpose: Connects containers on the same Docker host.
- Scope: Local to a single machine.
- When to use: For multi-container apps running on the same host.
Example:
# Create a user-defined bridge network
docker network create my_bridge
# Run containers and attach them to the network
docker run -dit --name container1 --network my_bridge alpine
docker run -dit --name container2 --network my_bridge alpine
# Test connectivity
docker exec -it container1 ping container2
2. host
- Purpose: Removes network isolation — container shares the host's network.
- Scope: Local to a single machine.
- When to use: When performance is critical or when you need to use the host’s IP directly.
Example:
docker run -d --network host nginx
# Now accessible on host's IP: http://localhost
3. none
- Purpose: Completely disables networking for the container.
- When to use: For fully isolated workloads or security reasons.
Example:
docker run -dit --network none alpine
4. overlay
- Purpose: Connects containers across multiple Docker hosts (used in Docker Swarm).
- Scope: Cluster-wide.
- When to use: For distributed systems or multi-node deployments.
5. macvlan
- Purpose: Assigns a MAC address to the container, making it appear as a physical device on your network.
- When to use: When containers need direct access to the physical network.
6. custom network plugins
- Purpose: Allows third-party networking solutions.
🌐 5. Working with Docker Networks
- List Networks: View all Docker networks on your system.
docker network ls
- Create Network: Create a new Docker network.
This command creates a new network that containers can use to communicate with each other. You can specify additional options such as the network driver (e.g.,docker network create <network_name> # Example: docker network create my_network
bridge
,overlay
,null
etc.) using the--driver
flag:docker network create --driver bridge my_network # Example: docker network create --driver bridge my_network
- Connect Container to Network: Attach a container to a specific network.
This command connects a running container to a specified network, allowing it to communicate with other containers on that network. If the container is not running, you can use thedocker network connect <network_name> <container_name or container_id> # Example: docker network connect my_network myapp_container
--alias
option to assign an alias for the container on the network:
This allows you to refer to the container by the alias within the network, making it easier to manage and communicate with multiple containers.docker network connect --alias my_alias my_network myapp_container # Example: docker network connect --alias my_alias my_network myapp_container
Disconnect Container from Network: Remove a container from a network.
docker network disconnect <network_name> <container_name or container_id> # Example: docker network disconnect my_network myapp_container
This command disconnects a running container from a specified network, preventing it from communicating with other containers on that network. If the container is not running, you can still disconnect it using the same command.
Inspect Network: View detailed information about a network.
docker network inspect <network_name> # Example: docker network inspect my_network
This command provides detailed information about the specified network, including its configuration, connected containers, and IP address ranges. The output is in JSON format, which can be useful for debugging or understanding how the network is set up. You can also use the
--format
option to filter the output and display specific information. For example, to get the list of connected containers:docker network inspect --format '{{ range .Containers }}{{ .Name }} {{ end }}' <network_name> # Example: docker network inspect --format '{{ range .Containers }}{{ .Name }} {{ end }}' my_network
Remove Network: Delete a Docker network.
docker network rm <network_name> # Example: docker network rm my_network
This command removes a specified network from your system. You can only remove networks that are not currently in use by any containers. If you try to remove a network that is still connected to one or more containers, Docker will return an error. To forcefully remove a network and disconnect all connected containers, you can use the
-f
flag:docker network rm -f <network_name> # Example: docker network rm -f my_network
- List Containers in a Network: View all containers connected to a specific network.
This command lists all containers that are currently connected to the specified network. The output will show the names of the containers, making it easy to see which containers are part of the network. If you want to see more details about each container, you can modify thedocker network inspect <network_name> --format '{{ range .Containers }}{{ .Name }} {{ end }}' # Example: docker network inspect my_network --format '{{ range .Containers }}{{ .Name }} {{ end }}'
--format
option to include additional information, such as the container ID or IP address:docker network inspect <network_name> --format '{{ range .Containers }}{{ .Name }} ({{ .ID }}) - {{ .IPv4Address }} {{ end }}' # Example: docker network inspect my_network --format '{{ range .Containers }}{{ .Name }} ({{ .ID }}) - {{ .IPv4Address }} {{ end }}'
- Remove all unused networks: Clean up networks that are not in use by any containers.
This command removes all networks that are not currently in use by any containers. It helps keep your Docker environment clean and free of unused resources. Docker will prompt you for confirmation before proceeding with the removal. If you want to skip the confirmation prompt, you can use thedocker network prune
-f
flag:
This will forcefully remove all unused networks without asking for confirmation.docker network prune -f
- List Containers in a Network: View all containers connected to a specific network.
📚 6. Real-World Example – Web App + Database
Let’s connect a Node.js app with a MongoDB database using Docker networks.
Step 1 – Create a bridge network
docker network create app_network
Step 2 – Run MongoDB container
docker run -d --name mydb --network app_network mongo
Step 3 – Run Node.js container
docker run -d --name myapp --network app_network my-node-image
Now, inside your Node.js app, you can connect to MongoDB using:
mongodb://mydb:27017
Notice: We use container name (
mydb
) instead of IP address. Docker’s internal DNS handles the resolution.
⚡ 7. Pro Tips for Docker Networking
- Use user-defined networks instead of the default
bridge
— they give you container name resolution. - Don’t hardcode IP addresses — use container names.
- Use
docker-compose
to simplify multi-container networking. - For production, restrict open ports using firewall rules.
- Monitor network usage with:
docker stats
📌 8. Docker Networks in Docker Compose
With docker-compose
, networking becomes automatic — all services are connected to the same network unless specified.
Example docker-compose.yml
:
version: "3"
services:
db:
image: mongo
app:
build: .
depends_on:
- db
Here, app
can reach db
just by using the hostname db
.
🎯 Conclusion
Docker networks are the backbone of container communication. Whether you’re connecting a simple app to a database or orchestrating a fleet of microservices, understanding Docker networking is essential for building scalable and secure containerized systems.
✅ Key Takeaways:
- Docker networks connect containers internally and externally.
- Use user-defined networks for better control and DNS resolution.
- Choose the right network type (
bridge
,host
,none
, etc.) based on your needs. - Networking in
docker-compose
is simple and powerful.
🔗 What’s Next?
You might want to explore:
- Docker Command Cheat Sheet
- Everything About Docker Compose
- How to Build Docker Images with Dockerfile
- Docker Volumes Explained – The Complete Beginner-to-Pro Guide
- Real-Time Development with Docker Volumes (Bind Mounts)
📝 Additional Resources
✍️ Author’s Note
This blog is a comprehensive guide to Docker Networking, but the best way to learn is by doing! Try building your own app using the examples provided. If you have any questions or suggestions, feel free to reach out.
💬 Have Questions or Suggestions?
Drop a comment below or connect with me on LinkedIn or GitHub. Let’s make apps faster together! 🚀
Subscribe to my newsletter
Read articles from KUNTAL MAITY directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

KUNTAL MAITY
KUNTAL MAITY
I’m a passionate Full-Stack Developer who loves building performance-driven web and mobile applications. I work primarily with JavaScript, React, Next.js, Node.js, and MongoDB, and I enjoy simplifying complex concepts into developer-friendly tutorials. On this blog, you'll find hands-on guides, real-world projects, and developer insights—all aimed at helping you level up your coding skills and build production-ready apps. Whether you're into backend performance, frontend polish, or full-stack architecture, there's something here for you! Let’s learn, build, and grow together. 💻✨