Table of contents
- ๐ Introduction
- ๐ข Impact in Real Life
- ๐ Checking available networks in the system
- โ๏ธ Types of Docker Networking
- ๐ Managing Networks
- ๐ Connecting Containers to Networks
- ๐ก Connecting Containers to Containers
- ๐ป Host Networking
- ๐ซ None Networking
- ๐คฆโโ๏ธ Removing Containers from Networks
- ๐๐Conclusion:
๐ Introduction
Docker is a containerization platform that uses OS-level virtualization to package software applications and their dependencies into reusable units called containers. Docker containers can be run on any host with Docker or an equivalent container runtime installed, whether locally on your laptop or in a remote cloud.
Docker includes a networking system for managing communications between containers, your Docker host, and the outside world. Several different network types are supported, facilitating a variety of common use cases.
Docker networking is a fundamental aspect of containerization that allows containers to communicate with each other, the host, and external networks. In this blog, we'll explore the different types of Docker networking, their purposes, and use cases, and provide a practical example.
๐ข Impact in Real Life
๐ Scalability: Overlay networks enable seamless communication between backend services, even if distributed across different hosts.
๐๏ธ Isolation: Bridge networks ensure containers can communicate while being isolated from external networks.
โกPerformance: Host networking provides maximum throughput for the database container.
๐งFlexibility: Macvlan networks are ideal for scenarios where containers need unique MAC addresses.
๐ Checking available networks in the system
Here we will check the available network layer in the system using the below command
ip address show
ip link show
ip link
Here you will see 2 by default network is showing in the system
Now after installing the docker, you can see that one more network layer is added which is docker0
so docker is used the docker0
network layer
โ๏ธ Types of Docker Networking
Here are seven different types of Docker networks explained.
Bridge Network:
๐ Description: Default network for containers on a single host.
๐ Use Case: Applications with multiple containers needing communication within the same host.
๐ Key Points:
Isolated internal network.
Containers can access each other using container names as hostnames.
Containers can't directly access the host network.
Host Network:
๐ Description: Containers share the host's network namespace.
๐ Use Case: Performance-critical applications needing direct access to the host network.
๐ Key Points:
No network isolation between containers.
Containers use the host's network stack.
Optimal network performance.
Overlay Network:
๐ญ Description: Spans multiple Docker daemons and hosts.
๐ Use Case: Containers distributed across a cluster of machines requiring inter-host communication.
๐ Key Points:
Uses the overlay driver to create networks.
Facilitates seamless communication between containers on different hosts.
Suited for microservices architectures.
Macvlan Network:
๐ฅ๏ธ Description: Assigns a unique MAC address to each container.
๐ข Use Case: Containers needing direct connectivity to the physical network.
๐ Key Points:
Containers appear as separate physical devices on the network.
Provides a bridge between virtual and physical networks.
Enables containers to be part of VLANs.
None Network:
๐ซ Description: Containers with no network access.
๐ Use Case: Containers where network access is not needed, enhancing security.
๐ Key Points:
Containers are isolated from any network.
Suitable for containers with restricted functionality.
Custom Bridge Network:
๐ Description: User-defined bridge network with custom configurations.
๐ ๏ธ Use Case: Fine-tuned networking requirements for specific applications.
๐ Key Points:
Users can create and manage their own bridge networks.
Provides more control over network settings.
Network Aliases:
๐ Description: Assigning multiple network aliases to a container.
๐ Use Case: Running multiple services on a single container with different network identities.
๐ Key Points:
Containers can have multiple virtual network interfaces.
Useful for multi-service containers without launching separate containers.
๐ Managing Networks
Listing networks: You can list all your Docker networks with the command:
docker network ls
Creating a user-defined network: You can create the custom/user define network with the command:
docker network create <your-network-name>
You can see the new custom network is created as
my-network
Deleting the network: You can delete the network with the command:
docker network rm <your-network-name/id>
Delete all unused networks: You can automatically delete all unused networks using the
network prune
command:docker network prune
๐ Connecting Containers to Networks
You can create a new container without setting the --network
flag then the containers will create within the default bridge network.
We can see the container added in the default bridge network.
You can attach new containers to a network by setting the --network
flag with your docker run
command.
Now create your own network add attach the network to the container
After inspecting the my-network you can see the newly created container con2
added into the custom network my-network
๐ก Connecting Containers to Containers
Using Different Networking:
Here we will create two Ubuntu containers
con1
in thedefault bridge
network andcon2
in custommy-network
and let's see if can we able to achieve network isolation with the help of docker networkingNow try communicating between the two containers, using their names/id, Enter into the container
con1
and try to connect to the containercon2
The containers arenโt in the same network yet, so they canโt directly communicate with each other.
Using the Same Networking:
Now, Here we will create two Ubuntu containers
con3
andcon4
in same custommy-network
, and let's see how can we achieve network isolation with the help of docker networking.Now try communicating between the two containers, using their names/id, Enter into the container
con3
and try to connect to the containercon4
Now the containers are in the same network, so they can directly communicate with each other.
๐ป Host Networking
Bridge networks are what youโll most commonly use to connect your containers. Letโs also explore the capabilities of host networks, where containers attach directly to your hostโs interfaces instead of docker0
network. You can enable host networking for a container by connecting it to the built-in host
network
Here we will create nginx web server
container in it and try to access it from the localhost
NGINX listens on port 80 by default. Because the containerโs using a host network, you can access your NGINX server on your hostโs localhost:80
or inEC2 instance IP Address
even though no ports have been explicitly exposed
Note: If you are using EC2 instance make sure port 80
should be open in the inbound rule of the EC2 instance security group
Copy the IP address of the EC2 instance and paste it into the browser you are able to see the nginx page
๐ซ None Networking
When a containerโs networking is disabled, it will have no connectivity available โ either to other containers or your wider network. Disable networking by attaching your container to the none
network
Here we will create a container into none network and try to ping google.com
๐คฆโโ๏ธ Removing Containers from Networks
Docker lets you freely manage network connections without restarting your containers. In the previous section, you saw how to connect a container after its creation; itโs also possible to remove containers from networks they no longer need to participate in
Now the container is no belongs to my-network network
๐๐Conclusion:
๐ Understanding Docker networking ๐ and its types is crucial for designing efficient, scalable, and secure containerized applications. By leveraging the appropriate networking type for each scenario, you can optimize communication, performance, and isolation within your container ecosystem.
Special Thanks & Reference Video Shubham Londhe
Thank you๐๐... for taking the time to read this blog. I hope you found the information helpful and insightful. So please keep yourself updated with my latest insights and articles on DevOps ๐ by following me on
So, Stay in the loop and stay ahead in the world of DevOps!
Happy Learning !... Keep Learning ! ๐