[英]Linked docker containers can't communicate with each other
I am using docker-compose to run my application stack. 我使用docker-compose来运行我的应用程序堆栈。
The application stack is: 应用程序堆栈是:
Below is the snippet of my docker-compose: 下面是我的docker-compose的片段:
services:
mongodb:
image: mongo:3
container_name: mongodb
ports:
- "17027:27017"
rest-service:
build: ./rest-service/
container_name: rest
ports:
- "15000:5000"
command: "/opt/app/conf/config.yml"
links:
- mongodb:mongo
ui-service:
build: ./ui-service/
container_name: ui
ports:
- "18080:8080"
links:
- rest-service:rest
environment:
NODE_ENV: development
The problem I am facing over here is that my rest-service can talk to mongo container (I mean on port(27017 on docker container)), since mongo is linked to restService. 我在这里遇到的问题是我的休息服务可以与mongo容器(我的意思是在端口(Docker容器上的27017)上),因为mongo链接到restService。 But ui-service cant talk to rest-service(I mean on port(5000 on docker container)).
但ui-service不能谈论休息服务(我的意思是在港口(码头集装箱上的5000))。
If I try to make ui-service talk to rest-service on host port (I mean port 15000 which listens on port 5000 of docker container), it works. 如果我尝试与主机端口上的休息服务进行ui-service通话(我的意思是端口15000,它监听docker容器的端口5000),它可以工作。 Hence, I am not able to understand why this happens.
因此,我无法理解为什么会这样。
Any help would be highly appreciated. 任何帮助将受到高度赞赏。
Remove the ports
parts unless you want to access the port from outside this container network. 除非您要从此容器网络外部访问端口,否则请删除
ports
部件。 Linked containers do not need to explicitly expose ports to each other and as you found out, it exposes them to the host. 链接容器不需要相互明确地公开端口,并且正如您所发现的那样,它将它们公开给主机。 You need to either not expose the ports or only access the ports you are using, but through
localhost:1234
syntax (not container-name:1234
). 您需要不公开端口或仅访问您正在使用的端口,而是通过
localhost:1234
语法(不是container-name:1234
)。
Make sure you understand how you are referencing the ports, if you are using container name you will likely need link but if you don't want to do this, will need to use localhost and the host port. 确保您了解如何引用端口,如果使用容器名称,则可能需要链接,但如果您不想这样做,则需要使用localhost和主机端口。
Using link automatically allow linked containers to access ports of the container. 使用链接自动允许链接的容器访问容器的端口。 This means any port on your mongodb can be accessed via it's container port, even if it's not exposed.
这意味着你的mongodb上的任何端口都可以通过它的容器端口访问,即使它没有暴露。
When you explicitly expose them, they are exposed to your localhost for docker, which is why some of your stuff works. 当您明确地公开它们时,它们会暴露给您的localhost for docker,这就是为什么您的某些东西可以工作的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.