在另一个docker集装箱内访问在docker集装箱中运行的服务
目前,我正在一个docker容器中运行一个node.js应用程序,该容器需要连接到在另一个容器中运行的camunda。
我使用以下命令启动容器
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
这两个应用程序现在都在运行,我可以通过在端口8000上导航到主机的IP来访问camunda,并运行wget http://localhost:8000 -q -O -
也会返回camunda页面。 当我用docker exec -it app sh
login到我的应用程序容器并键入wget http://localhost:8000 -q -O -
,我无法访问camunda。 相反,我得到以下错误:
wget: can't connect to remote host (127.0.0.1): Connection refused
当我连接我的应用程序容器与--link camunda:camunda
camunda容器,并键入wget http://camunda:8000 -q -O -
在我的应用程序容器中,我得到以下错误:
wget: can't connect to remote host (172.17.0.4): Connection refused`
我见过这个选项 ,所以我用--add-host camunda:my_hosts_ip
启动了我的应用程序容器, --add-host camunda:my_hosts_ip
再次尝试了wget,结果是:
wget: can't connect to remote host (149.210.227.191): Operation timed out
当在我的主机上运行wget http://149.210.227.191:5001 -q -O -
时,我立即得到正确的响应。
理想情况下,我想只是启动我的应用程序容器,而不需要以任何方式提供外部IP,并让应用程序容器通过本地主机使用camunda服务,或者将camunda容器连接到我的应用程序容器。 什么是最简单的方法来实现呢?
为您的应用程序容器使用--link camunda:camunda
选项。 然后,您可以通过http://camunda:8080/...
访问camunda。 链接选项使用camunda容器的IP地址将条目添加到应用程序容器的/etc/hosts
文件中。 这也意味着如果重新启动camunda容器,则必须重新启动应用程序容器。
为什么它不起作用?
容器和主机不共享他们的本地IP堆栈。 因此,当你在一个容器内,并尝试anything localhost:port
命令将尝试连接到特定于容器的本地IP堆栈,而不是其他容器或主机。
如何使其工作?
硬的方法:你需要知道另一个容器的IP地址,并连接到这个IP地址..
更简单更清洁的方式:或者链接您的容器。
--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
所以你需要执行,假设camunda容器被命名为camunda :
docker run -d --name app -p 3000:3000 --link camunda app
然后,一旦你docker-exec
-ed进入容器应用程序,你将能够执行wget http://camunda:8080 -q -O -
没有错误。
请注意,虽然链接容器graphics不能循环,例如, camunda不能链接到应用程序,因为你需要启动一个容器才能够链接它,你实际上做任何你想/需要播放IP地址。 另请注意,您可以使用--ip
选项指定容器的IP地址(尽pipe它只能与--net
一起用于用户定义的networking)。