Docker Networking – Connecting Containers Like a Pro

1. Introduction
Docker networking allows containers to communicate with each other and the outside world.
Think of it like Wi-Fi for containers 📡—it enables seamless communication between services running in different containers.
In this guide, you'll learn how Docker networking works, how to expose ports, and how to connect multiple containers using a custom network. 🚀
2. Basics of Docker Networking
Docker provides different networking options:
Network Type | Description |
Bridge (default) | Containers on the same bridge network can communicate. |
Host | Containers use the host machine’s network directly. |
None | Completely isolates the container from any network. |
Custom | User-defined networks for better container-to-container communication. |
3. Default Docker Bridge Network
By default, Docker containers run on the bridge network, allowing communication between containers on the same host.
Example: Running a Container on the Default Network
docker run -d --name mynginx nginx
Now, inspect the network:
docker network inspect bridge
✅ You’ll see that the container is connected to the default bridge network.
4. How to Expose Ports & Access Containers from the Host
By default, containers cannot be accessed from the host unless ports are exposed.
Exposing a Port
docker run -d -p 8080:80 nginx
Now, open http://localhost:8080 in your browser to access the container. 🎉
Flag | Purpose |
-p 8080:80 | Maps port 80 (inside container) to 8080 (host machine). |
5. Creating and Using Custom Networks
For better container-to-container communication, create a custom network.
Step 1: Create a Network
docker network create mynetwork
Step 2: Run Containers on This Network
docker run -d --name webserver --network mynetwork nginx
docker run -d --name appserver --network mynetwork busybox sleep 3600
Step 3: Verify the Network
docker network inspect mynetwork
✅ Both containers can now communicate using their container names instead of IP addresses.
6. Connecting Multiple Containers Using a Docker Network
Let’s connect a web app container to a database container using a Docker network.
Step 1: Create a Network
docker network create myappnetwork
Step 2: Run a Database Container
docker run -d --name mydb --network myappnetwork -e MYSQL_ROOT_PASSWORD=root mysql
Step 3: Run a Web App Container
docker run -d --name myapp --network myappnetwork my-web-app
Step 4: Access the Database from the App
Inside the myapp container, connect to mydb using:
mysql -h mydb -u root -p
✅ The app can now communicate with the database securely inside the network! 🎉
7. Conclusion
Docker networking allows seamless container-to-container communication. Now you know how to:
✅ Use the default bridge network
✅ Expose ports to access containers
✅ Create and connect containers on custom networks
Try setting up a multi-container network today! 🚀
Subscribe to my newsletter
Read articles from Pawan Luhana directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
