一尘不染

docker容器内的Mongodb连接错误

docker

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

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

Docker文件

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。EG:mongodb://
localhost:27017 / TodoApp >> mongodb:// mongodb:27017 / TodoApp

但是,这已经完成。好难受。

日志-很好的措施

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]

阅读 937

收藏
2020-06-17

共1个答案

一尘不染

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

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

您还需要更新Dockerfile以利用docker-compose-wait。

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


版本:“ 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
2020-06-17