docker容器内的Mongodb连接错误 [英] Mongodb connection error inside docker container

查看:177
本文介绍了docker容器内的Mongodb连接错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试获取基本的nodeJS api以连接到mongo容器.两种服务都在docker-compose.yml文件中定义.我在这里和码头工人论坛上都读过无数类似的问题,所有这些都表明问题是您的mongo连接URI.这不是我的问题,您将在下面看到.

I've been trying to get a basic nodeJS api to connect to a mongo container. Both services are defined in a docker-compose.yml file. I've read countless similar questions here and on docker's forum all stating that the issue is your mongo connection URI. This is not my issue as you'll see below.

docker-compose.yml

version: '3.7'

services: 
  api:
    build: ./
    command: npm run start:dev
    working_dir: /usr/src/api-boiler/
    restart: always
    environment: 
      PORT: 3001
      MONGODB_URI: mongodb://mongodb:27017/TodoApp
      JWT_SECRET: asdkasd9a9sdn2r3513032
    links:
      - mongodb
    ports:
      - "3001:3001"
    volumes:
      - ./:/usr/src/api-boiler/ 
    depends_on:
      - mongodb

  mongodb:
    image: mongo
    restart: always
    volumes:
      - /usr/local/var/mongodb:/data/db
    ports:
      - 27017:27017

Dockerfile

FROM node:10.8.0

WORKDIR /usr/src/api-boiler

COPY ./ ./

RUN npm install

CMD ["/bin/bash"]

db/mongoose.js 设置mongodb连接

const mongoose = require('mongoose');

mongoose.Promise = global.Promise;
mongoose.connect(
  process.env.MONGODB_URI,
  { useMongoClient: true }
);

module.exports.mongoose = mongoose;

但是,无论api容器无法连接什么.我尝试将mongo uri设置为0.0.0.0:3001,但没有任何乐趣.我使用db.serverCmdLineOpts()检查了用于在容器中启动mongo的配置设置.并且,已经传递了命令bind_ip_all,因此mongo应该接受来自任何ip的连接.典型的问题是人们忘记了用其mongo容器名称替换localhost.例如: mongodb://localhost:27017/TodoApp >> mongodb://mongodb:27017/TodoApp

But no matter what the api container cannot connect. I'm tried setting the mongo uri to 0.0.0.0:3001 but no joy. I checked the config settings used to launch mongo in the container using db.serverCmdLineOpts(). And, the command bind_ip_all has been passed so mongo should accept connections from any ip. The typical issue is people forgetting to replace localhost with their mongo container name. EG: mongodb://localhost:27017/TodoApp >> mongodb://mongodb:27017/TodoApp

但是,这已经完成了.好难受.

But, this has been done. So pretty stumped.

日志-采取适当措施

