【问题标题】:mongodb and mongo-express not connecting with docker-composemongodb 和 mongo-express 没有与 docker-compose 连接
【发布时间】:2023-02-17 02:23:19
【问题描述】:

我是学习 docker 的新手,在这里学习。这是我的 compose.yaml 文件,其中包含 mongo 和 mongo-express 最新版本用例。 mongo-express 没有连接到 mongodb,尝试了 mongo-express 中的重新启动功能,它继续重新启动 mongo-express,我就足够了,它没有与 mongodb 连接。我试过给予网络并没有改变任何东西。

version: "3"
services:
  mongodb:
    image: mongo
    ports:
      - "27017:27017"
    environment:
      - MONGO_INITDB_ROOT_USERNAME=admin
      - MONGO_INITDB_ROOT_PASSWORD=password
    # networks:
    #   - mongo-network
  mongo-express:
    image: mongo-express
    # restart: on-failure
    ports:
      - "8081:8081"
    environment:
      - ME_CONFIG_MONGODB_ADMINUSERNAME=admin
      - ME_CONFIG_MONGODB_ADMINPASSWORD=password
      - ME_CONFIG_MONGODB_ADMINSERVER=mongodb
      - ME_CONFIG_MONGODB_PORT=27017
    # networks:
    #   - mongo-network
    depends_on:
      - mongodb

这是我从 mongo-express 容器进入命令提示符的错误:

2022-11-15 10:39:21 Welcome to mongo-express
2022-11-15 10:39:21 ------------------------
2022-11-15 10:39:21 
2022-11-15 10:39:21 
2022-11-15 10:39:21 (node:7) [MONGODB DRIVER] Warning: Current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
2022-11-15 10:39:26 Could not connect to database using connectionString: mongodb://mongo:27017"
2022-11-15 10:39:26 (node:7) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [mongo:27017] on first connect [Error: getaddrinfo EAI_AGAIN mongo
2022-11-15 10:39:26     at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:66:26) {
2022-11-15 10:39:26   name: 'MongoNetworkError'
2022-11-15 10:39:26 }]
2022-11-15 10:39:26     at Pool.<anonymous> (/node_modules/mongodb/lib/core/topologies/server.js:441:11)
2022-11-15 10:39:26     at Pool.emit (events.js:314:20)
2022-11-15 10:39:26     at /node_modules/mongodb/lib/core/connection/pool.js:564:14
2022-11-15 10:39:26     at /node_modules/mongodb/lib/core/connection/pool.js:1000:11
2022-11-15 10:39:26     at /node_modules/mongodb/lib/core/connection/connect.js:32:7
2022-11-15 10:39:26     at callback (/node_modules/mongodb/lib/core/connection/connect.js:300:5)
2022-11-15 10:39:26     at Socket.<anonymous> (/node_modules/mongodb/lib/core/connection/connect.js:330:7)
2022-11-15 10:39:26     at Object.onceWrapper (events.js:421:26)
2022-11-15 10:39:26     at Socket.emit (events.js:314:20)
2022-11-15 10:39:26     at emitErrorNT (internal/streams/destroy.js:92:8)
2022-11-15 10:39:26     at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
2022-11-15 10:39:26     at processTicksAndRejections (internal/process/task_queues.js:84:21)
2022-11-15 10:39:26 (node:7) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
2022-11-15 10:39:26 (node:7) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

【问题讨论】:

  • 请编辑您的问题并添加代码、日志、输出、错误消息...在问题正文中作为代码块.为此使用图像有 numerous disadvantages 并且在 How to Ask 中被明确列为不良做法。谢谢
  • @Zeitounator 感谢您告诉我,我下次会记住它,但从 zsolt 得到了答案,真不敢相信我在重新检查了这么多次之后没有注意到这个错误。
  • 您的问题得到解答后,您仍然可以edit您的问题。

标签: mongodb docker docker-compose mongo-express


【解决方案1】:

根据文档,服务器环境变量不是 ME_CONFIG_MONGODB_ADMINSERVER 而是 ME_CONFIG_MONGODB_SERVER: https://hub.docker.com/_/mongo-express

您还需要为 mongodb 添加健康检查并使 mongo-express 依赖于它,因为节点在第一次连接失败后以退出代码 0 退出,如日志中所述。如果它将以非零退出代码退出,只要它没有失败,docker 就会尝试重新启动容器。它在日志中说,这将是未来的方式,但现在你需要健康检查。

(节点:7)[DEP0018] DeprecationWarning:未处理的承诺拒绝 已弃用。将来,未处理的承诺拒绝 将以非零退出代码终止 Node.js 进程。

所以这个撰写文件有效:

version: "3"
services:
  mongodb:
    image: mongo
    ports:
      - "27017:27017"
    environment:
      - MONGO_INITDB_ROOT_USERNAME=admin
      - MONGO_INITDB_ROOT_PASSWORD=password
    healthcheck:
      test:  echo 'db.runCommand("ping").ok' | mongosh localhost:27017/test --quiet
      interval: 10s
      timeout: 10s
      retries: 5
      start_period: 10s
  mongo-express:
    image: mongo-express
    # restart: on-failure
    ports:
      - "8081:8081"
    environment:
      - ME_CONFIG_MONGODB_ADMINUSERNAME=admin
      - ME_CONFIG_MONGODB_ADMINPASSWORD=password
      - ME_CONFIG_MONGODB_SERVER=mongodb
      - ME_CONFIG_MONGODB_PORT=27017
    depends_on:
      mongodb:
        condition: service_healthy

【讨论】:

    【解决方案2】:

    这对我有用:

    版本:“3.8”
    服务:
      ps_mongodb:
        图片:mongo:最新
        容器名称:ps_mongodb
        环境:
            - MONGO_INITDB_ROOT_USERNAME=adminUsr
            - MONGO_INITDB_ROOT_PASSWORD=adminPwd
        健康检查:
          测试: echo 'db.runCommand("ping").ok' | mongosh localhost:27021/admin --quiet
          间隔:10s
          超时:10s
          重试:5
          start_period: 10s
        网络:
          - ps-mongo-网络
        重启:总是
        命令:mongod --auth --port 27021
        端口:
          - “27021:27021”
        卷:
          - ./数据库/数据库:/数据/数据库
          - ./database/dev.archive:/Databases/dev.archive
          - ./database/production:/Databases/production
          #- ./mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro
          # mongo.dbHost=172.17.0.1 用于在终端中使用 :>ifconfig 进行本地访问
      ps_mongoexp:
        图片:mongo-express
        容器名称:ps_mongoexp
        重启:总是
        依赖于取决于:
          ps_mongodb:
            条件:service_healthy
        环境:
          - ME_CONFIG_MONGODB_ADMINUSERNAME=adminUsr
          - ME_CONFIG_MONGODB_ADMINPASSWORD=adminPwd
          - ME_CONFIG_MONGODB_EN​​ABLE_ADMIN=true
          - ME_CONFIG_MONGODB_SERVER=ps_mongodb
          - ME_CONFIG_MONGODB_PORT=27021
          - ME_CONFIG_MONGODB_URL=mongodb://adminUsr:adminPwd@ps_mongodb:27021/?authSource=admin
          - ME_CONFIG_BASICAUTH_USERNAME=admin
          - ME_CONFIG_BASICAUTH_PASSWORD=admin
        网络:
          - ps-mongo-网络
        端口:
          - “8081:8081”
    网络:
      ps-mongo-网络:
        司机:桥

    【讨论】:

      猜你喜欢
      • 2021-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-04
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      • 2018-09-02
      相关资源
      最近更新 更多