1

I have the web application which is running on the docker container and that one must send requests to my local machine (localhost) and when I'm trying to do that I get the following exception:

java.util.concurrent.ExecutionException: java.net.ConnectException: Connection refused: localhost/127.0.0.1:9033

I'm trying to give you some more details. I looked at this post but it does not work for me.
First of all, I want to explain my project's structure: 1. I have a project that consist of two parts: frontend (angular 9) and backend (play 2.8.x framework) 2. I have another one project (web-service)

My first projects (backend, frontend) are placing in the docker container and the second (web-service) is placing at the localhost. I have the following flow between these projects:
1. The user sends a request (from the web browser) to the backend it may be login request for instance.
2. The backend process this request (retrieve data from the database) and sends these data to the web-service which is placing on the localhost.
and I have a problem with step 2 where I trying to send data to the web-service. I'm getting the exception which was writing above.
I'm trying to set --network=host but in this case, I get 404 status code in the browser. How can I solve this issue?

John
  • 1,375
  • 4
  • 17
  • 40
  • Did you use --network="host" flag? – VM4 May 07 '20 at 15:26
  • @VM4 no I don't use this flag – John May 07 '20 at 15:35
  • Try it. This explains it well: https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach – VM4 May 07 '20 at 15:43
  • https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach – gmolaire May 07 '20 at 15:47
  • Thanks to all but this approach do not work for me. When I'm using ```--network=host``` I couldn't get anything in my browser and when I'm entering ```http://localhost:4200``` I get the page not found. I'm trying to edit my question and give to you more details. – John May 08 '20 at 10:02

1 Answers1

1

You cannot perform requests to localhost inside the container, since from the container perspective localhost is not the host machine (where docker runs), but the container itself.

If 9033 is the port where your server is listening on your machine, you can do the mapping when running the container via

docker run --expose 9033 containerName

Then, if no custom network configuration has been applied to the container, the default gateway (seen using ip route show | grep 'default' | awk '{print $3}') corresponds to the IP you should connect to.

I tried this with a simple Python HTTP server running on port 9000 in my host.

From within the host: python3 -m http.server 9000

From the container:

export IP=$(ip route show | grep 'default' | awk '{print $3}')
curl -k -I -X GET "${IP}:9000"

gives HTTP/1.0 200 OK

dariofac
  • 247
  • 1
  • 14
  • 1
    `docker run --expose` doesn't do anything for this case. (It was useful in an outdated mode of Docker inter-container communication, and names a port where a server inside the container is listening; modern Docker does pretty much nothing with it, and its port number is not a host port.) – David Maze May 07 '20 at 16:34
  • you mentioned, "if no custom network configuration has been applied to the container." what if a custom network configuration has been applied? Is there a way to connect to a server running on the host in that case? – Chris Mar 19 '21 at 16:55