3

I am using docker-compose to run my application stack.

The application stack is:

  1. Mongo
  2. Rest Service (referred as rest-service hereafter)
  3. UI Service (referred as ui-service hereafter)

Below is the snippet of my 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. But ui-service cant talk to rest-service(I mean on port(5000 on docker container)).

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. Hence, I am not able to understand why this happens.

Any help would be highly appreciated.

4

1 回答 1

1

ports除非您想从该容器网络外部访问该端口,否则请移除这些部件。链接容器不需要显式地相互公开端口,正如您发现的那样,它将它们公开给主机。您需要不公开端口仅访问您正在使用的端口,而是通过localhost:1234语法(不是container-name:1234)。

确保您了解如何引用端口,如果您使用容器名称,您可能需要链接,但如果您不想这样做,则需要使用 localhost 和主机端口。

使用链接自动允许链接容器访问容器的端口。这意味着您的 mongodb 上的任何端口都可以通过它的容器端口访问,即使它没有公开。

当您显式公开它们时,它们会暴露给您的 docker 本地主机,这就是您的某些东西有效的原因。

于 2016-09-21T02:24:22.707 回答