Sagar Shroff
Sagar Shroff

Reputation: 115

Question related to how docker containers networking work

When we expose ports through docker its network path is as below

Docker container network flow

So when I run my container using below command

docker run --rm -it --name server -p 45678:45678 ubuntu:14.04 bash

Here we are basically mapping external-host-port : with internal container port correct?

Now inside the above container If I start netcat to listen to port 45678; Then any container should be able to connect with it using nc <my-windows-hostname> 45678 right? However, it does not work.

I read about this and found out that we need to use host.docker.internal instead of windows-hostname.

My question is why so??

Upvotes: 1

Views: 68

Answers (2)

danielorn
danielorn

Reputation: 6147

Docker for Windows (and Docker for Mac) utilizes a virtual machine with a Linux Kernel to provide runtime environment for linux containers.

This means that the containers do run on a separate host (with another name and IP) than your windows host, as describe din the illustration below below

|--------------------------------------------------------|
|  Windows host     |----------------------------------| |
|                   | Docker VM                        | |
|  docker cli       | |-------------|  |-------------| | |
|                   | | ContainerA  |  | ContainerB  | | |
|                   | |             |  |             | | |
|                   | |-------------|  |-------------| | |
|                   |----------------------------------| |
|--------------------------------------------------------|

The docker cli runs on windows, but all containers run inside the Docker VM.

When you run the command docker run --rm -it --name containerA -p 45678:45678 ubuntu:14.04 bash port 45678 on the Docker VM is forwarded to port 45678 in containerA .

In addition the Docker CLI takes care of forwarding port 45678 on the windows host to the Docker VM. The result of this is that when you use localhost:45678 or <my-windows-hostname>:45678 from your windows machine, you will end up on the container through the chain:

<my-windows-hostname>:45678 -> <docker VM>:45678 -> ContainerA:45678

What you are trying to do is to access another container through a published port from another container, not the windows host. In order to do so you would need to have the internal hostname or IP of the Docker VM rather than the windows host. This is what you can use host.docker.internal for.

From the Docker for Windows Documentation

The host has a changing IP address (or none if you have no network access). From 18.03 onwards our recommendation is to connect to the special DNS name host.docker.internal, which resolves to the internal IP address used by the host.

This is for development purpose and will not work in a production environment outside of Docker Desktop for Windows.

Upvotes: 1

Kapil Khandelwal
Kapil Khandelwal

Reputation: 1176

When you start Docker, a default bridge network (also called bridge) is created automatically, and newly-started containers connect to it unless otherwise specified.

So when you started your container, it was connected to the default bridge network.

Also, containers on the default bridge network can only access each other by IP addresses, unless you use the --link option, which is considered legacy.

What host.docker.internal does is that it resolves to the internal IP address used by the host (which changes frequently).

Reference

Upvotes: 0

Related Questions