Не удается подключиться к mongoDB с Nodejs с помощью Docker-compose - PullRequest
1 голос
/ 11 ноября 2019

Я придерживаюсь docker-compose для создания 2-х изображений, одно из которых является приложением nodejs, а другое - mongodb.

Информация о моей ОС

Hardware: Raspberry Pi 3+
OS: Raspbian buster

Это мое дерево папок:

__docker // contain mongodb data
|__docker_mongo // mongo image
|__docker_node // nodejs iamge
|__docker-compose.yml

Докер приложения Node работает без подключения к монго.

Конфигурация узла базы данных:

const url = 'mongodb://172.20.0.3:56086/nodejs';
const url = 'mongodb://mongo:27017/nodejs';

Я пробовал 2 случая выше, но ни один из них не увенчался успехом.

MongoDB Dockerfile

FROM cretzel/rpi-mongodb
WORKDIR /usr/src/mongdb
COPY ./entrypoint.sh .
RUN chmod +x ./entrypoint.sh
ENTRYPOINT ["./entrypoint.sh"]
VOLUME ./database /data/db
EXPOSE 27017
CMD ["mongod"]

entrypoit.sh

#!/bin/bash
service mongod start
exec "$@"

файл docker-compose.yml

version: '3.5'
services:
  web:
    build: ./docker_node/
    ports:
      - "3000:3000"
    command: npm start
    depends_on:
      - mongo
  mongo:
    build: ./docker_mongodb/
    ports:
      - "27017:27017" 

когда я запускаю docker-compose up, после того как построил образ, это журнал:

