我的docker命令有什么问题?无法将mongo express连接到mongo db

ryhaxcpt  于 2023-03-22  发布在  Docker
关注(0)|答案(1)|浏览(180)

我已经让MongoDB容器正常运行了一段时间。当我尝试启动Mongo Express时,我得到一个错误,告诉我它无法连接到数据库。我使用的命令有什么问题?

docker run -d \
  -p 27017:27017 \
  -e MONGO_INITDB_ROOT_USERNAME=admin \
  -e MONGO_INITDB_ROOT_PASSWORD=password \
  --name mongodb \
  --network mongo-network \
  mongo

docker run -d \
  -p 8081:8081 \
  -e ME_CONFIG_MONGODB_SERVER=ee163c15dcf2 \
  -e ME_CONFIG_MONGODB_ADMINUSERNAME=admin \
  -e ME_CONFIG_MONGODB_ADMINPASSWORD=password \
  --network mongo_network \
  --name mongo-express \
  mongo-express

我通过运行以下命令获得MongoDB服务器的主机名

docker exec <container_name_or_id> hostname

下面是不断退出的Mongo Express容器的日志。

root@ubuntu-s-2vcpu-2gb-nyc1-01:~# docker logs 7519be7544de
Welcome to mongo-express
------------------------

(node:8) [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.
Could not connect to database using connectionString: mongodb://admin:password@ee163c15dcf2:27017/"
(node:8) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [ee163c15dcf2:27017] on first connect [Error: getaddrinfo EAI_AGAIN ee163c15dcf2
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:66:26) {
  name: 'MongoNetworkError'
}]
    at Pool.<anonymous> (/node_modules/mongodb/lib/core/topologies/server.js:441:11)
    at Pool.emit (events.js:314:20)
    at /node_modules/mongodb/lib/core/connection/pool.js:564:14
    at /node_modules/mongodb/lib/core/connection/pool.js:1000:11
    at /node_modules/mongodb/lib/core/connection/connect.js:32:7
    at callback (/node_modules/mongodb/lib/core/connection/connect.js:300:5)
    at Socket.<anonymous> (/node_modules/mongodb/lib/core/connection/connect.js:330:7)
    at Object.onceWrapper (events.js:421:26)
    at Socket.emit (events.js:314:20)
    at emitErrorNT (internal/streams/destroy.js:92:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
    at processTicksAndRejections (internal/process/task_queues.js:84:21)
(node:8) 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)
(node:8) [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.
root@ubuntu-s-2vcpu-2gb-nyc1-01:~# docker exec ee163c15dcf2 hostname
ee163c15dcf2
root@ubuntu-s-2vcpu-2gb-nyc1-01:~#

我尝试启动Mongo Express容器,但无法连接。

owfi6suc

owfi6suc1#

有一个语法错误,你已经张贴在上面

1.

网络是mongo-network,而不是mongo_network

2.

ME_CONFIG_MONGODB_SERVER的值应为容器mongodb的CONTAINER ID

在我的ubuntu上,它工作得很好。

  • 在浏览器上使用此URL:SERVER_IP:8081,你会看到效果。

正如@David Maze所说,为了避免在重建操作中修改mongodb的容器ID,建议这里使用容器名mongodb,

docker run -d \
  -p 8081:8081 \
  -e ME_CONFIG_MONGODB_SERVER=mongodb \
  -e ME_CONFIG_MONGODB_ADMINUSERNAME=admin \
  -e ME_CONFIG_MONGODB_ADMINPASSWORD=password \
  --network mongo_network \
  --name mongo-express \
  mongo-express

相关问题