'Docker container sending request to http

Hi I have a question.

I am using docker compose ver3 and consul image as container, on my host machine I have a simple service that should response to http request and it is set on port 40001. So now I want to make docker send requests to this service as health check through http (in other words send request from docker container to service that is running on host).

My docker compose for consul is simple:

version: '3'

services:

  consul:
  image: consul:latest
  command: consul agent -dev -log-level=warning -ui -client=0.0.0.0
  hostname: consul
  container_name: consul
  ports:
    - 8500:8500
    - 8600:8600/udp
    - 40000-41000:40000-41000

So I startup this container, service on host is registered inside it with http health check address for service on host with is "http://localhost:40005//Health". Interval for request is about 15 seconds so I expect such a request arrives on service and service will response with 200.

Also I have access to UI for consul in container.

Well service on host does not register any incoming request, but interesting thing is when I run stand-alone consul it works fine (that is I am getting health checks).

So my question is, is this could be the problem that container can not communicate with service on host.



Solution 1:[1]

For the example, lets say that you have a service running on port 5555 on your host.

Below are the four solutions if you want to communicate to your host from a docker container :

  1. You're on linux : In order to contact your host, you need to contact it throught the default bridge network IP. Usually, it's called docker0 and it's IP address is something like 172.17.0.1 (you have to check those value).
    The service address will be http://172.17.0.1:5555
  2. You're on Mac OS : Since Docker 18.03, the recommandation is to use the special DNS name host.docker.internal (here is the doc).
    The service address will be http://host.docker.internal:5555.
  3. You're on Windows : The solution is the same one as for Docker for mac (doc).
    The service address will be http://host.docker.internal:5555.
  4. All OS : You have a static public IP or DNS name that points to your service, you can just use it.
    The service address will be http://[host_public_ip_or_dns]:5555.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1