Deep Dive into Docker ๐ŸณNetworking ๐Ÿ”Œ and Its Types

Deep Dive into Docker ๐ŸณNetworking ๐Ÿ”Œ and Its Types

ยท

7 min read

๐Ÿš€ 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.

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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.

  6. 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.

  7. 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

  1. Listing networks: You can list all your Docker networks with the command:

      docker network ls
    

  2. 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

  3. Deleting the network: You can delete the network with the command:

     docker network rm <your-network-name/id>
    

  4. 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

  1. Using Different Networking:

    Here we will create two Ubuntu containers con1 in the default bridge network and con2 in custom my-network and let's see if can we able to achieve network isolation with the help of docker networking

    Now try communicating between the two containers, using their names/id, Enter into the container con1 and try to connect to the container con2

    The containers arenโ€™t in the same network yet, so they canโ€™t directly communicate with each other.

  2. Using the Same Networking:

    Now, Here we will create two Ubuntu containers con3 and con4 in same custom my-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 container con4

    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 ! ๐Ÿ˜Š

ย