简体   繁体   English

链接的docker容器无法相互通信

[英]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: 应用程序堆栈是:

  1. Mongo 蒙戈
  2. Rest Service (referred as rest-service hereafter) 休息服务(以下称为休息服务)
  3. UI Service (referred as ui-service hereafter) UI服务(以下称为ui-service)

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.

相关问题 为什么Docker容器无法相互通信? - Why Docker containers can't communicate with each other? Docker Swarm中的容器无法相互通信 - Containers in Docker Swarm unable to communicate with each other 无法让 Docker 容器相互通信 - Unable to have Docker containers communicate with each other 两个Docker容器如何相互通信? - How two docker containers communicate each other? Docker 容器无法在one.network中通信 - Docker containers can't communicate in one network 在Internet上公开本地Docker容器(有两个相互链接的容器)。 - Exposing local Docker containers on internet (There are two containers linked to each other). 同一个网络中的Docker容器无法相互通信 - Docker containers in the same network cannot communicate with each other Docker 同一网络内的容器无法相互通信 - Docker containers inside same network not able to communicate with each other 码头工人组成 | 为什么容器不能互相到达? - Docker-Compose | why the containers can't reach each other? 创建能够相互通信的 docker 容器的最佳方法是什么? - what is the best approach to create docker containers that are able to communicate with each other?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM