Networking in Docker: A Comprehensive Guide for Beginners

LFX'24 @Kyverno | Web Dev | DevOps | OpenSource | Exploring Cloud Native Technologies.
If you’ve been following along in this Docker journey, you already know the basics of containers, how to run and manage them, and how to keep your data safe using Docker volumes. But today, we’re diving into something even more fascinating—Networking in Docker.
Now, you might be thinking, "Networking sounds complicated—do I really need to know this?" The answer is a big YES! Networking is the secret sauce that makes containers talk to each other and to the outside world. Without it, your containers would be like isolated islands in the ocean—beautiful but useless.
In this post, we’re going to break down everything you need to know about Docker networking. We’ll cover:
The different types of Docker networks (bridge, host, and overlay).
How to create and manage networks.
The magic of exposing container ports.
How to make containers talk to each other.
By the end of this guide, you’ll feel like a Docker networking pro, even if you’re just starting. So, grab a coffee, and let’s dive in!
What Is Docker Networking?
Imagine this: You’ve built a container for your backend application, another one for your frontend, and a third one for your database. Now, how do they communicate? Enter Docker networking!
Docker networking is like the highway system for your containers. It allows them to exchange data seamlessly. Without networking, your app wouldn’t be able to connect to its database, and your frontend wouldn’t be able to serve your users.
Docker’s networking model is designed to be simple yet powerful. It supports everything from basic local setups to complex, distributed systems running across multiple machines.
Docker Network Modes
Docker provides several built-in networking modes. Each mode has its unique characteristics and use cases. Let’s break them down.
1. Bridge Network: The Default Option
When you create a container, Docker automatically connects it to a bridge network. Think of this as a private network for containers running on the same Docker host.
Here’s how it works:
Docker creates a virtual switch called
docker0on your host machine.Containers on the same bridge network can talk to each other using their names (e.g.,
frontendcan pingbackend).If you want a container to be accessible from outside, you need to map its ports to the host machine.
Example:
Let’s say you’re running a Node.js app in one container and MongoDB in another. By connecting them to the same bridge network, the app can talk to the database using the namemongo-db.bashCopyEditdocker run -d --name mongo-db --network bridge mongo docker run -d --name node-app --network bridge nodeYour app can now connect to MongoDB using the hostname
mongo-db.Pro Tip: The bridge network is great for small, local setups, but it doesn’t work if your containers are spread across multiple machines.
2. Host Network: Direct Access to the Host’s Network
In host mode, the container shares the host machine’s network stack. This means the container’s ports are directly mapped to the host’s ports.
This mode is super simple but comes with trade-offs. Since there’s no network isolation, your container has direct access to the host’s network.
Example: Let’s say you’re running Nginx in host mode. You don’t need to map any ports because the container will use the host’s network directly:
bashCopyEditdocker run --network host nginx
When to Use:
When you need super-low latency.
When you don’t care about isolating your container’s network from the host.
Watch Out: This mode isn’t ideal for production environments where security and isolation are important.
3. Overlay Network: The Superhero for Multi-Host Setups
If you’re running a distributed system (like a microservices app) across multiple Docker hosts, the overlay network is your best friend. It creates a virtual network that spans all your Docker hosts, allowing containers on different machines to communicate as if they’re on the same local network.
Example: If you’re using Docker Swarm, you can create an overlay network like this:
bashCopyEditdocker network create --driver overlay my_overlay_network
Containers in this network can now communicate, no matter which host they’re running on.
Pro Tip: Overlay networks are powerful but require a bit more setup, especially if you’re not using Docker Swarm.
Creating and Managing Docker Networks
Now that you understand the types of networks, let’s learn how to create and manage them.
1. Creating a Custom Network
To create your own network, use the docker network create command:
bashCopyEditdocker network create my_custom_network
By default, this creates a bridge network.
2. Listing Networks
To see all the networks on your host, use:
bashCopyEditdocker network ls
You’ll see something like this:
plaintextCopyEditNETWORK ID NAME DRIVER SCOPE
123abc456def bridge bridge local
789xyz012ghi host host local
345lmn678opq my_custom_network bridge local
3. Connecting Containers to a Network
When you create a container, you can specify which network it should connect to:
bashCopyEditdocker run --name my-container --network my_custom_network nginx
If you forget, don’t worry—you can connect an existing container to a network later:
bashCopyEditdocker network connect my_custom_network my-container
Exposing Container Ports
Let’s say you’re running a web server in a container. How do you make it accessible to the outside world? This is where port mapping comes in.
Publishing Ports
To publish a container’s port to the host, use the -p flag:
bashCopyEditdocker run -d -p 8080:80 nginx
Now, you can access your Nginx server at http://localhost:8080.
Connecting Multiple Containers
Imagine you’re building an app with a backend, frontend, and a database. How do you make them talk to each other? Simple—connect them to the same network!
Example: Python App with Redis
Create a custom network:
bashCopyEditdocker network create app_networkRun the Redis container:
bashCopyEditdocker run -d --name redis-db --network app_network redisRun your Python app container:
bashCopyEditdocker run -d --name python-app --network app_network python-app
Inside your Python app, you can now connect to Redis using the hostname redis-db.
Best Practices for Docker Networking
Use Custom Networks: This makes it easier to isolate and organize your containers.
Expose Only What’s Needed: Avoid exposing unnecessary ports to reduce security risks.
Keep It Simple: For small apps, bridge networks are usually enough. For distributed systems, use overlay networks.
Wrapping Up
Docker networking might seem daunting at first, but with a little practice, it becomes second nature. Whether you’re building a simple app or a distributed system, understanding networking is key to getting the most out of Docker.
Thanks for Reading!
I hope this article helped you understand Docker networking in a simple and practical way. If you found this helpful, follow me on Twitter and LinkedIn for more tech insights. Stay tuned for the next post in the series!