mongo_1  | Starting database: mongodb failed!
mongo_1  | db level locking enabled: 1
mongo_1  | mongod --help for help and startup options
mongo_1  | Mon Nov 11 17:06:05 
mongo_1  | Mon Nov 11 17:06:05 warning: 32-bit servers don't have journaling enabled by default. Please use --journal if you want durability.
mongo_1  | Mon Nov 11 17:06:05 
mongo_1  | warning: some regex utf8 things will not work.  pcre build doesn't have --enable-unicode-properties
mongo_1  | Mon Nov 11 17:06:05 [initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/data/db/ 32-bit host=07c67d97d0e9
mongo_1  | Mon Nov 11 17:06:05 [initandlisten] 
mongo_1  | Mon Nov 11 17:06:05 [initandlisten] ** NOTE: This is a development version (2.1.1-pre-) of MongoDB.
mongo_1  | Mon Nov 11 17:06:05 [initandlisten] **       Not recommended for production.
mongo_1  | Mon Nov 11 17:06:05 [initandlisten] 
mongo_1  | Mon Nov 11 17:06:05 [initandlisten] ** NOTE: when using MongoDB 32 bit, you are limited to about 2 gigabytes of data
mongo_1  | Mon Nov 11 17:06:05 [initandlisten] **       see http://blog.mongodb.org/post/137788967/32-bit-limitations
mongo_1  | Mon Nov 11 17:06:05 [initandlisten] **       with --journal, the limit is lower
mongo_1  | Mon Nov 11 17:06:05 [initandlisten] 
mongo_1  | Mon Nov 11 17:06:05 [initandlisten] db version v2.1.1-pre-, pdfile version 4.5
mongo_1  | Mon Nov 11 17:06:05 [initandlisten] git version: 47fbbdceb21fc2b791d22db7f01792500647daa9
mongo_1  | Mon Nov 11 17:06:05 [initandlisten] build info: Linux raspberrypi 3.2.27+ #102 PREEMPT Sat Sep 1 01:00:50 BST 2012 armv6l BOOST_LIB_VERSION=1_49
mongo_1  | Mon Nov 11 17:06:05 [initandlisten] options: {}
mongo_1  | Mon Nov 11 17:06:08 [websvr] admin web console waiting for connections on port 28017
mongo_1  | Mon Nov 11 17:06:08 [initandlisten] waiting for connections on port 27017
web_1    | 
web_1    | > init@0.0.0 start /usr/src/app
web_1    | > node ./bin/www
web_1    | 
mongo_1  | Mon Nov 11 17:07:08 [clientcursormon] mem (MB) res:20 virt:83 mapped:0
web_1    | mongodb://172.20.0.3:56086/nodejs
web_1    | Error:  Unable to connect to database 
web_1    | MongoTimeoutError: Server selection timed out after 30000 ms
web_1    |     at mongoose.connect.then.err (/usr/src/app/src/untils/database.js:32:24)

Кажется, service mongod start вызывает эту проблему, но я не знаю, как ее решить. :(

Пожалуйста, помогите.

---------------- ОБНОВЛЕНИЕ --------------------------------------------

Когда конфигурация базы данных const url = 'mongodb://mongo:27017/nodejs', файл журнала:

mongo_1  | Starting database: mongodb failed!
mongo_1  | db level locking enabled: 1
mongo_1  | mongod --help for help and startup options
mongo_1  | Mon Nov 11 18:01:17 
mongo_1  | Mon Nov 11 18:01:17 warning: 32-bit servers don't have journaling enabled by default. Please use --journal if you want durability.
mongo_1  | Mon Nov 11 18:01:17 
mongo_1  | warning: some regex utf8 things will not work.  pcre build doesn't have --enable-unicode-properties
mongo_1  | Mon Nov 11 18:01:17 [initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/data/db/ 32-bit host=07c67d97d0e9
mongo_1  | Mon Nov 11 18:01:17 [initandlisten] 
mongo_1  | Mon Nov 11 18:01:17 [initandlisten] ** NOTE: This is a development version (2.1.1-pre-) of MongoDB.
mongo_1  | Mon Nov 11 18:01:17 [initandlisten] **       Not recommended for production.
mongo_1  | Mon Nov 11 18:01:17 [initandlisten] 
mongo_1  | Mon Nov 11 18:01:17 [initandlisten] ** NOTE: when using MongoDB 32 bit, you are limited to about 2 gigabytes of data
mongo_1  | Mon Nov 11 18:01:17 [initandlisten] **       see http://blog.mongodb.org/post/137788967/32-bit-limitations
mongo_1  | Mon Nov 11 18:01:17 [initandlisten] **       with --journal, the limit is lower
mongo_1  | Mon Nov 11 18:01:17 [initandlisten] 
mongo_1  | Mon Nov 11 18:01:17 [initandlisten] db version v2.1.1-pre-, pdfile version 4.5
mongo_1  | Mon Nov 11 18:01:17 [initandlisten] git version: 47fbbdceb21fc2b791d22db7f01792500647daa9
mongo_1  | Mon Nov 11 18:01:17 [initandlisten] build info: Linux raspberrypi 3.2.27+ #102 PREEMPT Sat Sep 1 01:00:50 BST 2012 armv6l BOOST_LIB_VERSION=1_49
mongo_1  | Mon Nov 11 18:01:17 [initandlisten] options: {}
mongo_1  | Mon Nov 11 18:01:17 [websvr] admin web console waiting for connections on port 28017
mongo_1  | Mon Nov 11 18:01:17 [initandlisten] waiting for connections on port 27017
web_1    | 
web_1    | > init@0.0.0 start /usr/src/app
web_1    | > node ./bin/www
web_1    | 
mongo_1  | Mon Nov 11 18:02:09 [initandlisten] connection accepted from 172.20.0.3:35650 #1 (1 connection now open)
mongo_1  | Mon Nov 11 18:02:09 [conn1] end connection 172.20.0.3:35650 (0 connections now open)
mongo_1  | Mon Nov 11 18:02:09 [initandlisten] connection accepted from 172.20.0.3:35652 #2 (1 connection now open)
mongo_1  | Mon Nov 11 18:02:09 [conn2] end connection 172.20.0.3:35652 (0 connections now open)
mongo_1  | Mon Nov 11 18:02:10 [initandlisten] connection accepted from 172.20.0.3:35654 #3 (1 connection now open)
mongo_1  | Mon Nov 11 18:02:10 [conn3] end connection 172.20.0.3:35654 (0 connections now open)
mongo_1  | Mon Nov 11 18:02:10 [initandlisten] connection accepted from 172.20.0.3:35656 #4 (1 connection now open)
mongo_1  | Mon Nov 11 18:02:10 [conn4] end connection 172.20.0.3:35656 (0 connections now open)
mongo_1  | Mon Nov 11 18:02:11 [initandlisten] connection accepted from 172.20.0.3:35658 #5 (1 connection now open)
mongo_1  | Mon Nov 11 18:02:11 [conn5] end connection 172.20.0.3:35658 (0 connections now open)
mongo_1  | Mon Nov 11 18:02:11 [initandlisten] connection accepted from 172.20.0.3:35660 #6 (1 connection now open)
mongo_1  | Mon Nov 11 18:02:11 [conn6] end connection 172.20.0.3:35660 (0 connections now open)
mongo_1  | Mon Nov 11 18:02:12 [initandlisten] connection accepted from 172.20.0.3:35662 #7 (1 connection now open)
mongo_1  | Mon Nov 11 18:02:12 [conn7] end connection 172.20.0.3:35662 (0 connections now open)
mongo_1  | Mon Nov 11 18:02:12 [initandlisten] connection accepted from 172.20.0.3:35664 #8 (1 connection now open)
mongo_1  | Mon Nov 11 18:02:12 [conn8] end connection 172.20.0.3:35664 (0 connections now open)
mongo_1  | Mon Nov 11 18:02:13 [initandlisten] connection accepted from 172.20.0.3:35666 #9 (1 connection now open)
mongo_1  | Mon Nov 11 18:02:13 [conn9] end connection 172.20.0.3:35666 (0 connections now open)
mongo_1  | Mon Nov 11 18:02:13 [initandlisten] connection accepted from 172.20.0.3:35668 #10 (1 connection now open)
mongo_1  | Mon Nov 11 18:02:13 [conn10] end connection 172.20.0.3:35668 (0 connections now open)
mongo_1  | Mon Nov 11 18:02:14 [initandlisten] connection accepted from 172.20.0.3:35670 #11 (1 connection now open)
mongo_1  | Mon Nov 11 18:02:14 [conn11] end connection 172.20.0.3:35670 (0 connections now open)
mongo_1  | Mon Nov 11 18:02:14 [initandlisten] connection accepted from 172.20.0.3:35672 #12 (1 connection now open)
mongo_1  | Mon Nov 11 18:02:14 [conn12] end connection 172.20.0.3:35672 (0 connections now open)
mongo_1  | Mon Nov 11 18:02:15 [initandlisten] connection accepted from 172.20.0.3:35674 #13 (1 connection now open)
mongo_1  | Mon Nov 11 18:02:15 [conn13] end connection 172.20.0.3:35674 (0 connections now open)
mongo_1  | Mon Nov 11 18:02:15 [initandlisten] connection accepted from 172.20.0.3:35676 #14 (1 connection now open)
mongo_1  | Mon Nov 11 18:02:15 [conn14] end connection 172.20.0.3:35676 (0 connections now open)
mongo_1  | Mon Nov 11 18:02:16 [initandlisten] connection accepted from 172.20.0.3:35678 #15 (1 connection now open)
mongo_1  | Mon Nov 11 18:02:16 [conn15] end connection 172.20.0.3:35678 (0 connections now open)
mongo_1  | Mon Nov 11 18:02:16 [initandlisten] connection accepted from 172.20.0.3:35680 #16 (1 connection now open)
mongo_1  | Mon Nov 11 18:02:16 [conn16] end connection 172.20.0.3:35680 (0 connections now open)
mongo_1  | Mon Nov 11 18:02:17 [clientcursormon] mem (MB) res:19 virt:84 mapped:0
mongo_1  | Mon Nov 11 18:02:17 [initandlisten] connection accepted from 172.20.0.3:35682 #17 (1 connection now open)
mongo_1  | Mon Nov 11 18:02:17 [conn17] end connection 172.20.0.3:35682 (0 connections now open)
mongo_1  | Mon Nov 11 18:02:18 [initandlisten] connection accepted from 172.20.0.3:35684 #18 (1 connection now open)
mongo_1  | Mon Nov 11 18:02:18 [conn18] end connection 172.20.0.3:35684 (0 connections now open)
mongo_1  | Mon Nov 11 18:02:18 [initandlisten] connection accepted from 172.20.0.3:35686 #19 (1 connection now open)
mongo_1  | Mon Nov 11 18:02:18 [conn19] end connection 172.20.0.3:35686 (0 connections now open)
mongo_1  | Mon Nov 11 18:02:19 [initandlisten] connection accepted from 172.20.0.3:35688 #20 (1 connection now open)
mongo_1  | Mon Nov 11 18:02:19 [conn20] end connection 172.20.0.3:35688 (0 connections now open)
mongo_1  | Mon Nov 11 18:02:19 [initandlisten] connection accepted from 172.20.0.3:35690 #21 (1 connection now open)
mongo_1  | Mon Nov 11 18:02:19 [conn21] end connection 172.20.0.3:35690 (0 connections now open)
mongo_1  | Mon Nov 11 18:02:20 [initandlisten] connection accepted from 172.20.0.3:35692 #22 (1 connection now open)
mongo_1  | Mon Nov 11 18:02:20 [conn22] end connection 172.20.0.3:35692 (0 connections now open)
mongo_1  | Mon Nov 11 18:02:20 [initandlisten] connection accepted from 172.20.0.3:35694 #23 (1 connection now open)
mongo_1  | Mon Nov 11 18:02:20 [conn23] end connection 172.20.0.3:35694 (0 connections now open)
mongo_1  | Mon Nov 11 18:02:21 [initandlisten] connection accepted from 172.20.0.3:35696 #24 (1 connection now open)
mongo_1  | Mon Nov 11 18:02:21 [conn24] end connection 172.20.0.3:35696 (0 connections now open)
mongo_1  | Mon Nov 11 18:02:21 [initandlisten] connection accepted from 172.20.0.3:35698 #25 (1 connection now open)
mongo_1  | Mon Nov 11 18:02:21 [conn25] end connection 172.20.0.3:35698 (0 connections now open)
mongo_1  | Mon Nov 11 18:02:22 [initandlisten] connection accepted from 172.20.0.3:35700 #26 (1 connection now open)
mongo_1  | Mon Nov 11 18:02:22 [conn26] end connection 172.20.0.3:35700 (0 connections now open)
mongo_1  | Mon Nov 11 18:02:22 [initandlisten] connection accepted from 172.20.0.3:35702 #27 (1 connection now open)
mongo_1  | Mon Nov 11 18:02:22 [conn27] end connection 172.20.0.3:35702 (0 connections now open)
mongo_1  | Mon Nov 11 18:02:23 [initandlisten] connection accepted from 172.20.0.3:35704 #28 (1 connection now open)
mongo_1  | Mon Nov 11 18:02:23 [conn28] end connection 172.20.0.3:35704 (0 connections now open)
mongo_1  | Mon Nov 11 18:02:23 [initandlisten] connection accepted from 172.20.0.3:35706 #29 (1 connection now open)
mongo_1  | Mon Nov 11 18:02:23 [conn29] end connection 172.20.0.3:35706 (0 connections now open)
mongo_1  | Mon Nov 11 18:02:24 [initandlisten] connection accepted from 172.20.0.3:35708 #30 (1 connection now open)
mongo_1  | Mon Nov 11 18:02:24 [conn30] end connection 172.20.0.3:35708 (0 connections now open)
mongo_1  | Mon Nov 11 18:02:24 [initandlisten] connection accepted from 172.20.0.3:35710 #31 (1 connection now open)
mongo_1  | Mon Nov 11 18:02:24 [conn31] end connection 172.20.0.3:35710 (0 connections now open)
mongo_1  | Mon Nov 11 18:02:25 [initandlisten] connection accepted from 172.20.0.3:35712 #32 (1 connection now open)
mongo_1  | Mon Nov 11 18:02:25 [conn32] end connection 172.20.0.3:35712 (0 connections now open)
mongo_1  | Mon Nov 11 18:02:25 [initandlisten] connection accepted from 172.20.0.3:35714 #33 (1 connection now open)
mongo_1  | Mon Nov 11 18:02:25 [conn33] end connection 172.20.0.3:35714 (0 connections now open)
mongo_1  | Mon Nov 11 18:02:26 [initandlisten] connection accepted from 172.20.0.3:35716 #34 (1 connection now open)
mongo_1  | Mon Nov 11 18:02:26 [conn34] end connection 172.20.0.3:35716 (0 connections now open)
mongo_1  | Mon Nov 11 18:02:26 [initandlisten] connection accepted from 172.20.0.3:35718 #35 (1 connection now open)
mongo_1  | Mon Nov 11 18:02:26 [conn35] end connection 172.20.0.3:35718 (0 connections now open)
mongo_1  | Mon Nov 11 18:02:27 [initandlisten] connection accepted from 172.20.0.3:35720 #36 (1 connection now open)
mongo_1  | Mon Nov 11 18:02:27 [conn36] end connection 172.20.0.3:35720 (0 connections now open)
mongo_1  | Mon Nov 11 18:02:27 [initandlisten] connection accepted from 172.20.0.3:35722 #37 (1 connection now open)
mongo_1  | Mon Nov 11 18:02:27 [conn37] end connection 172.20.0.3:35722 (0 connections now open)
mongo_1  | Mon Nov 11 18:02:28 [initandlisten] connection accepted from 172.20.0.3:35724 #38 (1 connection now open)
mongo_1  | Mon Nov 11 18:02:28 [conn38] end connection 172.20.0.3:35724 (0 connections now open)
mongo_1  | Mon Nov 11 18:02:28 [initandlisten] connection accepted from 172.20.0.3:35726 #39 (1 connection now open)
mongo_1  | Mon Nov 11 18:02:28 [conn39] end connection 172.20.0.3:35726 (0 connections now open)
mongo_1  | Mon Nov 11 18:02:29 [initandlisten] connection accepted from 172.20.0.3:35728 #40 (1 connection now open)
mongo_1  | Mon Nov 11 18:02:29 [conn40] end connection 172.20.0.3:35728 (0 connections now open)
mongo_1  | Mon Nov 11 18:02:29 [initandlisten] connection accepted from 172.20.0.3:35730 #41 (1 connection now open)
mongo_1  | Mon Nov 11 18:02:29 [conn41] end connection 172.20.0.3:35730 (0 connections now open)
mongo_1  | Mon Nov 11 18:02:30 [initandlisten] connection accepted from 172.20.0.3:35732 #42 (1 connection now open)
mongo_1  | Mon Nov 11 18:02:30 [conn42] end connection 172.20.0.3:35732 (0 connections now open)
mongo_1  | Mon Nov 11 18:02:30 [initandlisten] connection accepted from 172.20.0.3:35734 #43 (1 connection now open)
mongo_1  | Mon Nov 11 18:02:30 [conn43] end connection 172.20.0.3:35734 (0 connections now open)
mongo_1  | Mon Nov 11 18:02:31 [initandlisten] connection accepted from 172.20.0.3:35736 #44 (1 connection now open)
mongo_1  | Mon Nov 11 18:02:31 [conn44] end connection 172.20.0.3:35736 (0 connections now open)
mongo_1  | Mon Nov 11 18:02:31 [initandlisten] connection accepted from 172.20.0.3:35738 #45 (1 connection now open)
mongo_1  | Mon Nov 11 18:02:31 [conn45] end connection 172.20.0.3:35738 (0 connections now open)
mongo_1  | Mon Nov 11 18:02:32 [initandlisten] connection accepted from 172.20.0.3:35740 #46 (1 connection now open)
mongo_1  | Mon Nov 11 18:02:32 [conn46] end connection 172.20.0.3:35740 (0 connections now open)
mongo_1  | Mon Nov 11 18:02:32 [initandlisten] connection accepted from 172.20.0.3:35742 #47 (1 connection now open)
mongo_1  | Mon Nov 11 18:02:32 [conn47] end connection 172.20.0.3:35742 (0 connections now open)
mongo_1  | Mon Nov 11 18:02:33 [initandlisten] connection accepted from 172.20.0.3:35744 #48 (1 connection now open)
mongo_1  | Mon Nov 11 18:02:33 [conn48] end connection 172.20.0.3:35744 (0 connections now open)
mongo_1  | Mon Nov 11 18:02:33 [initandlisten] connection accepted from 172.20.0.3:35746 #49 (1 connection now open)
mongo_1  | Mon Nov 11 18:02:33 [conn49] end connection 172.20.0.3:35746 (0 connections now open)
mongo_1  | Mon Nov 11 18:02:34 [initandlisten] connection accepted from 172.20.0.3:35748 #50 (1 connection now open)
mongo_1  | Mon Nov 11 18:02:34 [conn50] end connection 172.20.0.3:35748 (0 connections now open)
mongo_1  | Mon Nov 11 18:02:34 [initandlisten] connection accepted from 172.20.0.3:35750 #51 (1 connection now open)
mongo_1  | Mon Nov 11 18:02:34 [conn51] end connection 172.20.0.3:35750 (0 connections now open)
mongo_1  | Mon Nov 11 18:02:35 [initandlisten] connection accepted from 172.20.0.3:35752 #52 (1 connection now open)
mongo_1  | Mon Nov 11 18:02:35 [conn52] end connection 172.20.0.3:35752 (0 connections now open)
mongo_1  | Mon Nov 11 18:02:35 [initandlisten] connection accepted from 172.20.0.3:35754 #53 (1 connection now open)
mongo_1  | Mon Nov 11 18:02:35 [conn53] end connection 172.20.0.3:35754 (0 connections now open)
mongo_1  | Mon Nov 11 18:02:36 [initandlisten] connection accepted from 172.20.0.3:35756 #54 (1 connection now open)
mongo_1  | Mon Nov 11 18:02:36 [conn54] end connection 172.20.0.3:35756 (0 connections now open)
mongo_1  | Mon Nov 11 18:02:36 [initandlisten] connection accepted from 172.20.0.3:35758 #55 (1 connection now open)
mongo_1  | Mon Nov 11 18:02:36 [conn55] end connection 172.20.0.3:35758 (0 connections now open)
mongo_1  | Mon Nov 11 18:02:37 [initandlisten] connection accepted from 172.20.0.3:35760 #56 (1 connection now open)
mongo_1  | Mon Nov 11 18:02:37 [conn56] end connection 172.20.0.3:35760 (0 connections now open)
mongo_1  | Mon Nov 11 18:02:37 [initandlisten] connection accepted from 172.20.0.3:35762 #57 (1 connection now open)
mongo_1  | Mon Nov 11 18:02:37 [conn57] end connection 172.20.0.3:35762 (0 connections now open)
mongo_1  | Mon Nov 11 18:02:38 [initandlisten] connection accepted from 172.20.0.3:35764 #58 (1 connection now open)
mongo_1  | Mon Nov 11 18:02:38 [conn58] end connection 172.20.0.3:35764 (0 connections now open)
mongo_1  | Mon Nov 11 18:02:39 [initandlisten] connection accepted from 172.20.0.3:35766 #59 (1 connection now open)
mongo_1  | Mon Nov 11 18:02:39 [conn59] end connection 172.20.0.3:35766 (0 connections now open)
web_1    | mongodb://mongo:27017/nodejs
web_1    | Error:  Unable to connect to database 
web_1    | MongoTimeoutError: Server selection timed out after 30000 ms
web_1    |     at mongoose.connect.then.err (/usr/src/app/src/untils/database.js:32:24)

1 Ответ

1 голос
/ 11 ноября 2019

Решено, изменить cretzel/rpi-mongodb на andresvidal/rpi3-mongodb3

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...