使用 - Docker 版本 20.10.7,构建 f0df350
这就是我想要做的:在同一个 docker 网络中有两个容器——MongoDb 和 Mongo-Express 各一个。将 localhost 端口 8090 暴露给 Mongo-Express 的容器端口 8081。不暴露 MongoDd 容器中的端口。
作为 docker pull mongo 和 docker pull mongo-express 拉取的图像
场景1:工作正常
使用如下运行语句,我可以浏览 localhost:8090 并查看 Mongo-Express。
rohit@rohitu2004lts:~$ docker run -d -e --MONGO_INITDB_ROOT_USERNAME=admin -e --MONGO_INITDB_ROOT_PASSWORD=password --name mongo --net testMongoNw mongo
7b1c0ca1518d0d8390efa384fcd76504295e861547e1644cb01f1862624018af
rohit@rohitu2004lts:~$ docker run -d -p 8090:8081 -e --ME_CONFIG_MONGODB_ADMINUSERNAME=admin -e --ME_CONFIG_MONGODB_ADMINPASSWORD=password -e --ME_CONFIG_MONGODB_SERVER=mongo --name myMongoExpress --net testMongoNw mongo-express
f7ab70c43fba1dd23e2e78ae3ddf08cce28fd75d0459938b1c6f057a564decc2
rohit@rohitu2004lts:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f7ab70c43fba mongo-express "tini -- /docker-ent…" 7 seconds ago Up 4 seconds 0.0.0.0:8090->8081/tcp, :::8090->8081/tcp myMongoExpress
7b1c0ca1518d mongo "docker-entrypoint.s…" 17 seconds ago Up 14 seconds 27017/tcp mongo
rohit@rohitu2004lts:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f7ab70c43fba mongo-express "tini -- /docker-ent…" 19 seconds ago Up 16 seconds 0.0.0.0:8090->8081/tcp, :::8090->8081/tcp myMongoExpress
7b1c0ca1518d mongo "docker-entrypoint.s…" 29 seconds ago Up 27 seconds 27017/tcp mongo
rohit@rohitu2004lts:~$ docker logs f7ab
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.
Mongo Express server listening at http://0.0.0.0:8081
Server is open to allow connections from anyone (0.0.0.0)
basicAuth credentials are "admin:pass", it is recommended you change this in your config.js!
rohit@rohitu2004lts:~$
场景2:不工作
但是,如果我只在将 MongoDb 从“mongo”启动为“mymongo”(或任何其他名称)时更改容器名称,Mongo-Express 会启动,但几秒钟后会退出。
rohit@rohitu2004lts:~$ docker run -d -e --MONGO_INITDB_ROOT_USERNAME=admin -e --MONGO_INITDB_ROOT_PASSWORD=password --name mymongo --net testMongoNw mongo
875b4e6b470fca677bd0566a586a40976d5f12b6e71c8d924c8889b07e9e7766
rohit@rohitu2004lts:~$ docker run -d -p 8090:8081 -e --ME_CONFIG_MONGODB_ADMINUSERNAME=admin -e --ME_CONFIG_MONGODB_ADMINPASSWORD=password -e --ME_CONFIG_MONGODB_SERVER=mymongo --name myMongoExpress --net testMongoNw mongo-express
a5e0da7e1b83e018a7010ebdb4e430d4b186161a9ad1bc36c8ce278027e705d0
rohit@rohitu2004lts:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a5e0da7e1b83 mongo-express "tini -- /docker-ent…" 6 seconds ago Up 4 seconds 0.0.0.0:8090->8081/tcp, :::8090->8081/tcp myMongoExpress
875b4e6b470f mongo "docker-entrypoint.s…" 34 seconds ago Up 32 seconds 27017/tcp mymongo
rohit@rohitu2004lts:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a5e0da7e1b83 mongo-express "tini -- /docker-ent…" 16 seconds ago Exited (0) 8 seconds ago myMongoExpress
875b4e6b470f mongo "docker-entrypoint.s…" 44 seconds ago Up 41 seconds 27017/tcp mymongo
rohit@rohitu2004lts:~$ docker logs a5e
Welcome to mongo-express
------------------------
(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.
Could not connect to database using connectionString: mongodb://mongo:27017"
(node:7) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [mongo:27017] on first connect [Error: getaddrinfo EAI_AGAIN mongo
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:289:5)
at Socket.<anonymous> (/node_modules/mongodb/lib/core/connection/connect.js:319: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: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)
(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.
rohit@rohitu2004lts:~$
在 Mongo-Express 运行命令中 ME_CONFIG_MONGODB_SERVER 的参数值中,如果我正确指定了 MongoDB 容器名称,那么它应该可以正常工作......在场景 2 中做错了什么?谢谢你。