Networking Between Two Docker Containers in Window OS

Introduction

Basically, you’ve gone through the quick starts, and you’ve run your first Docker containers. But now you’re struggling to understand how to run more than one container at the same time. If Docker containers are isolated, then how supposedly do they communicate with each other?

How do containers communicate?

First, a quick overview! Although containers have a level of isolation from the environment around them, they often need to communicate with each other and the outside world.

Networking

Two containers can talk to each other in one of two ways, usually.

  • Communicating through networking: Containers are designed to be isolated. But they can send and receive requests to other applications using networking.
    For example. a web server container might expose a port so that it can receive requests on port 80. Or an application container might make a connection to a database container.
  • Sharing files on disk: Some applications communicate by reading and writing files. These kinds of applications can communicate by writing their files into a volume, which can also be shared with other containers.
    For example. a data processing application might write a file to a shared volume that contains customer data, which is then read by another application. Or, two identical containers might even share the same files.

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.

In this article, we’ll explain how 2 Docker images communicate as different container over bridge network.

Docker Network Types

Docker networks configure communications between neighboring containers and external services. Containers must be connected to a Docker network to receive any network connectivity. The communication routes available to the container depend on the network connections it has.

Docker comes with five built-in network drivers that implement core networking functionality.

1. Bridge

Bridge networks create a software-based bridge between your host and the container. Containers connected to the network can communicate with each other, but they’re isolated from those outside the network.

Each container in the network is assigned its own IP address. Because the network’s bridged to your host, containers are also able to communicate on your LAN and the internet. They will not appear as physical devices on your LAN, however.

2. Host

Containers that use the host network mode share your host’s network stack without any isolation. They aren’t allocated their own IP addresses, and port binds will be published directly to your host’s network interface. This means a container process that listens on port 80 will bind to <your_host_ip>:80.

3. Overlay

Overlay networks are distributed networks that span multiple Docker hosts. The network allows all the containers running on any of the hosts to communicate with each other without requiring OS-level routing support.

Overlay networks implement the networking for Docker Swarm clusters, but you can also use them when you’re running two separate instances of Docker Engine with containers that must directly contact each other. This allows you to build your own Swarm-like environments.

4. IPvLAN

IPvLAN is an advanced driver that offers precise control over the IPv4 and IPv6 addresses assigned to your containers, as well as layer 2 and 3 VLAN tagging and routing.

This driver is useful when you’re integrating containerized services with an existing physical network. IPvLAN networks are assigned their own interfaces, which offer performance benefits over bridge-based networking.

5. MACvLAN

macvlan is another advanced option that allows containers to appear as physical devices on your network. It works by assigning each container in the network a unique MAC address.

This network type requires you to dedicate one of your host’s physical interfaces to the virtual network. The wider network must also be appropriately configured to support the potentially large number of MAC addresses that could be created by an active Docker host running many containers.

Let’s create 2 .Net applications and create their images for deployment.

  1. .Net MVC application as a frontend
  2. .Net Web API as a business layer

MVC Web App

Create a new project for the MVC web app.

.NET MVC new project

Don’t forget to tick “Enable Docker”, as you get ready to make the Docker file included in the project to create an image.

MVC Web application

Add the below code to call the API request to fetch data. In the below snippet, we have tried calling API on the 5000 port to run on local debug mode.

For Docker running on Windows, I have used directly “host.docker.internal” to connect to services running on the WIndows-host from inside a Docker container.

Docker container

Web API

The same initial process for Web API, do select the Docker enable option for Web API, too.

Create a list of objects to return as data.

Docker repository

APIDocker controllers

Run both applications in debug mode to check interconnectivity.

Run Application

Let’s create their respective images to run in containers

Command prompt

Use the same command to create an image for other MVC applications, too.

Docker build –t <docker_image>:<tag>.

In our case, we haven’t mentioned the tag, so it is considered the latest version. We have created two images in the repository i.e. “dockerproject” following the image name as shown below pic.

Docker images

Once the images have been created then we can run the images in their respective containers.

  • --name d1: It tells us we are assigning a name to a container i.e. “d1” and “d2” in the above case.
  • -d: It is short for the --detach option. Docker detaches the container process and runs it in the background.
  • -p: It tells the port number where it can be run.

“docker ps” will enlist the list of Docker containers and provide crucial information about each container, such as its ID, image, status, and more, making Docker container management simpler and more efficient.

Docker PS

If you have Docker Desktop, then you can see those images running in their respective containers.

Containers

We can now click on their respective port to run on the browser and check their connectivity.

MVC Docker

If I have stop the API image container from Docker Desktop, then the MVC image container stops responding and gives an error.

API image container

Exception

Similarly, you can isolate and containerize your application or its layers, and then you establish the connection between them.

Networking is about communication among processes, and Docker’s networking is no different. Docker networking is primarily used to establish communication between Docker containers and the outside world via the host machine where the Docker daemon is running.