Connect from Docker to Local Host

2019-08-31 02:25发布

问题:

I have a docker container basically running Athena Tool Kit for PDF generation. The service in docker in turn calls a NodeJs Application at this URL: - http://localhost:3005.

However I see a message on Docker Console:-

[GIN] 2019/08/23 - 22:24:46 | 500 |    1.850464ms |    192.168.99.1 |  GET     /convert
Error #01: Get http://localhost:3005/presentations/d0161fed-0ade-40a5-8dff-26696e553e96?id=d0161fed-0ade-40a5-8dff-26696e553e96&type=box-upload&params=: dial tcp 127.0.0.1:3005: connect: connection refused
captured errors:
Error #01: Get http://localhost:3005/presentations/d0161fed-0ade-40a5-8dff-26696e553e96?id=d0161fed-0ade-40a5-8dff-26696e553e96&type=box-upload&params=: dial tcp 127.0.0.1:3005: connect: connection refused.

The Docker Service runs locally at http://192.168.99.100:8080. From this address it is trying to call 127.0.0.1:3005. When it does I get the error above.

I am guessing this must be allowing access to local host from a different IP.

Please let me know what needs to be done.

回答1:

as I can see, you are running to applications, the first one is patelsan/presentations-pdf-toolkit with a docker container and the second one is a nodejs application within your host.

your problem here that the two application running in two different networks, the host network for nodejs app and the docker bridge default network as a default network for your container.

otherwise as mentioned in Docker doc Docker network host :

The host networking driver only works on Linux hosts, and is not supported on Docker Desktop for Mac, Docker Desktop for Windows, or Docker EE for Windows Server.

I have 3 solutions for both local and prod environments:

  1. Local env:

as a local env solution, you can assign the patelsan/presentations-pdf-toolkit container to the default host network in the docker, in this way both applications can communicate with each other:

 - run the `patelsan/presentations-pdf-toolkit` container:
 - `docker network connect host container_name`
  1. production env:

you can use weave net service to create a virtual network.

NP:

If your host is Windows or Mac I suggest you containerize the nodejs application as well, with that you can create a new network and your 2 containers can communicate with that network.