简体   繁体   English

从另一个docker容器内部访问在docker容器中运行的服务

[英]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? 实现这一目标最简单的方法是什么?

Why does it not work? 为什么不起作用?

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:portanything命令将尝试连接到特定于容器的本地IP堆栈,而不是其他容器和主机。

How to make it work? 如何使它工作?

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.

相关问题 在另一个Docker容器中运行一个Docker容器? - Running a docker container inside another docker container? Docker服务未运行容器 - Docker service not running container 从另一个 Docker 容器内部生成 Docker 容器的最佳方法? - Best way to spawn a Docker container from inside another Docker container? 从另一个容器连接到在 docker 容器中运行的 mysql 服务器 - connect to mysql server running in docker container from another container 能够连接到本地运行在Docker容器中的MongoDB,但不能从另一个容器连接 - Able to connect to MongoDB running in a Docker container locally, but not from another container 如何从 AWS SAM 本地 Docker 容器访问另一个 Docker 容器(运行节点快速服务器)? - How do I access another Docker container (running a node express server) from a AWS SAM Local Docker container? 从Docker容器的服务访问同级服务 - Access sibling service from docker container's service 如何从另一个 docker 容器连接到作为 docker 容器启动的 Verdaccio 服务? - How can I connect to my Verdaccio service launched as docker container from another docker container? 如何从另一个 docker 容器访问一个 docker 容器的快速路由 - How to get access to express routes of a docker container from another docker container 本地运行的节点是否需要 docker 容器中的文件? - Can node running locally require a file from inside a docker container?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM