Understanding Docker Networks: A Guide with Examples
Why Containers Can't Talk Without a Network
Default Isolation: Docker containers are isolated by default. This means each container runs separately from others, similar to how different houses on the same street are separate.
No Common Network: Just like houses need a road to connect them, containers need a network to communicate. Without a common network, containers can't find or talk to each other.
Service Discovery: Containers need a way to discover or find each other, just like people need addresses to find houses. Without a network, there's no system for this discovery.
How Docker Networks Fix This
Common Network: Docker networks connect containers like roads connect houses. Containers on the same network can communicate with each other.
DNS Resolution: Docker networks have built-in DNS (Domain Name System), which helps containers find each other using their names.
Network Isolation: Docker networks ensure only containers on the same network can talk to each other, enhancing security.
Basic Docker Network Commands
Here are some basic Docker network commands, explained step-by-step:
Create a Network:
docker network create mynetwork
docker
: The Docker command.network
: Tells Docker we are working with networks.create
: We want to create something.mynetwork
: The name of the network we are creating.
List Networks:
docker network ls
docker
: The Docker command.network
: We are working with networks.ls
: List all existing networks.
Inspect a Network:
docker network inspect mynetwork
docker
: The Docker command.network
: We are working with networks.inspect
: Show detailed information.mynetwork
: The name of the network we want details about.
Connect a Container to a Network:
docker network connect mynetwork mycontainer
docker
: The Docker command.network
: We are working with networks.connect
: We want to connect something.mynetwork
: The name of the network.mycontainer
: The name of the container we want to connect.
Disconnect a Container from a Network:
docker network disconnect mynetwork mycontainer
docker
: The Docker command.network
: We are working with networks.disconnect
: We want to disconnect something.mynetwork
: The name of the network.mycontainer
: The name of the container we want to disconnect.
Remove a Network:
docker network rm mynetwork
docker
: The Docker command.network
: We are working with networks.rm
: Remove something.mynetwork
: The name of the network to remove.
How to Use Docker Networks with an Example (Using MongoDB)
Step-by-Step Guide with MongoDB Example
Let's see an example of how to use Docker networks with MongoDB (a database) and Mongo Express (a web-based interface for MongoDB).
Step 1: Install Docker
Make sure Docker is installed on your system. You can download and install Docker from here.
Step 2: Create a Docker Network
Create a network that our containers will use to communicate.
docker network create mynetwork
- This command creates a network named "mynetwork".
Step 3: Run MongoDB Container in the Network
Start a MongoDB container and connect it to the network.
docker run -d --name mongodb --network mynetwork mongo
run
: Start a new container.-d
: Run the container in the background (detached mode).--name mongodb
: Name the container "mongodb".--network mynetwork
: Connect this container to the "mynetwork" network.mongo
: Use the official MongoDB image.
Step 4: Run Mongo Express Container in the Same Network
Start a Mongo Express container and connect it to the same network.
docker run -d --name mongo-express --network mynetwork -p 8081:8081 -e ME_CONFIG_MONGODB_SERVER=mongodb mongo-express
run
: Start a new container.-d
: Run the container in the background (detached mode).--name mongo-express
: Name the container "mongo-express".--network mynetwork
: Connect this container to the "mynetwork" network.-p 8081:8081
: Map port 8081 on the host to port 8081 in the container.-e ME_CONFIG_MONGODB_SERVER=mongodb
: Set the environment variable to point to our MongoDB container.mongo-express
: Use the official Mongo Express image.
Mongo Express Environment Variables
Here's a brief explanation of the environment variable used:
ME_CONFIG_MONGODB_SERVER=mongodb
: This sets the MongoDB server address to "mongodb", which is the name of our MongoDB container. Docker's DNS resolution will allow Mongo Express to find and connect to the MongoDB container using this name.
Step 5: Access Mongo Express
Open your browser and go to http://localhost:8081
. You should see the Mongo Express interface, connected to your MongoDB instance.
Summary
Docker networks are like virtual roads connecting containers, allowing them to communicate. They provide isolation, security, and flexibility, making it easier to manage containerized applications. In this guide, we showed how to create and use Docker networks with MongoDB and Mongo Express, explaining each command in detail.
I hope this guide helps you get started with Docker networks. Happy Dockering!
Subscribe to my newsletter
Read articles from Hitesh Mishra directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Hitesh Mishra
Hitesh Mishra
I am Hitesh Mishra, currently pursuing my bachelor of technology in computer science engineering from Graphic Era Hill University, Dehradun. I am always ready to have new experiences, meet new people and learn new things. 🔹Passionate about coding(problem solving) and web development. Currently learning web development. I have also created some projects like a full stack ecommerce website, full blog in which a user can create, edit, delete and update blog posts, forkfiy project in which users can find a recipe and instructions on how to make them! 🔹Currently learning ReactJS 🔹SKILLS: HTML, CSS, JAVASCRIPT, NODEJS, EXPRESSJS, MYSQL, MONGODB, C, C++. 🔹Check out my GitHub profile: https://github.com/hiteshmishra2103/ 🔹Linkedin: https://www.linkedin.com/in/hiteshmishra21/ Want to contact me?.📞 Just leave me a message on linkedin.