[英]Access service running in docker container from inside another docker container
At the moment I'm running a node.js application inside a docker container which needs to connect to camunda, which runs in another container. 目前我正在一个docker容器中运行一个node.js应用程序,该容器需要连接到另一个容器中运行的camunda。
I start the containers with the following command 我使用以下命令启动容器
docker run -d --restart=always --name camunda -p 8000:8080 camunda/camunda-bpm-platform:tomcat-7.4.0
docker run -d --name app -p 3000:3000 app
Both applications are now running and I can access camunda by navigating to my host's IP on port 8000, and running wget http://localhost:8000 -q -O -
also returns the camunda page. 这两个应用程序现在都在运行,我可以通过在端口8000上导航到主机的IP来访问camunda,并且运行wget
http://localhost:8000 -q -O -
也会返回camunda页面。 When I login to my app container with docker exec -it app sh
and type wget http://localhost:8000 -q -O -
, I cannot access camunda. 当我使用
docker exec -it app sh
登录我的app容器并键入wget http://localhost:8000 -q -O -
,我无法访问camunda。 Instead I get the following error: 相反,我收到以下错误:
wget: can't connect to remote host (127.0.0.1): Connection refused
When I link my app container to the camunda container with --link camunda:camunda
, and type wget http://camunda:8000 -q -O -
in my app container, I get the following error: 当我使用
--link camunda:camunda
将我的app容器链接到camunda容器,并在我的app容器中输入wget http://camunda:8000 -q -O -
,我收到以下错误:
wget: can't connect to remote host (172.17.0.4): Connection refused`
I've seen this option , so I started my app container with --add-host camunda:my_hosts_ip
and tried wget again, resulting in: 我见过这个选项 ,所以我用
--add-host camunda:my_hosts_ip
启动了我的app容器并再次尝试了wget,结果是:
wget: can't connect to remote host (149.210.227.191): Operation timed out
When running wget http://149.210.227.191:5001 -q -O -
on my host machine however, I get a correct response immediately. 在我的主机上运行
wget http://149.210.227.191:5001 -q -O -
时,我立即收到正确的响应。
Ideally I would like to just start my app container without the need to supply the external IP in any way, and let the app container just use the camunda service via the localhost or by linking the camunda container tot my app container. 理想情况下,我想启动我的应用程序容器,而无需以任何方式提供外部IP,并让app容器通过localhost使用camunda服务或将camunda容器链接到我的app容器。 What would be the easiest way to achieve this?
实现这一目标最简单的方法是什么?
Containers and host do not share their local IP stack. 容器和主机不共享其本地IP堆栈。 Thus, when you are within a container and try
anything localhost:port
the anything
command will try to connect to the container-specific local IP stack, not the other container nor the host. 因此,当您在容器内并尝试
anything localhost:port
, anything
命令将尝试连接到特定于容器的本地IP堆栈,而不是其他容器和主机。
Hard way: you either need to know the IP address of the other container and connect to this IP address.. 困难的方法:您需要知道其他容器的IP地址并连接到此IP地址。
Easier and cleaner way: .. either link your containers. 更简单,更清洁的方式: .. 链接您的容器。
--link=[] Add link to another container in the form of <name or id>:alias or just <name or id> in which case the alias will match the name
So you'll need to perform, assuming the camunda container is named camunda : 假设camunda容器名为camunda ,那么你需要执行:
docker run -d --name app -p 3000:3000 --link camunda app
Then, once you docker-exec
-ed into the container app you will be able to execute wget http://camunda:8080 -q -O -
without error. 然后,一旦你将
docker-exec
-ed放入容器应用程序中,你就可以执行wget http://camunda:8080 -q -O -
而不会出错。
Note that while the linked containers graph cannot loop, eg, camunda cannot be linked to app as you need to start a container to be able to link it, you actually do whatever you want/need playing with IP addresses. 请注意,虽然链接的容器图形无法循环,例如, camunda无法链接到应用程序,因为您需要启动容器以便能够链接它,您实际上做任何您想要/需要使用IP地址。 Note also that you can specify the IP address of a container using the
--ip
option (though it can only be used in conjunction with --net
for user-defined networks). 另请注意,您可以使用
--ip
选项指定容器的IP地址(尽管它只能与--net
一起用于用户定义的网络)。
Use the --link camunda:camunda
option for your app container. 使用适用于您的应用容器的
--link camunda:camunda
选项。 Then you can access camunda via http://camunda:8080/...
. 然后你可以通过
http://camunda:8080/...
访问camunda。 The link option adds a entry to the /etc/hosts
file of the app container with the IP address of the camunda container. 链接选项使用camunda容器的IP地址向应用程序容器的
/etc/hosts
文件添加条目。 This also means you have to restart your app container if you restart the camunda container. 这也意味着如果重新启动camunda容器,则必须重新启动应用程序容器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.