Skip to main content

Command Palette

Search for a command to run...

Inter-Container Communication in Docker

Docker | Containers | Container Networking in detail

Published
4 min read
Inter-Container Communication in Docker

Hey folks,

I recently encountered a relatively less discussed aspect of Docker, ie. Docker networking. So I decided to write a blog on this topic. Docker networking is like setting up a communication system for containers. It helps them share information, data, and resources effectively. Just as the rooms are connected with doors or hallways, Docker containers are connected with the Docker networks.

Docker Networking

Docker networking is establishing efficient and secure communication pathways between the containers, enabling them to seamlessly exchange data and interact with each other in a safe and secure way. These networks can be internal networks or external networks.

Types Of Docker Network

  • Host Network

  • Bridged Network

  • Overlay Network

  • Macvlan Network

  • Ipvlan Network

  • None Network

🚀Here, in this blog, we will discuss each of these in detail, their Use Cases, and creating and establishing these networks.

So let's get started...

Host Network

  • Connecting a Docker container to the Host network means directly using the host network for this container.

  • This removes the Network Isolation between the host machine and the docker container.

  • Port mapping does not work in this network.

docker run -itd --net=host image_name

-itd : Interactive Terminal Mode

--net : used to connect containers to host

To access this go to localhost and then that port of the container. I am using google cloud so the output looks like:

Bridged Network

  • Bridge network is a Link Layer device that forwards traffic between network segments. A bridge can be a hardware device or a software device running within a host machine’s kernel.

  • These are typically of two types- Default Bridge and User-defined Bridge

  • Port mapping is done in order to access the application hence, network isolation is there.

Default bridge

  • It is the default network for the Docker container if no network is defined while creating containers they will be connected to the default bridge.

  • It does not provide a feature of host resolution for the other containers within the network.

User-Defined bridge

  • As the name suggests these networks are created by the user, while creating of the Docker containers we need to specify the bridge name.

  • It provides a feature of host resolution, we can directly communicate with the other containers using their container names.

Creating a bridge:

To create a bridge network:

docker network create network_name

Illustration:

Here we are creating two containers(san1 and san2) and attaching them to the User-defined Bridge(sanwork).

Below you can see that we can ping san1 from san2. This feature is called Host Resolution, this is only available for User-defined Bridge.

Overlay Network

  • The overlay network is a network that is designed to enable containerized applications to communicate with each other even though they are running on separate physical or virtual machines.

  • It is usually used in Docker Swarm(clustering and orchestration solution for Docker containers).

Creating an Overlay Network:

docker network create --driver overlay overlay_name

Macvlan Network

  • Macvlan network is a type of Docker network that allows you to connect containers directly to a physical network interface on the host system.

  • It allows us to create multiple virtual network interfaces with unique MAC addresses on a single physical network interface. Each macvlan interface behaves as if it were a physical interface on its own

Creating Macvlan Network:

docker network create --driver macvlan --subnet=subnet_range --gateway=gateway_IP network_name

Ipvlan Network

  • Similar to Macvlan used to connect the containers directly to the physical network.

  • It operates at Layer 3, Each ipvlan interface gets its own unique IP address.

Creating Ipvlan Network

docker network create -d ipvlan --subnet=subnet_range --gateway=gateway_IP -o parent=parent_interface network_name

None Network

  • None Networks are networks that are used to completely isolate the containers from external sources.

  • Internet or any external network resources cannot be accessed within the containers.

Launching a container with None Network

docker run -itd --network none image_name

Some Networking Commands

To delete a network:

docker network rm network_name

To view the existing networks:

docker network ls

To inspect a Network:

docker network inspect network_name

To Connect the container to the network:

docker network connect network_name container_name

To Disconnect the container from the network:

docker network disconnect network_name container_name

To prune unused networks:

docker network prune

🌟Feel free to connect with me and share your feedback!

Connect with me👇