mongo express在第一次尝试后没有尝试重新连接



我正试图通过docker-compose文件创建两个服务mongomongoexpress

让我来解释一下这个场景-如果我在同一个容器中使用dockerrun命令一个接一个地分别运行这两个容器,它们就可以正常工作。

我在终端中运行的命令->

对于mongo

docker run -d  
-p27017:27017 
-e MONGO_INITDB_ROOT_USERNAME=admin 
-e MONGO_INITDB_ROOT_PASSWORD=password 
--network 6-mar-learning-docker-with-mongo-image 
--name mongodb 
mongo

对于mongo express

docker run -d  
-p 8081:8081  
-e ME_CONFIG_MONGODB_ADMINUSERNAME=admin 
-e ME_CONFIG_MONGODB_ADMINPASSWORD=password 
--network 6-mar-learning-docker-with-mongo-image 
--name mongo-express 
-e ME_CONFIG_MONGODB_SERVER=mongodb 
mongo-express

但是,如果我尝试使用docker-compose yaml文件mongoexpress服务,则只尝试连接到mongo服务器一次,然后退出并返回代码0。

这是我的yaml文件:

version: "3"
services:
mongodb:
image: mongo
ports:
- 27017:27017
environment:
- MONGO_INITDB_ROOT_USERNAME=admin
- MONGO_INITDB_ROOT_PASSWORD=password
mongo-express:
image: mongo-express
ports:
- 8080:8081
environment:
- ME_CONFIG_MONGODB_ADMINUSERNAME=admin
- ME_CONFIG_MONGODB_ADMINPASSWORD=password
- ME_CONFIG_MONGODB_SERVER=mongodb

我用来执行这个文件的命令

docker-compose -f docker-compose.yaml up 

此命令的日志:

