mongodb和mongo-express未与docker-compose连接

u3r8eeie  于 2022-11-22  发布在  Docker
关注(0)|答案(1)|浏览(287)

我刚开始学习docker,在这里学习了stuch。这是我的compose.yaml文件,里面有mongo和mongo-express最新版本的用例。mongo-express没有连接到mongodb,尝试了mongo-express中的重启功能,它一直在重启mongo-express,我觉得它没有连接到mongodb。我尝试了网络,但没有改变任何东西。

  1. version: "3"
  2. services:
  3. mongodb:
  4. image: mongo
  5. ports:
  6. - "27017:27017"
  7. environment:
  8. - MONGO_INITDB_ROOT_USERNAME=admin
  9. - MONGO_INITDB_ROOT_PASSWORD=password
  10. # networks:
  11. # - mongo-network
  12. mongo-express:
  13. image: mongo-express
  14. # restart: on-failure
  15. ports:
  16. - "8081:8081"
  17. environment:
  18. - ME_CONFIG_MONGODB_ADMINUSERNAME=admin
  19. - ME_CONFIG_MONGODB_ADMINPASSWORD=password
  20. - ME_CONFIG_MONGODB_ADMINSERVER=mongodb
  21. - ME_CONFIG_MONGODB_PORT=27017
  22. # networks:
  23. # - mongo-network
  24. depends_on:
  25. - mongodb

这是我从mongo-express容器的命令提示符中得到的错误:

  1. 2022-11-15 10:39:21 Welcome to mongo-express
  2. 2022-11-15 10:39:21 ------------------------
  3. 2022-11-15 10:39:21
  4. 2022-11-15 10:39:21
  5. 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.
  6. 2022-11-15 10:39:26 Could not connect to database using connectionString: mongodb://mongo:27017"
  7. 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
  8. 2022-11-15 10:39:26 at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:66:26) {
  9. 2022-11-15 10:39:26 name: 'MongoNetworkError'
  10. 2022-11-15 10:39:26 }]
  11. 2022-11-15 10:39:26 at Pool.<anonymous> (/node_modules/mongodb/lib/core/topologies/server.js:441:11)
  12. 2022-11-15 10:39:26 at Pool.emit (events.js:314:20)
  13. 2022-11-15 10:39:26 at /node_modules/mongodb/lib/core/connection/pool.js:564:14
  14. 2022-11-15 10:39:26 at /node_modules/mongodb/lib/core/connection/pool.js:1000:11
  15. 2022-11-15 10:39:26 at /node_modules/mongodb/lib/core/connection/connect.js:32:7
  16. 2022-11-15 10:39:26 at callback (/node_modules/mongodb/lib/core/connection/connect.js:300:5)
  17. 2022-11-15 10:39:26 at Socket.<anonymous> (/node_modules/mongodb/lib/core/connection/connect.js:330:7)
  18. 2022-11-15 10:39:26 at Object.onceWrapper (events.js:421:26)
  19. 2022-11-15 10:39:26 at Socket.emit (events.js:314:20)
  20. 2022-11-15 10:39:26 at emitErrorNT (internal/streams/destroy.js:92:8)
  21. 2022-11-15 10:39:26 at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
  22. 2022-11-15 10:39:26 at processTicksAndRejections (internal/process/task_queues.js:84:21)
  23. 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)
  24. 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.
wixjitnu

wixjitnu1#

根据文档,服务器环境变量不是ME_CONFIG_MONGODB_ADMINSERVER,而是ME_CONFIG_MONGODB_SERVER:https://hub.docker.com/_/mongo-express
你还需要为mongodb添加一个healthcheck,并让mongo-express依赖它,因为节点在第一次失败的连接后退出,退出代码为0,正如日志中所写的。如果它以非零退出代码退出,只要它没有失败,docker就会尝试重新启动容器。日志中说这将是未来的方式,但现在你需要healthcheck。
(node:7)[DEP 0018]过时警告:不赞成使用未处理的承诺拒绝。将来,未处理的承诺拒绝将使用非零退出代码终止Node.js进程。
因此,此合成文件的工作原理如下:

  1. version: "3"
  2. services:
  3. mongodb:
  4. image: mongo
  5. ports:
  6. - "27017:27017"
  7. environment:
  8. - MONGO_INITDB_ROOT_USERNAME=admin
  9. - MONGO_INITDB_ROOT_PASSWORD=password
  10. healthcheck:
  11. test: echo 'db.runCommand("ping").ok' | mongosh localhost:27017/test --quiet
  12. interval: 10s
  13. timeout: 10s
  14. retries: 5
  15. start_period: 10s
  16. mongo-express:
  17. image: mongo-express
  18. # restart: on-failure
  19. ports:
  20. - "8081:8081"
  21. environment:
  22. - ME_CONFIG_MONGODB_ADMINUSERNAME=admin
  23. - ME_CONFIG_MONGODB_ADMINPASSWORD=password
  24. - ME_CONFIG_MONGODB_SERVER=mongodb
  25. - ME_CONFIG_MONGODB_PORT=27017
  26. depends_on:
  27. mongodb:
  28. condition: service_healthy
展开查看全部

相关问题