1

I have 2 microservices, one is a registration API using a Postgres database, and the second microservice is a login API. I used docker compose up --build to generate the registration miscroservice docker image, which also generated 2 bound containers, airport_registration_container for the springboot registration api and postgres_container for the postgres database run together from the docker-compose.yml file. I added EXPOSE 1993 to the Dockerfile of the registration API to publish its port outside of the docker. The second microservice which is login api is not a docker container, but I want to perform unit testing of the login methods using Jupiter.

The login API's unit tests require to get registered users from the airport_registration_container endpoints running in its docker container.

I’m using spring webClient of springWebflux to get the registered users and I'm having issues with the HOST specified in the webClient's URI parameter. I tried 4 differents HOST and each is giving different error message as follow:

1. First HOST : localhost

When I use localhost in the webClient'uri parameter like public UserDto retreiveUserById(){

    return webClient.get().uri("http://localhost:1993/users/admin", id)
    .retrieve()
    .bodyToMono(UserDto.class)
    .collectList()
    .block();
}

I receive the 403 Forbidden from GET http://localhost:1993/user/admin/02ed840f-6a5d-4b8e-8b05-896c96148309:

"-- Error response code is 403 FORBIDDEN and response body is
15:47:17.745 [main] ERROR com.emuragroup.agculogin.restClients.UserRestClient -- WebClientResponseException in retrieving UserById org.springframework.web.reactive.function.client.WebClientResponseException$Forbidden: 403 Forbidden from GET http://localhost:1993/user/admin/02ed840f-6a5d-4b8e-8b05-896c96148309 at org.springframework.web.reactive.function.client.WebClientResponseException.create(WebClientResponseException.java:312) Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException: Error has been observed at the following site(s): *__checkpoint ⇢ 403 FORBIDDEN from GET http://localhost:1993/user/admin/02ed840f-6a5d-4b8e-8b05-896c96148309 [DefaultWebClient]"

2. Second HOST : airport_registration_container

As the localhost is forbiden I mapped the localhost to the container name like 127.0.0.1 airport_registration_container in /etc/hosts file and then changed the webClient URI to "http://airport_registration_container:1993/users/admin"

Here I received Host is not specified error as bellow:

"-- Exception in retrieving UserById org.springframework.web.reactive.function.client.WebClientRequestException: Host is not specified at org.springframework.web.reactive.function.client.ExchangeFunctions$DefaultExchangeFunction.lambda$wrapException$9(ExchangeFunctions.java:136) Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException: Error has been observed at the following site(s): *__checkpoint ⇢ Request to GET http://airport_registration_container:1993/user/admin/02ed840f-6a5d-4b8e-8b05-896c96148309 [DefaultWebClient]"

3. Third HOST : docker container IP adress 172.21.0.3

It appeared to me that the login microservice in my local machine was unable to reach the registration API from docker. So I did

“docker inspect \ -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' airport_registration_container “

to get the registration's IP address which gives 172.21.0.3, and I then changed the webClient's URI to “http://172.21.0.3:1993/user/admin”

Here I received connection timed out: /172.21.0.3:1993 error as bellow:

"-- Exception in retrieving UserById org.springframework.web.reactive.function.client.WebClientRequestException: connection timed out: /172.21.0.3:1993 at org.springframework.web.reactive.function.client.ExchangeFunctions$DefaultExchangeFunction.lambda$wrapException$9(ExchangeFunctions.java:136) Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException: Error has been observed at the following site(s): *__checkpoint ⇢ Request to GET http://172.21.0.3:1993/user/admin/02ed840f-6a5d-4b8e-8b05-896c96148309 [DefaultWebClient]"

4. Fourth HOST : host.docker.internal

I tried to find the reasons of the time out error, and it seems to me that the 172.21.0.3 address is reachable but not responding to the login api call due to docker itself blocking the request. So I continued my researchs, and from this stackoverflow post I found that host.docker.internal is the substitute for localhost to reach a docker container, and I added this

extra_hosts:
      - "host.docker.internal:host-gateway"
    command: sleep infinity 

to the docker-compose.yml. Now in doing this I received Failed to resolve 'host.docker.internal' error as bellow:

"-- Exception in retrieving UserById org.springframework.web.reactive.function.client.WebClientRequestException: Failed to resolve 'host.docker.internal' [A(1), AAAA(28)] after 6 queries at org.springframework.web.reactive.function.client.ExchangeFunctions$DefaultExchangeFunction.lambda$wrapException$9(ExchangeFunctions.java:136) Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException: Error has been observed at the following site(s): *__checkpoint ⇢ Request to GET http://host.docker.internal:1993/user/admin/02ed840f-6a5d-4b8e-8b05-896c96148309 [DefaultWebClient]"

As you can notice in that stackoverflow post the requests was made from the docker container to the local machine, and the fact that my situation is in inverse order is possibly the reason why it didn't work for me ? And so pursuing my researchs in this documentation, I want to know what it means when they say I want to connect from a container to a service on the host or I want to connect to a container from the host ? Which host are they talking about ? What is docker host and what is docker localhost ?

Ultimately my question is, How to reach my Springboot docker container's endpoints from an Springboot api running on the local machine's localhost ? I'm really out of inspiration and out of solutions. I highly appreciate your help.

System:

  • OS: Mac Big Sur 11.6.1

  • Docker Version: 24.0.2

Himmels DJ
  • 395
  • 5
  • 20

1 Answers1

0

In newest version of docker desktop, i had the similar problem, I changed "localhost" to "docker.for.mac.localhost" worked for me, if you are in windows then it will be "docker.for.windows.localhost"

  • I receive the same error as for host.docker.internal : Caused by: java.net.UnknownHostException: Failed to resolve 'docker.for.mac.localhost' [A(1)] after 6 queries. – Himmels DJ Aug 17 '23 at 07:01