Introduction of Docker Networks Requests and Container Communication

Posted By : Lokesh Singh | 26-Apr-2021

1. Introduction

When Working with docker in many applications, you'll need more than one container - for two main reasons:

  1. It's considered good practice to focus each container on one main task (e.g. run a web server, run a database, ...) 
  2. It's very hard to configure a Container that does more than one "main thing" (e.g. run a web server AND a database)

Multi-Container apps are immensely common, specifically if you're working on "real applications".

In this blog, we have created a brief technical terminology.

1. Docker - Docker is a tool for creating and managing container and it is a container technology

2. Container - A standardized unit of software.

Often, some of these Containers need to communicate through:

  • Either with each other
  • Or with the host machine
  • or with the world wide web

 

2. Communicating with the World Wide Web(www)

 

Communicating with the WWW (i.e. sending an HTTP request or other kinds of requests to other servers) is thankfully very easy.

Consider this JavaScript example - through it'll always work, no matter which technology you're using:

fetch ('https://some-api.com/my-data').then(...)

This generic code snippet sends a GET request to some-api.com/my-data.

As no extra configuration is required, this works as an out-of-the-box approach! The application, running in a Container, will have no problems sending these requests.

 

3. Communicating with the Host Machine

 

Communicating with the Host Machine (e.g. because you have a database running on the Host Machine) is also quite simple, though it doesn't work without any changes.

One important note: if you deploy a Container onto a server or another machine, it is unlikely that you are required to communicate with that machine. Typically, communicating to the Host Machine is a requirement during the development stage - for instance, since you're running some development database on your machine.

Again, consider this JS example:

fetch('localhost:3000/demo').then(...)

This code snippet sends a GET request to some web server running on the local host machine or outside of the Container but not the  World Wide Web or WWW.

On your local machine, this would work - inside of a Container, it will fail. Because localhost inside of the Container refers to the Container environment, not to your localhost machine which is running the Container / Docker!

But Docker has got you covered!

You just need to change this snippet like this:

fetch('host.docker.internal:3000'/demo).then(...)

host.docker.internal is a special address/identifier which is translated to the IP address of the machine hosting the Container by Docker.

Important: "Translated" does not mean that Docker goes ahead and changes the source code. Instead, it simply detects the outgoing request and is able to resolve the IP address for the request.

 

4. Communicating with Other Containers

 

Communicating with other Containers is also quite straightforward. You have two main options:

  1. Manually find out the IP of the other Container (It may change though)
  2. Use Docker Networks and put the communicating Containers into the same Network

Since you need to search for the IP on your own and chances are that it might change over time, Option 1 is not a great approach.

Option 2 is perfect though. With Docker, you can create Networks via docker network create SOME_NAME and you can then attach multiple Containers to one and the same Network.

Like this:

docker run -netwok my-network --name cont1 my-image
docker run -network my-network --name cont2 my-other-image

Both cont1 and con2 will be in the same Network.

Now, you can simply use the Container names to let them communicate with each other - again, Docker will resolve the IP for you (see above).

fetch('cont/my-data').then(...)

 

5. Conclusion 

 

Now, this way you can communicate between containers or any container with your host machine or with WWW in Docker.

About Author

Author Image
Lokesh Singh

He is Hard Working and Punctual and keen to learn new technologies. He works as both Full Stack Developer and MEAN Stack Developer on both frontend and backend technologies.

Request for Proposal

Name is required

Comment is required

Sending message..