【问题标题】:Cant reach mongo db container无法访问 mongodb 容器
【发布时间】:2020-03-14 15:26:50
【问题描述】:

我试图通过 article 实现一个基于 dockerized mongo/node 的应用程序。

docker-compose.yml

version: '3'
services:
  gecar_app:
    build: 
      context: . 
      dockerfile: Dockerfile.gecars
    restart: always
    ports:
      - 3000:3000
    depends_on:
      - mongo
    command: /usr/app/initAndStartService.sh
  mongo:
    container_name: mongo
    image: mongo
    volumes:
      - ./data:/data/db
    ports:
      - "27017:27017"

Dockerfile.gercars

FROM node:latest
RUN mkdir -p /usr/app
WORKDIR /usr/app
COPY src/ /usr/app/src/
COPY package.json /usr/app/package.json
COPY webpack.config.js /usr/app/webpack.config.js
COPY tsconfig.json /usr/app/tsconfig.json
COPY initAndStartService.sh /usr/app/initAndStartService.sh
RUN yarn
RUN yarn build

RUN ["chmod", "+x", "/usr/app/initAndStartService.sh"]

initAndStartService.sh

#!/bin/sh
curl 127.0.0.1:27017

但是当运行docker-compose up 时得到这个:

mongo        | 2019-11-19T06:13:21.682+0000 I  NETWORK  [initandlisten] Listening on 0.0.0.0
mongo        | 2019-11-19T06:13:21.683+0000 I  NETWORK  [initandlisten] waiting for connections on port 27017

.......

gecar_app_1  |                                  Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0curl: (7) Failed to connect to 127.0.0.1 port 27017: Connection refused

如果我正确识别情况,gecar_app_1容器可以t reachmongo`容器。

【问题讨论】:

    标签: node.js mongodb docker docker-compose


    【解决方案1】:

    依赖

    表达服务之间的依赖关系,服务依赖关系导致 以下行为:

    docker-compose up starts services in dependency order. In the following example, db and redis are started before web.
    
    docker-compose up SERVICE automatically includes SERVICE’s dependencies. In the following example, docker-compose up web also
    

    创建并启动 db 和 redis。

    docker-compose stop stops services in dependency order. In the following example, web is stopped before db and redis.
    

    也就是说,当你想和mongo容器通信时,你只需要根据你的容器服务命名将它称为mongo即可。

    所以 gecar_app 容器中的 curl mongo:27017 可以解决问题

    【讨论】:

      【解决方案2】:

      127.0.0.1 是一个环回地址,这意味着当您在gecar_app 容器上运行curl 127.0.0.1:27017 时,您尝试连接到自己,但gecar_app 没有在27017 端口上运行任何服务。你需要连接的是mongo服务(我想是的)。

      您使用depends_on 等待mongo 服务启动,然后启动gecar_app 容器(好点),现在mongo 将成为主机名,gecar_app 可以连接到@987654332 @通过mongo“域”。

      但是,gecar_app 可能已经在 mongodb 服务之前启动了,那么你必须确保当 mongo 服务已经启动。您可以使用healthcheck 属性来做到这一点。

      最后, docker-compose.yml

      version: '3'
      services:
        gecar_app:
          build: 
            context: . 
            dockerfile: Dockerfile.gecars
          restart: always
          ports:
            - 3000:3000
          depends_on:
            - mongo
          command: /usr/app/initAndStartService.sh
        mongo:
          container_name: mongo
          image: mongo
          volumes:
            - ./data:/data/db
          ports:
            - "27017:27017"
          healthcheck:
            test: echo 'db.runCommand("ping").ok' | mongo mongo:27017/test --quiet 1
            interval: 3s
            timeout: 5s
            retries: 5
      

      还有initAndStartService.sh

      #!/bin/sh
      curl mongo:27017
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-12-07
        • 2020-05-26
        • 2016-03-18
        • 2018-08-09
        • 1970-01-01
        • 2015-06-06
        • 1970-01-01
        • 2018-10-10
        相关资源
        最近更新 更多