【问题标题】:cannot connect to MongoDB with Docker-compose无法使用 Docker-compose 连接到 MongoDB
【发布时间】:2020-04-23 03:52:02
【问题描述】:

我正在尝试使用 Docker-compose 创建 2 个容器,其中一个包含 Mongo 服务,另一个包含由 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

这表明端口27017上的mongo服务已经准备好,但是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 连接到 url mongodb://mongo:27017/dockermongo 是 docker-compose 服务的名称。我也尝试了localhost,但没有成功。

我已经在树莓派上试过了,而且效果很好(只需更改 mongo 图像)。 Mongo Image on Pi 有人可以帮忙吗?

【问题讨论】:

  • @Dipakchavda 感谢代表,不幸的是,这些解决方案对我来说太不清楚了。
  • @simpsons3,您传递给 nodejs 应用程序的 mongo 的主机名是什么?
  • WARNING: This server is bound to localhost. Remote systems will be unable to connect to this server. 在这种情况下,“远程”是容器之外的任何东西。您需要如图所示调整配置(或使用 Docker Hub mongo 映像)。
  • @DavidMaze 你能解释的更清楚吗?

标签: node.js mongodb docker docker-compose


【解决方案1】:

为了连接两个容器,首先,你必须了解 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

【讨论】:

  • 这也可能有用:edureka.co/blog/docker-networking
  • 抱歉最近的回复。我刚试过你的解决方案,但还是不行,显示警告WARNING: This server is bound to localhost.
  • 您无法连接到 mongo 还是只是收到警告?
  • 我无法连接到 mongo 并且仍然收到警告。
  • 这表明mongo需要很长时间才能连接,所以我认为问题来自NodeJS中的mongoDB配置,但我不知道如何配置它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-03-06
  • 2021-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-19
  • 2022-12-09
相关资源
最近更新 更多