Creating network "docker_default" with the default driver
Creating docker_mongo-express_1 ... done
Creating docker_mongodb_1       ... done
Attaching to docker_mongo-express_1, docker_mongodb_1
mongodb_1        | about to fork child process, waiting until server is ready for connections.
mongodb_1        | forked process: 32
mongodb_1        | 
mongodb_1        | {"t":{"$date":"2022-03-06T13:39:34.852+00:00"},"s":"I",  "c":"CONTROL",  "id":20698,   "ctx":"-","msg":"***** SERVER RESTARTED *****"}
mongodb_1        | {"t":{"$date":"2022-03-06T13:39:34.856+00:00"},"s":"I",  "c":"CONTROL",  "id":23285,   "ctx":"main","msg":"Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'"}
mongodb_1        | {"t":{"$date":"2022-03-06T13:39:34.856+00:00"},"s":"I",  "c":"NETWORK",  "id":4915701, "ctx":"main","msg":"Initialized wire specification","attr":{"spec":{"incomingExternalClient":{"minWireVersion":0,"maxWireVersion":13},"incomingInternalClient":{"minWireVersion":0,"maxWireVersion":13},"outgoing":{"minWireVersion":0,"maxWireVersion":13},"isInternalClient":true}}}
mongodb_1        | {"t":{"$date":"2022-03-06T13:39:34.857+00:00"},"s":"W",  "c":"ASIO",     "id":22601,   "ctx":"main","msg":"No TransportLayer configured during NetworkInterface startup"}
mongodb_1        | {"t":{"$date":"2022-03-06T13:39:34.857+00:00"},"s":"I",  "c":"NETWORK",  "id":4648601, "ctx":"main","msg":"Implicit TCP FastOpen unavailable. If TCP FastOpen is required, set tcpFastOpenServer, tcpFastOpenClient, and tcpFastOpenQueueSize."}
mongodb_1        | {"t":{"$date":"2022-03-06T13:39:34.858+00:00"},"s":"W",  "c":"ASIO",     "id":22601,   "ctx":"main","msg":"No TransportLayer configured during NetworkInterface startup"}
mongodb_1        | {"t":{"$date":"2022-03-06T13:39:34.859+00:00"},"s":"I",  "c":"REPL",     "id":5123008, "ctx":"main","msg":"Successfully registered PrimaryOnlyService","attr":{"service":"TenantMigrationDonorService","ns":"config.tenantMigrationDonors"}}
mongodb_1        | {"t":{"$date":"2022-03-06T13:39:34.859+00:00"},"s":"I",  "c":"REPL",     "id":5123008, "ctx":"main","msg":"Successfully registered PrimaryOnlyService","attr":{"service":"TenantMigrationRecipientService","ns":"config.tenantMigrationRecipients"}}
mongodb_1        | {"t":{"$date":"2022-03-06T13:39:34.859+00:00"},"s":"I",  "c":"CONTROL",  "id":5945603, "ctx":"main","msg":"Multi threading initialized"}
mongodb_1        | {"t":{"$date":"2022-03-06T13:39:34.859+00:00"},"s":"I",  "c":"CONTROL",  "id":4615611, "ctx":"initandlisten","msg":"MongoDB starting","attr":{"pid":32,"port":27017,"dbPath":"/data/db","architecture":"64-bit","host":"5a9ee157c392"}}
mongodb_1        | {"t":{"$date":"2022-03-06T13:39:34.860+00:00"},"s":"I",  "c":"CONTROL",  "id":23403,   "ctx":"initandlisten","msg":"Build Info","attr":{"buildInfo":{"version":"5.0.6","gitVersion":"212a8dbb47f07427dae194a9c75baec1d81d9259","openSSLVersion":"OpenSSL 1.1.1f  31 Mar 2020","modules":[],"allocator":"tcmalloc","environment":{"distmod":"ubuntu2004","distarch":"x86_64","target_arch":"x86_64"}}}}
mongodb_1        | {"t":{"$date":"2022-03-06T13:39:34.860+00:00"},"s":"I",  "c":"CONTROL",  "id":51765,   "ctx":"initandlisten","msg":"Operating System","attr":{"os":{"name":"Ubuntu","version":"20.04"}}}
mongodb_1        | {"t":{"$date":"2022-03-06T13:39:34.860+00:00"},"s":"I",  "c":"CONTROL",  "id":21951,   "ctx":"initandlisten","msg":"Options set by command line","attr":{"options":{"net":{"bindIp":"127.0.0.1","port":27017,"tls":{"mode":"disabled"}},"processManagement":{"fork":true,"pidFilePath":"/tmp/docker-entrypoint-temp-mongod.pid"},"systemLog":{"destination":"file","logAppend":true,"path":"/proc/1/fd/1"}}}}
mongodb_1        | {"t":{"$date":"2022-03-06T13:39:34.861+00:00"},"s":"I",  "c":"STORAGE",  "id":22297,   "ctx":"initandlisten","msg":"Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem","tags":["startupWarnings"]}
mongodb_1        | {"t":{"$date":"2022-03-06T13:39:34.861+00:00"},"s":"I",  "c":"STORAGE",  "id":22315,   "ctx":"initandlisten","msg":"Opening WiredTiger","attr":{"config":"create,cache_size=480M,session_max=33000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),builtin_extension_config=(zstd=(compression_level=6)),file_manager=(close_idle_time=600,close_scan_interval=10,close_handle_minimum=250),statistics_log=(wait=0),verbose=[recovery_progress,checkpoint_progress,compact_progress],"}}
mongodb_1        | {"t":{"$date":"2022-03-06T13:39:35.323+00:00"},"s":"I",  "c":"STORAGE",  "id":22430,   "ctx":"initandlisten","msg":"WiredTiger message","attr":{"message":"[1646573975:323191][32:0x7ff1314c6c80], txn-recover: [WT_VERB_RECOVERY_ALL] Set global recovery timestamp: (0, 0)"}}
mongodb_1        | {"t":{"$date":"2022-03-06T13:39:35.323+00:00"},"s":"I",  "c":"STORAGE",  "id":22430,   "ctx":"initandlisten","msg":"WiredTiger message","attr":{"message":"[1646573975:323262][32:0x7ff1314c6c80], txn-recover: [WT_VERB_RECOVERY_ALL] Set global oldest timestamp: (0, 0)"}}
mongodb_1        | {"t":{"$date":"2022-03-06T13:39:35.329+00:00"},"s":"I",  "c":"STORAGE",  "id":4795906, "ctx":"initandlisten","msg":"WiredTiger opened","attr":{"durationMillis":468}}
mongodb_1        | {"t":{"$date":"2022-03-06T13:39:35.329+00:00"},"s":"I",  "c":"RECOVERY", "id":23987,   "ctx":"initandlisten","msg":"WiredTiger recoveryTimestamp","attr":{"recoveryTimestamp":{"$timestamp":{"t":0,"i":0}}}}
mongodb_1        | {"t":{"$date":"2022-03-06T13:39:35.340+00:00"},"s":"I",  "c":"STORAGE",  "id":4366408, "ctx":"initandlisten","msg":"No table logging settings modifications are required for existing WiredTiger tables","attr":{"loggingEnabled":true}}
mongodb_1        | {"t":{"$date":"2022-03-06T13:39:35.340+00:00"},"s":"I",  "c":"STORAGE",  "id":22262,   "ctx":"initandlisten","msg":"Timestamp monitor starting"}
mongodb_1        | {"t":{"$date":"2022-03-06T13:39:35.345+00:00"},"s":"W",  "c":"CONTROL",  "id":22120,   "ctx":"initandlisten","msg":"Access control is not enabled for the database. Read and write access to data and configuration is unrestricted","tags":["startupWarnings"]}
mongodb_1        | {"t":{"$date":"2022-03-06T13:39:35.346+00:00"},"s":"I",  "c":"STORAGE",  "id":20320,   "ctx":"initandlisten","msg":"createCollection","attr":{"namespace":"admin.system.version","uuidDisposition":"provided","uuid":{"uuid":{"$uuid":"6cdf8a79-38eb-4cf7-a722-437bf35ff55f"}},"options":{"uuid":{"$uuid":"6cdf8a79-38eb-4cf7-a722-437bf35ff55f"}}}}
mongodb_1        | {"t":{"$date":"2022-03-06T13:39:35.354+00:00"},"s":"I",  "c":"INDEX",    "id":20345,   "ctx":"initandlisten","msg":"Index build: done building","attr":{"buildUUID":null,"namespace":"admin.system.version","index":"_id_","commitTimestamp":null}}
mongodb_1        | {"t":{"$date":"2022-03-06T13:39:35.355+00:00"},"s":"I",  "c":"REPL",     "id":20459,   "ctx":"initandlisten","msg":"Setting featureCompatibilityVersion","attr":{"newVersion":"5.0"}}
mongodb_1        | {"t":{"$date":"2022-03-06T13:39:35.355+00:00"},"s":"I",  "c":"NETWORK",  "id":4915702, "ctx":"initandlisten","msg":"Updated wire specification","attr":{"oldSpec":{"incomingExternalClient":{"minWireVersion":0,"maxWireVersion":13},"incomingInternalClient":{"minWireVersion":0,"maxWireVersion":13},"outgoing":{"minWireVersion":0,"maxWireVersion":13},"isInternalClient":true},"newSpec":{"incomingExternalClient":{"minWireVersion":0,"maxWireVersion":13},"incomingInternalClient":{"minWireVersion":13,"maxWireVersion":13},"outgoing":{"minWireVersion":13,"maxWireVersion":13},"isInternalClient":true}}}
mongodb_1        | {"t":{"$date":"2022-03-06T13:39:35.355+00:00"},"s":"I",  "c":"NETWORK",  "id":4915702, "ctx":"initandlisten","msg":"Updated wire specification","attr":{"oldSpec":{"incomingExternalClient":{"minWireVersion":0,"maxWireVersion":13},"incomingInternalClient":{"minWireVersion":13,"maxWireVersion":13},"outgoing":{"minWireVersion":13,"maxWireVersion":13},"isInternalClient":true},"newSpec":{"incomingExternalClient":{"minWireVersion":0,"maxWireVersion":13},"incomingInternalClient":{"minWireVersion":13,"maxWireVersion":13},"outgoing":{"minWireVersion":13,"maxWireVersion":13},"isInternalClient":true}}}
mongodb_1        | {"t":{"$date":"2022-03-06T13:39:35.355+00:00"},"s":"I",  "c":"STORAGE",  "id":5071100, "ctx":"initandlisten","msg":"Clearing temp directory"}
mongodb_1        | {"t":{"$date":"2022-03-06T13:39:35.355+00:00"},"s":"I",  "c":"CONTROL",  "id":20536,   "ctx":"initandlisten","msg":"Flow Control is enabled on this deployment"}
mongodb_1        | {"t":{"$date":"2022-03-06T13:39:35.356+00:00"},"s":"I",  "c":"FTDC",     "id":20625,   "ctx":"initandlisten","msg":"Initializing full-time diagnostic data capture","attr":{"dataDirectory":"/data/db/diagnostic.data"}}
mongodb_1        | {"t":{"$date":"2022-03-06T13:39:35.356+00:00"},"s":"I",  "c":"STORAGE",  "id":20320,   "ctx":"initandlisten","msg":"createCollection","attr":{"namespace":"local.startup_log","uuidDisposition":"generated","uuid":{"uuid":{"$uuid":"c7bdfd70-ca9a-4a5a-8c7d-b498500fff1f"}},"options":{"capped":true,"size":10485760}}}
mongodb_1        | {"t":{"$date":"2022-03-06T13:39:35.367+00:00"},"s":"I",  "c":"INDEX",    "id":20345,   "ctx":"initandlisten","msg":"Index build: done building","attr":{"buildUUID":null,"namespace":"local.startup_log","index":"_id_","commitTimestamp":null}}
mongodb_1        | {"t":{"$date":"2022-03-06T13:39:35.367+00:00"},"s":"I",  "c":"REPL",     "id":6015317, "ctx":"initandlisten","msg":"Setting new configuration state","attr":{"newState":"ConfigReplicationDisabled","oldState":"ConfigPreStart"}}
mongodb_1        | {"t":{"$date":"2022-03-06T13:39:35.369+00:00"},"s":"I",  "c":"CONTROL",  "id":20712,   "ctx":"LogicalSessionCacheReap","msg":"Sessions collection is not set up; waiting until next sessions reap interval","attr":{"error":"NamespaceNotFound: config.system.sessions does not exist"}}
mongodb_1        | {"t":{"$date":"2022-03-06T13:39:35.369+00:00"},"s":"I",  "c":"NETWORK",  "id":23015,   "ctx":"listener","msg":"Listening on","attr":{"address":"/tmp/mongodb-27017.sock"}}
mongodb_1        | {"t":{"$date":"2022-03-06T13:39:35.369+00:00"},"s":"I",  "c":"STORAGE",  "id":20320,   "ctx":"LogicalSessionCacheRefresh","msg":"createCollection","attr":{"namespace":"config.system.sessions","uuidDisposition":"generated","uuid":{"uuid":{"$uuid":"a361b644-06c4-4102-8abd-a78af1e4fb4d"}},"options":{}}}
mongodb_1        | {"t":{"$date":"2022-03-06T13:39:35.369+00:00"},"s":"I",  "c":"NETWORK",  "id":23015,   "ctx":"listener","msg":"Listening on","attr":{"address":"127.0.0.1"}}
mongodb_1        | {"t":{"$date":"2022-03-06T13:39:35.369+00:00"},"s":"I",  "c":"NETWORK",  "id":23016,   "ctx":"listener","msg":"Waiting for connections","attr":{"port":27017,"ssl":"off"}}
mongodb_1        | child process started successfully, parent exiting
mongo-express_1  | Welcome to mongo-express
mongo-express_1  | ------------------------
mongo-express_1  | 
mongo-express_1  | 
mongodb_1        | {"t":{"$date":"2022-03-06T13:39:35.388+00:00"},"s":"I",  "c":"INDEX",    "id":20345,   "ctx":"LogicalSessionCacheRefresh","msg":"Index build: done building","attr":{"buildUUID":null,"namespace":"config.system.sessions","index":"_id_","commitTimestamp":null}}
mongodb_1        | {"t":{"$date":"2022-03-06T13:39:35.388+00:00"},"s":"I",  "c":"INDEX",    "id":20345,   "ctx":"LogicalSessionCacheRefresh","msg":"Index build: done building","attr":{"buildUUID":null,"namespace":"config.system.sessions","index":"lsidTTLIndex","commitTimestamp":null}}
mongo-express_1  | (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.
mongo-express_1  | Could not connect to database using connectionString: mongodb://admin:password@mongodb:27017/"
mongo-express_1  | (node:7) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [mongodb:27017] on first connect [Error: connect ECONNREFUSED 172.27.0.3:27017
mongo-express_1  |     at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1144:16) {
mongo-express_1  |   name: 'MongoNetworkError'
mongo-express_1  | }]
mongo-express_1  |     at Pool.<anonymous> (/node_modules/mongodb/lib/core/topologies/server.js:441:11)
mongo-express_1  |     at Pool.emit (events.js:314:20)
mongo-express_1  |     at /node_modules/mongodb/lib/core/connection/pool.js:564:14
mongo-express_1  |     at /node_modules/mongodb/lib/core/connection/pool.js:1000:11
mongo-express_1  |     at /node_modules/mongodb/lib/core/connection/connect.js:32:7
mongo-express_1  |     at callback (/node_modules/mongodb/lib/core/connection/connect.js:300:5)
mongo-express_1  |     at Socket.<anonymous> (/node_modules/mongodb/lib/core/connection/connect.js:330:7)
mongo-express_1  |     at Object.onceWrapper (events.js:421:26)
mongo-express_1  |     at Socket.emit (events.js:314:20)
mongo-express_1  |     at emitErrorNT (internal/streams/destroy.js:92:8)
mongo-express_1  |     at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
mongo-express_1  |     at processTicksAndRejections (internal/process/task_queues.js:84:21)
mongo-express_1  | (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)
mongodb_1        | {"t":{"$date":"2022-03-06T13:39:35.432+00:00"},"s":"I",  "c":"NETWORK",  "id":22943,   "ctx":"listener","msg":"Connection accepted","attr":{"remote":"127.0.0.1:60038","uuid":"70bac03c-a30e-47b6-b6a5-bc6a551ab95e","connectionId":1,"connectionCount":1}}
mongo-express_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.
mongodb_1        | {"t":{"$date":"2022-03-06T13:39:35.433+00:00"},"s":"I",  "c":"NETWORK",  "id":51800,   "ctx":"conn1","msg":"client metadata","attr":{"remote":"127.0.0.1:60038","client":"conn1","doc":{"application":{"name":"MongoDB Shell"},"driver":{"name":"MongoDB Internal Client","version":"5.0.6"},"os":{"type":"Linux","name":"Ubuntu","architecture":"x86_64","version":"20.04"}}}}
mongodb_1        | {"t":{"$date":"2022-03-06T13:39:35.443+00:00"},"s":"I",  "c":"NETWORK",  "id":22944,   "ctx":"conn1","msg":"Connection ended","attr":{"remote":"127.0.0.1:60038","uuid":"70bac03c-a30e-47b6-b6a5-bc6a551ab95e","connectionId":1,"connectionCount":0}}
docker_mongo-express_1 exited with code 0
mongodb_1        | {"t":{"$date":"2022-03-06T13:39:35.598+00:00"},"s":"I",  "c":"NETWORK",  "id":22943,   "ctx":"listener","msg":"Connection accepted","attr":{"remote":"127.0.0.1:60040","uuid":"2f5cd59e-5691-40c5-a31a-fd4fbb0e6a63","connectionId":2,"connectionCount":1}}

