gpt4 book ai didi

node.js - 无法通过Docker-compose连接到MongoDB

转载 作者:行者123 更新时间:2023-12-02 18:27:00 25 4
gpt4 key购买 nike

我正在尝试使用Docker-compose创建2个容器,其中一个包含Mongo serive,另一个包含NodeJS编写的Web服务。
这是我的docker-compose.yml文件:

version: '3'
services:
mongo:
build: ./docker_mongo/
volumes:
- ./database:/data/db
ports:
- "27017:27017"
node_app:
build: ./docker_node/
ports:
- "3001:3001"
- "16427:16427"
command: npm start
environment:
- OS:PI
depends_on:
- mongo
- mqtt


当我运行 docker-compose时,mongo服务显示以下消息:
mongo_1      | 2020-01-05T07:36:39.699+0000 I CONTROL  [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
mongo_1 | 2020-01-05T07:36:39.703+0000 I CONTROL [initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/data/db 64-bit host=90cbb91eebd0
mongo_1 | 2020-01-05T07:36:39.703+0000 I CONTROL [initandlisten] db version v4.0.14
mongo_1 | 2020-01-05T07:36:39.703+0000 I CONTROL [initandlisten] git version: 1622021384533dade8b3c89ed3ecd80e1142c132
mongo_1 | 2020-01-05T07:36:39.703+0000 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.2g 1 Mar 2016
mongo_1 | 2020-01-05T07:36:39.703+0000 I CONTROL [initandlisten] allocator: tcmalloc
mongo_1 | 2020-01-05T07:36:39.703+0000 I CONTROL [initandlisten] modules: none
mongo_1 | 2020-01-05T07:36:39.703+0000 I CONTROL [initandlisten] build environment:
mongo_1 | 2020-01-05T07:36:39.703+0000 I CONTROL [initandlisten] distmod: ubuntu1604
mongo_1 | 2020-01-05T07:36:39.703+0000 I CONTROL [initandlisten] distarch: x86_64
mongo_1 | 2020-01-05T07:36:39.703+0000 I CONTROL [initandlisten] target_arch: x86_64
mongo_1 | 2020-01-05T07:36:39.703+0000 I CONTROL [initandlisten] options: {}
mongo_1 | 2020-01-05T07:36:39.708+0000 I STORAGE [initandlisten]
mongo_1 | 2020-01-05T07:36:39.708+0000 I STORAGE [initandlisten] ** WARNING: Support for MMAPV1 storage engine has been deprecated and will be
mongo_1 | 2020-01-05T07:36:39.708+0000 I STORAGE [initandlisten] ** removed in version 4.2. Please plan to migrate to the wiredTiger
mongo_1 | 2020-01-05T07:36:39.708+0000 I STORAGE [initandlisten] ** storage engine.
mongo_1 | 2020-01-05T07:36:39.708+0000 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/deprecated-mmapv1
mongo_1 | 2020-01-05T07:36:39.708+0000 I STORAGE [initandlisten]
mongo_1 | 2020-01-05T07:36:39.708+0000 I STORAGE [initandlisten] Detected data files in /data/db created by the 'mmapv1' storage engine, so setting the active storage engine to 'mmapv1'.
mongo_1 | 2020-01-05T07:36:39.727+0000 I JOURNAL [initandlisten] journal dir=/data/db/journal
mongo_1 | 2020-01-05T07:36:39.728+0000 I JOURNAL [initandlisten] recover : no journal files present, no recovery needed
mongo_1 | 2020-01-05T07:36:39.730+0000 I STORAGE [initandlisten] WARNING: This file system is not supported. For further information see:
mongo_1 | 2020-01-05T07:36:39.730+0000 I STORAGE [initandlisten] http://dochub.mongodb.org/core/unsupported-filesystems
mongo_1 | 2020-01-05T07:36:39.730+0000 I STORAGE [initandlisten] Please notify MongoDB, Inc. if an unlisted filesystem generated this warning.
mongo_1 | 2020-01-05T07:36:39.970+0000 I JOURNAL [durability] Durability thread started
mongo_1 | 2020-01-05T07:36:39.971+0000 I JOURNAL [journal writer] Journal writer thread started
mongo_1 | 2020-01-05T07:36:40.003+0000 I STORAGE [initandlisten] WARNING: This file system is not supported. For further information see:
mongo_1 | 2020-01-05T07:36:40.003+0000 I STORAGE [initandlisten] http://dochub.mongodb.org/core/unsupported-filesystems
mongo_1 | 2020-01-05T07:36:40.003+0000 I STORAGE [initandlisten] Please notify MongoDB, Inc. if an unlisted filesystem generated this warning.
mongo_1 | 2020-01-05T07:36:40.003+0000 I CONTROL [initandlisten]
mongo_1 | 2020-01-05T07:36:40.003+0000 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
mongo_1 | 2020-01-05T07:36:40.003+0000 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
mongo_1 | 2020-01-05T07:36:40.003+0000 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
mongo_1 | 2020-01-05T07:36:40.003+0000 I CONTROL [initandlisten]
mongo_1 | 2020-01-05T07:36:40.003+0000 I CONTROL [initandlisten] ** WARNING: This server is bound to localhost.
mongo_1 | 2020-01-05T07:36:40.003+0000 I CONTROL [initandlisten] ** Remote systems will be unable to connect to this server.
mongo_1 | 2020-01-05T07:36:40.003+0000 I CONTROL [initandlisten] ** Start the server with --bind_ip <address> to specify which IP
mongo_1 | 2020-01-05T07:36:40.003+0000 I CONTROL [initandlisten] ** addresses it should serve responses from, or with --bind_ip_all to
mongo_1 | 2020-01-05T07:36:40.003+0000 I CONTROL [initandlisten] ** bind to all interfaces. If this behavior is desired, start the
mongo_1 | 2020-01-05T07:36:40.003+0000 I CONTROL [initandlisten] ** server with --bind_ip 127.0.0.1 to disable this warning.
mongo_1 | 2020-01-05T07:36:40.003+0000 I CONTROL [initandlisten]
mongo_1 | 2020-01-05T07:36:40.308+0000 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
mongo_1 | 2020-01-05T07:36:40.312+0000 I NETWORK [initandlisten] waiting for connections on port 27017

这表明mongo服务已在 27017端口上就绪,但NodeJs服务器仍无法访问数据库:
node_app_1   | Error:  Unable to connect to database
node_app_1 | MongoTimeoutError: Server selection timed out after 30000 ms
node_app_1 | at /node_app/app.js:84:20

在NodeJs服务器中,mongoDb使用docker-compose服务的名称连接到 mongodb://mongo:27017/docker的URL mongo。我也尝试了 localhost,但是没有用。

我已经在树莓派上尝试过了,并且可以正常工作(只是更改 mongo图像)。
Mongo Image on Pi
有人可以帮忙吗?

最佳答案

为了连接两个容器,首先,您必须了解Docker网络。您收到错误消息,可能是因为容器是隔离的。您可以通过多种方式使用不同的联网模式连接Docker容器,例如:

  • 仅限主机的网络
  • 桥接网络

  • 在此处阅读更多信息 https://docs.docker.com/network/

    您可以如下配置仅主机网络:
    为每个服务包括 network_mode: host
      version: '3'
    services:
    mongo:
    build: ./docker_mongo/
    network_mode: host
    volumes:
    - ./database:/data/db
    ports:
    - "27017:27017"
    node_app:
    build: ./docker_node/
    network_mode: host
    ports:
    - "3001:3001"
    - "16427:16427"
    command: npm start
    environment:
    - OS:PI
    depends_on:
    - mongo
    - mqtt

    关于node.js - 无法通过Docker-compose连接到MongoDB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59597944/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com