RequestError连接两个docker容器

时间:2018-01-06 13:59:24

标签: javascript node.js docker docker-compose dockerfile

我的docker compose中有3个容器:2个webserver,1个mongodb实例。 Weather-service向主服务发送http请求,因此在我的docker配置中我链接了两个容器。要从天气服务到达主要服务容器,我使用以下地址:http://main-service:3000 当我运行:docker-compose up --build时,我收到连接错误。 天气服务码头文件:

FROM node:8-alpine

EXPOSE 4000

ARG NODE_ENV
ENV NODE_ENV $NODE_ENV

RUN mkdir /app
WORKDIR /app
ADD package.json yarn.lock /app/
RUN yarn --pure-lockfile
ADD . /app

CMD ["yarn", "docker:start"]

主服务码头文件

FROM node:8-alpine

EXPOSE 3000

ARG NODE_ENV
ENV NODE_ENV $NODE_ENV

RUN apk update && apk upgrade && \
    apk add --no-cache bash git openssh

RUN mkdir /app
WORKDIR /app
ADD package.json yarn.lock /app/
RUN yarn --pure-lockfile
ADD . /app

CMD ["yarn", "docker:start"]

搬运工-compose.yml

version: "3"
services:
  main-service:
    build: main-service
    volumes:
      - .:/app/main-service
    ports:
      - "3000:3000"
    env_file:
      - ./main-service/.env
    networks:
      - app-network
    links:
      - weather-service

  weather-service:
    build: weather-service
    environment:
      - MONGO_URI=mongodb://mongodb:27017/forecast-data
    volumes:
      - .:/app/weather-service
    ports:
      - "4000:4000"
    env_file:
      - ./weather-service/.env
    networks:
      - app-network
    depends_on:
      - mongodb

  mongodb:
    image: mongo
    networks:
            - app-network
    ports:
      - "27017:27017"

networks:
  app-network:
    external: true

这是我收到的错误:

  

weather-service_1 | 0 |索引| RequestError:错误:连接   ECONNREFUSED 172.23.0.4:3000 weather-service_1 | 0 |索引|在   新的RequestError   (/app/node_modules/request-promise-core/lib/errors.js:14:15)   weather-service_1 | 0 |索引|在Request.plumbing.callback   (/app/node_modules/request-promise-core/lib/plumbing.js:87:29)   weather-service_1 | 0 |索引|在Request.RP $回调[as   _callback](/ app/node_modules/request-promise-core/lib/plumbing.js:46:31)   mongodb_1 | 2018-01-06T13:46:19.281 + 0000我的存储[conn2]   createCollection:具有生成的UUID的forecast-data.refreshtokens:   85c26f12-464f-4d69-a969-713acf634c6f weather-service_1 | 0 |索引|   在self.callback(/app/node_modules/request/request.js:186:22)   weather-service_1 | 0 |索引|在emitOne(events.js:116:13)   weather-service_1 | 0 |索引|在Request.emit   (events.js:211:7)weather-service_1 | 0 |索引|在   Request.onRequestError(/app/node_modules/request/request.js:878:8)   weather-service_1 | 0 |索引|在emitOne(events.js:116:13)   weather-service_1 | 0 |索引|在ClientRequest.emit   (events.js:211:7)weather-service_1 | 0 |索引|在   Socket.socketErrorListener(_http_client.js:387:9)weather-service_1   | 0 |索引|在emitOne(events.js:116:13)weather-service_1 |   0 |索引|在Socket.emit(events.js:211:7)weather-service_1 |   0 |索引|在emitErrorNT(internal / streams / destroy.js:64:8)   weather-service_1 | 0 |索引|在_combinedTickCallback   (internal / process / next_tick.js:138:11)weather-service_1 | 0 |指数
  |在process._tickDomainCallback   (内部/过程/ next_tick.js:218:9)

1 个答案:

答案 0 :(得分:0)

我建议在compose堆栈文件中创建网络。

...
networks:
  app-network:
   driver: overlay

部署堆栈时,Docker会创建给定的网络。 在docker stack rm上,Docker删除了网络。

“ECONNREFUSED”可能需要通过tcp进行通信。 在每个服务中,将协议选项设置为tcp:

ports:
      - target: 3000
        published: 3000
        protocol: tcp
        mode: ingress

https://blog.docker.com/2016/12/understanding-docker-networking-drivers-use-cases/ https://docs.docker.com/compose/compose-file/#endpoint_mode