此实例的docker ps输出

CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS                                           NAMES
5a9ee157c392   mongo     "docker-entrypoint.s…"   3 minutes ago   Up 3 minutes   0.0.0.0:27017->27017/tcp, :::27017->27017/tcp   docker_mongodb_1

所以基本上mongo被创建了,而mongo express在退出时并没有。

现在,如果我在运行docker compose命令后按CTRL+C(退出Mac中的命令(,然后再次运行它。现在它可以工作了,因为mongo服务已经启动,mongoexpress可以在第一次尝试中建立连接。

但我认为,它应该尝试重新连接,我也看到并阅读了一些教程,这些教程验证了服务不应该退出,它应该试图重新连接。也许我在docker compose.yaml文件中遗漏了一些内容,请原谅我,因为我正在学习docker。

只是一个小提示-我通过使用/设置";重新启动";选项,但我在人们的教程和代码中看到,它试图重新连接自己,而不需要反复重新启动。

我使用的教程参考-https://youtu.be/3c-iBn73dDE?t=5705

mongodbmongo-express服务不应同时启动,因为mongo-express依赖于mongodb容器。在express服务器对compose文件进行以下更改之前,您可以确保数据库运行。

我添加了一个简单的healthcheck和一个depends_on,以确保在express服务器启动之前,mongo实例是健康就绪

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' | mongo localhost:27017/test --quiet
retries: 5
interval: 15s
start_period: 30s
mongo-express:
image: mongo-express
ports:
- 8080:8081
environment:
- ME_CONFIG_MONGODB_ADMINUSERNAME=admin
- ME_CONFIG_MONGODB_ADMINPASSWORD=password
- ME_CONFIG_MONGODB_SERVER=mongodb
depends_on:
mongodb:
condition: service_healthy

您可以简单地使用

mongo-express:
image: mongo-express
ports:
- 8080:8081
restart: unless-stopped
environment:
- ME_CONFIG_MONGODB_ADMINUSERNAME=admin
- ME_CONFIG_MONGODB_ADMINPASSWORD=password
- ME_CONFIG_MONGODB_SERVER=mongodb

或任何重新启动选项。这是码头文件。顺便说一句,如果你想使用启动订单,我建议你阅读Digital Ocean集装箱化教程,尤其是第4步

Ps:我觉得version: "3"还可以,但是在使用之前检查一下版本功能。快乐编码:(

最新更新