简体   繁体   English

docker容器中的应用程序无法在同一容器上调用服务

[英]App within docker container cannot invoke service on same container

To start my docker container i use : 要启动我的docker容器,我使用:

docker run -p 5001:5001 -p 7777:7777 -i -t --entrypoint /bin/bash dc261bdea451

As can see I'm exposing two ports : 5001 & 7777 可以看出我暴露了两个端口:5001和7777

nodejs is running on port 5001 & socko server on 7777. From the browser I can access these services directly via http://localhost:5001 & http://localhost:7777 . nodejs正在nodejs上的端口5001和socko服务器上运行。在浏览器中,我可以通过http://localhost:5001http://localhost:7777直接访问这些服务。 The nodejs app running on port 5001 makes a call to the service running on port 7777 : 在端口5001上运行的nodejs应用程序调用在端口7777上运行的服务:

So by invoking http://localhost:5001 a call to http://localhost:7777 is made : 因此,通过调用http://localhost:5001可以调用http://localhost:7777

var request = require('request');
request("http://localhost:7777?param=param....

The issue is service running on port 7777 is not being invoked. 问题是没有调用端口7777上运行的服务。 I also tried : 我也尝试过:

var request = require('request');
request("http://127.0.0.1:7777?param=param....

but same result. 但结果相同。

This issue does not occur when I run the apps outside of docker container. 当我在docker容器外运行应用程序时,不会发生此问题。

Is there a config option I'm missing that allows two servers running on one docker container to communicate ? 是否有一个我缺少的配置选项允许在一个docker容器上运行的两个服务器进行通信?

Update : 更新:

Changing the hostname to IP address of machine docker container is running on reaches the endpoint. 将主机名更改为机器泊坞窗容器的IP地址正在运行到达端点。 So changing this works : 所以改变这种方式:

var request = require('request');
request("http://localhost:7777?param=param....

to

var request = require('request');
request("http://my.ip.address:7777?param=param....

But this is not a solution as it requires an update to the container after it's deployed. 但这不是一个解决方案,因为它需要在部署后更新容器。

  1. You do not need to use port mapping ( -p flag) to allow container internal communication. 您不需要使用端口映射( -p标志)来允许容器内部通信。 This is only required when you want to connect from your host to the container. 只有当您想要从主机连接到容器时才需要这样做。
  2. In general it is recommended to only run one process per container. 通常,建议每个容器只运行一个进程。 You might want to look at running the two processes in different containers and link them via Docker networking. 您可能希望查看在不同容器中运行这两个进程并通过Docker网络链接它们。

Anyway, to investigate the issue start a bash in your container to validate the service on port 7777 is reachable from within the container ( docker exec -it <container_id> bash , telnet localhost 7777 ). 无论如何,要调查此问题,请在容器中启动bash以验证端口7777上的服务是否可从容器内访问( docker exec -it <container_id> bashtelnet localhost 7777 )。 In case it is not listening you need to check that it is binding to the correct hostname. 如果它没有监听,你需要检查它是否绑定到正确的主机名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM