Mongodb connection error inside docker container

2020-02-01 17:37发布

问题:

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 Setting up mongodb connection

const mongoose = require('mongoose');

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

module.exports.mongoose = mongoose;

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.

Logs - for good measure

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]

回答1:

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

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

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

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


version: '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