Successfully built 388868008521
Successfully tagged api-boiler_api:latest
Starting api-boiler_mongodb_1 ... done
Recreating api-boiler_api_1   ... done
Attaching to api-boiler_mongodb_1, api-boiler_api_1
mongodb_1  | 2018-08-20T20:09:27.072+0000 I CONTROL  [main]             Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --    sslDisabledProtocols 'none'
mongodb_1  | 2018-08-20T20:09:27.085+0000 I CONTROL  [initandlisten]     MongoDB starting : pid=1 port=27017 dbpath=/data/db 64-bit     host=72af162616c8
mongodb_1  | 2018-08-20T20:09:27.085+0000 I CONTROL  [initandlisten] db     version v4.0.1
mongodb_1  | 2018-08-20T20:09:27.085+0000 I CONTROL  [initandlisten]     git version: 54f1582fc6eb01de4d4c42f26fc133e623f065fb
mongodb_1  | 2018-08-20T20:09:27.085+0000 I CONTROL  [initandlisten]     OpenSSL version: OpenSSL 1.0.2g  1 Mar 2016
mongodb_1  | 2018-08-20T20:09:27.085+0000 I CONTROL  [initandlisten]     allocator: tcmalloc
mongodb_1  | 2018-08-20T20:09:27.085+0000 I CONTROL  [initandlisten]     modules: none
mongodb_1  | 2018-08-20T20:09:27.085+0000 I CONTROL  [initandlisten]     build environment:
mongodb_1  | 2018-08-20T20:09:27.085+0000 I CONTROL  [initandlisten]         distmod: ubuntu1604
mongodb_1  | 2018-08-20T20:09:27.085+0000 I CONTROL  [initandlisten]         distarch: x86_64
mongodb_1  | 2018-08-20T20:09:27.085+0000 I CONTROL  [initandlisten]         target_arch: x86_64
mongodb_1  | 2018-08-20T20:09:27.085+0000 I CONTROL  [initandlisten]     options: { net: { bindIpAll: true } }
mongodb_1  | 2018-08-20T20:09:27.088+0000 W STORAGE  [initandlisten]     Detected unclean shutdown - /data/db/mongod.lock is not empty.
mongodb_1  | 2018-08-20T20:09:27.093+0000 I STORAGE  [initandlisten]     Detected data files in /data/db created by the 'wiredTiger' storage engine,     so setting the active storage engine to 'wiredTiger'.
mongodb_1  | 2018-08-20T20:09:27.096+0000 W STORAGE  [initandlisten]     Recovering data from the last clean checkpoint.
mongodb_1  | 2018-08-20T20:09:27.097+0000 I STORAGE  [initandlisten]     wiredtiger_open config: create,cache_size=487M,session_max=20000,eviction=    (threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=    (enabled=true,archive=true,path=journal,compressor=snappy),file_manager=    (close_idle_time=100000),statistics_log=(wait=0),verbose=    (recovery_progress),
api_1      |
api_1      | > api-boiler@0.1.0 start:dev /usr/src/api-boiler
api_1      | > cross-env NODE_ENV=development node server/server.js
api_1      |
api_1      | Started on port 3001
api_1      | (node:24) UnhandledPromiseRejectionWarning: MongoError:     failed to connect to server [mongodb:27017] on first connect [MongoError:     connect ECONNREFUSED 172.18.0.2:27017]

推荐答案

确定.我已经解决了在此博客的帮助下- https://dev.to/hugodias/wait-for-mongodb-to-start-on-docker-3h8b

OK. I've solved it. With the help of this blog here - https://dev.to/hugodias/wait-for-mongodb-to-start-on-docker-3h8b

您需要等待mongod在容器中完全启动. docker-compose.yml中的Depend_on键不足.

You need to wait for mongod to fully start inside the container. The depend_on key in docker-compose.yml is not sufficient.

您还需要更新Dockerfile才能利用docker-compose-wait.

You'll also need to update your Dockerfile to take advantage of docker-compose-wait.

供参考-这是我更新的docker-compose和Dockerfile文件.

For reference - here is my updated docker-compose and Dockerfile files.

版本:"3.7"

services: 

  api:
    build: ./
    working_dir: /usr/src/api-boiler/
    restart: always
    environment: 
      PORT: 3001
      MONGODB_URI: mongodb://mongodb:27017/TodoApp
      JWT_SECRET: asdkasd9a9sdn2r3513032
    ports:
      - "3001:3001"
    volumes:
      - ./:/usr/src/api-boiler/ 
    depends_on:
      - mongodb
    environment:
      WAIT_HOSTS: mongodb:27017

  mongodb:
    image: mongo
    container_name: mongodb
    restart: always
    volumes:
     - 27017:27017


FROM node:10.8.0

WORKDIR /usr/src/api-boiler

COPY ./ ./

RUN npm install

EXPOSE 3001

## THE LIFE SAVER
ADD https://github.com/ufoscout/docker-compose-    wait/releases/download/2.2.1/wait /wait
RUN chmod +x /wait

# CMD ["/bin/bash"]
CMD /wait && npm run start:dev

这篇关于docker容器内的Mongodb连接错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