Mongo-Express 由 Docker-Compose 启动导致错误 [MongoError]:首次连接时无法连接到服务器 [mongo:27017]

Mongo-Express started by Docker-Compose causing Error [MongoError]: failed to connect to server [mongo:27017] on first connect

场景:我想从同一个 docker-compose 开始 Mongo 和 Mongo-Express。

我只从 docker-compose 开始 mongodb 没问题。当我尝试 link Mongo-Express 到 Mongo 数据库服务时出现问题。

我不确定我是否缺少 docker-compose 或 mongo-express 设置中的某些配置。我不认为问题是 Mongo 设置的一部分。顺便说一句,我在下面粘贴了整个 docker-compose 及其输出。

从下面的日志中我可以看到没有引发 Mongo 的问题。 mongo-express 尝试连接到 Mongo 服务时出现一些问题,可能是因为我没有正确设置 link。最重要的是,我的 Spring Webflux 已成功连接到 Mongo 具有以下属性的数据库。该问题仅限于 mongo-express 达到 mongodb.

application.yml 允许 Spring 数据(spring-boot-starter-data-mongodb-reactive)成功连接到这样的 MongoDb

spring:
  data:
    mongodb:
      host: 192.168.99.100
      port:  27017
      database: demodb
      authentication-database: admin
      username: root
      password: rootpassword

docker-compose.yml

version: '3.7'

services:
  mongo-express:
    image: mongo-express
    ports:
      - 8081:8081
    environment:
      ME_CONFIG_BASICAUTH_USERNAME: admin
      ME_CONFIG_BASICAUTH_PASSWORD: q
      ME_CONFIG_MONGODB_PORT: 27017
      ME_CONFIG_MONGODB_ADMINUSERNAME: root
      ME_CONFIG_MONGODB_ADMINPASSWORD: rootpassword
    links:
      - mongo-db
    networks:
      - mongo-compose-network

  mongo-db:
    image: mongo:latest
    environment:
      MONGO_INITDB_DATABASE: demodb  
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: rootpassword
    ports:
      - "27017:27017"
    volumes:
      - mongodb_data_container:/data/db
    networks:
      - mongo-compose-network

networks: 
    mongo-compose-network:
      driver: bridge

volumes:
  mongodb_data_container:       

输出

PS C:\Dockers\mongodb\express> docker-compose up
Creating express_mongo_1 ... done                                                                                                      Creating express_mongo-express_1 ... done                                                                                              Attaching to express_mongo_1, express_mongo-express_1
mongo_1          | 2020-03-18T21:49:37.366+0000 I  CONTROL  [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
mongo_1          | 2020-03-18T21:49:37.370+0000 I  CONTROL  [initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/data/db 64-bit host=8c1f22435085
mongo_1          | 2020-03-18T21:49:37.370+0000 I  CONTROL  [initandlisten] db version v4.2.3
mongo_1          | 2020-03-18T21:49:37.370+0000 I  CONTROL  [initandlisten] git version: 6874650b362138df74be53d366bbefc321ea32d4
mongo_1          | 2020-03-18T21:49:37.370+0000 I  CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.1.1  11 Sep 2018
mongo_1          | 2020-03-18T21:49:37.370+0000 I  CONTROL  [initandlisten] allocator: tcmalloc
mongo_1          | 2020-03-18T21:49:37.370+0000 I  CONTROL  [initandlisten] modules: none
mongo-express_1  | Waiting for mongo:27017...
mongo_1          | 2020-03-18T21:49:37.370+0000 I  CONTROL  [initandlisten] build environment:
mongo_1          | 2020-03-18T21:49:37.371+0000 I  CONTROL  [initandlisten]     distmod: ubuntu1804
mongo_1          | 2020-03-18T21:49:37.371+0000 I  CONTROL  [initandlisten]     distarch: x86_64
mongo_1          | 2020-03-18T21:49:37.371+0000 I  CONTROL  [initandlisten]     target_arch: x86_64
mongo_1          | 2020-03-18T21:49:37.371+0000 I  CONTROL  [initandlisten] options: { net: { bindIp: "*" }, security: { authorization: "enabled" } }
mongo-express_1  | /docker-entrypoint.sh: connect: Connection refused
mongo_1          | 2020-03-18T21:49:37.373+0000 I  STORAGE  [initandlisten] Detected data files in /data/db created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
mongo_1          | 2020-03-18T21:49:37.373+0000 I  STORAGE  [initandlisten]
mongo_1          | 2020-03-18T21:49:37.373+0000 I  STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
mongo_1          | 2020-03-18T21:49:37.373+0000 I  STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
mongo_1          | 2020-03-18T21:49:37.374+0000 I  STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=486M,cache_overflow=(file_max=0M),session_max=33000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000,close_scan_interval=10,close_handle_minimum=250),statistics_log=(wait=0),verbose=[recovery_progress,checkpoint_progress],
mongo_1          | 2020-03-18T21:49:38.229+0000 I  STORAGE  [initandlisten] WiredTiger message [1584568178:229680][1:0x7f9df38f3b00], txn-recover: Recovering log 5 through 6
mongo_1          | 2020-03-18T21:49:38.261+0000 I  STORAGE  [initandlisten] WiredTiger message [1584568178:261407][1:0x7f9df38f3b00], txn-recover: Recovering log 6 through 6
mongo_1          | 2020-03-18T21:49:38.313+0000 I  STORAGE  [initandlisten] WiredTiger message [1584568178:313791][1:0x7f9df38f3b00], txn-recover: Main recovery loop: starting at 5/6144 to 6/256
mongo_1          | 2020-03-18T21:49:38.378+0000 I  STORAGE  [initandlisten] WiredTiger message [1584568178:378014][1:0x7f9df38f3b00], txn-recover: Recovering log 5 through 6
mongo_1          | 2020-03-18T21:49:38.448+0000 I  STORAGE  [initandlisten] WiredTiger message [1584568178:448927][1:0x7f9df38f3b00], txn-recover: Recovering log 6 through 6
mongo_1          | 2020-03-18T21:49:38.480+0000 I  STORAGE  [initandlisten] WiredTiger message [1584568178:480668][1:0x7f9df38f3b00], txn-recover: Set global recovery timestamp: (0, 0)
mongo_1          | 2020-03-18T21:49:38.505+0000 I  RECOVERY [initandlisten] WiredTiger recoveryTimestamp. Ts: Timestamp(0, 0)
mongo_1          | 2020-03-18T21:49:38.519+0000 I  STORAGE  [initandlisten] Timestamp monitor starting
mongo_1          | 2020-03-18T21:49:38.530+0000 I  SHARDING [initandlisten] Marking collection local.system.replset as collection version: <unsharded>
mongo_1          | 2020-03-18T21:49:38.535+0000 I  STORAGE  [initandlisten] Flow Control is enabled on this deployment.
mongo_1          | 2020-03-18T21:49:38.535+0000 I  SHARDING [initandlisten] Marking collection admin.system.roles as collection version: <unsharded>
mongo_1          | 2020-03-18T21:49:38.536+0000 I  SHARDING [initandlisten] Marking collection admin.system.version as collection version: <unsharded>
mongo_1          | 2020-03-18T21:49:38.541+0000 I  SHARDING [initandlisten] Marking collection local.startup_log as collection version: <unsharded>
mongo_1          | 2020-03-18T21:49:38.542+0000 I  FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
mongo_1          | 2020-03-18T21:49:38.544+0000 I  SHARDING [LogicalSessionCacheRefresh] Marking collection config.system.sessions as collection version: <unsharded>
mongo_1          | 2020-03-18T21:49:38.545+0000 I  SHARDING [LogicalSessionCacheReap] Marking collection config.transactions as collection version: <unsharded>
mongo_1          | 2020-03-18T21:49:38.546+0000 I  NETWORK  [listener] Listening on /tmp/mongodb-27017.sock
mongo_1          | 2020-03-18T21:49:38.546+0000 I  NETWORK  [listener] Listening on 0.0.0.0
mongo-express_1  | /docker-entrypoint.sh: line 14: /dev/tcp/mongo/27017: Connection refused
mongo_1          | 2020-03-18T21:49:38.546+0000 I  NETWORK  [listener] waiting for connections on port 27017
mongo_1          | 2020-03-18T21:49:38.863+0000 I  NETWORK  [listener] connection accepted from 172.22.0.3:53802 #1 (1 connection now open)
mongo_1          | 2020-03-18T21:49:38.864+0000 I  NETWORK  [conn1] end connection 172.22.0.3:53802 (0 connections now open)
mongo-express_1  | Wed Mar 18 21:49:38 UTC 2020 retrying to connect to mongo:27017 (2/5)
mongo-express_1  | Welcome to mongo-express
mongo-express_1  | ------------------------
mongo-express_1  |
mongo-express_1  |
mongo-express_1  | Mongo Express server listening at http://0.0.0.0:8081
mongo_1          | 2020-03-18T21:49:39.000+0000 I  SHARDING [ftdc] Marking collection local.oplog.rs as collection version: <unsharded>
mongo_1          | 2020-03-18T21:49:39.338+0000 I  NETWORK  [listener] connection accepted from 172.22.0.3:53804 #2 (1 connection now open)
mongo-express_1  | Server is open to allow connections from anyone (0.0.0.0)
mongo_1          | 2020-03-18T21:49:39.344+0000 I  NETWORK  [conn2] received client metadata from 172.22.0.3:53804 conn2: { driver: { name: "nodejs", version: "2.2.24" }, os: { type: "Linux", name: "linux", architecture: "x64", version: "4.14.154-boot2docker" }, platform: "Node.js v12.16.1, LE, mongodb-core: 2.1.8" }
mongo_1          | 2020-03-18T21:49:39.350+0000 I  SHARDING [conn2] Marking collection admin.system.users as collection version: <unsharded>
mongo-express_1  | Database connected
mongo_1          | 2020-03-18T21:49:39.809+0000 I  ACCESS   [conn2] Successfully authenticated as principal root on admin from client 172.22.0.3:53804
mongo_1          | 2020-03-18T21:49:39.816+0000 I  NETWORK  [listener] connection accepted from 172.22.0.3:53806 #3 (2 connections now open)
mongo_1          | 2020-03-18T21:49:39.821+0000 I  NETWORK  [listener] connection accepted from 192.168.99.1:51536 #4 (3 connections now open)
mongo_1          | 2020-03-18T21:49:39.822+0000 I  NETWORK  [conn4] received client metadata from 192.168.99.1:51536 conn4: { driver: { name: "mongo-java-driver|legacy", version: "3.11.2" }, os: { type: "Windows", name: "Windows 10", architecture: "amd64", version: "10.0" }, platform: "Java/Oracle Corporation/11.0.6+8-LTS" }
mongo_1          | 2020-03-18T21:49:39.927+0000 I  ACCESS   [conn3] Successfully authenticated as principal root on admin from client 172.22.0.3:53806
mongo_1          | 2020-03-18T21:49:40.759+0000 I  NETWORK  [listener] connection accepted from 192.168.99.1:51537 #5 (4 connections now open)
mongo_1          | 2020-03-18T21:49:40.761+0000 I  NETWORK  [conn5] received client metadata from 192.168.99.1:51537 conn5: { driver: { name: "mongo-java-driver|async|mongo-java-driver-reactivestreams", version: "3.11.2|1.12.0" }, os: { type: "Windows", name: "Windows 10", architecture: "amd64", version: "10.0" }, platform: "Java/Oracle Corporation/11.0.6+8-LTS" }
mongo_1          | 2020-03-18T21:50:39.101+0000 I  NETWORK  [conn4] end connection 192.168.99.1:51536 (3 connections now open)
mongo_1          | 2020-03-18T21:50:39.102+0000 I  NETWORK  [conn5] end connection 192.168.99.1:51537 (2 connections now open)
mongo_1          | 2020-03-18T21:50:57.344+0000 I  NETWORK  [listener] connection accepted from 192.168.99.1:51576 #6 (3 connections now open)
mongo_1          | 2020-03-18T21:50:57.355+0000 I  NETWORK  [conn6] received client metadata from 192.168.99.1:51576 conn6: { driver: { name: "mongo-java-driver|legacy", version: "3.11.2" }, os: { type: "Windows", name: "Windows 10", architecture: "amd64", version: "10.0" }, platform: "Java/Oracle Corporation/11.0.6+8-LTS" }
mongo_1          | 2020-03-18T21:50:58.325+0000 I  NETWORK  [listener] connection accepted from 192.168.99.1:51593 #7 (4 connections now open)
mongo_1          | 2020-03-18T21:50:58.355+0000 I  NETWORK  [conn7] received client metadata from 192.168.99.1:51593 conn7: { driver: { name: "mongo-java-driver|async|mongo-java-driver-reactivestreams", version: "3.11.2|1.12.0" }, os: { type: "Windows", name: "Windows 10", architecture: "amd64", version: "10.0" }, platform: "Java/Oracle Corporation/11.0.6+8-LTS" }
mongo_1          | 2020-03-18T22:04:09.075+0000 I  NETWORK  [conn6] end connection 192.168.99.1:51576 (3 connections now open)
Gracefully stopping... (press Ctrl+C again to force)
Stopping express_mongo-express_1 ...
Stopping express_mongo_1         ...
Killing express_mongo_1          ... done                                                                                              PS C:\Dockers\mongodb\express> docker-compose down --remove-orphans
Removing express_mongo-express_1 ... done                                                                                              Removing express_mongo_1         ... done                                                                                              Removing network express_mongox-compose-network
PS C:\Dockers\mongodb\express> docker-compose up
Creating network "express_mongo-compose-network" with driver "bridge"
Creating express_mongo-db_1 ... done                                                                                                   Creating express_mongo-express_1 ... done                                                                                              Attaching to express_mongo-db_1, express_mongo-express_1
mongo-db_1       | 2020-03-18T22:06:12.909+0000 I  CONTROL  [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
mongo-db_1       | 2020-03-18T22:06:12.913+0000 I  CONTROL  [initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/data/db 64-bit host=d59ff752b4e0
mongo-db_1       | 2020-03-18T22:06:12.913+0000 I  CONTROL  [initandlisten] db version v4.2.3
mongo-db_1       | 2020-03-18T22:06:12.913+0000 I  CONTROL  [initandlisten] git version: 6874650b362138df74be53d366bbefc321ea32d4
mongo-db_1       | 2020-03-18T22:06:12.913+0000 I  CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.1.1  11 Sep 2018
mongo-db_1       | 2020-03-18T22:06:12.913+0000 I  CONTROL  [initandlisten] allocator: tcmalloc
mongo-db_1       | 2020-03-18T22:06:12.914+0000 I  CONTROL  [initandlisten] modules: none
mongo-db_1       | 2020-03-18T22:06:12.914+0000 I  CONTROL  [initandlisten] build environment:
mongo-db_1       | 2020-03-18T22:06:12.914+0000 I  CONTROL  [initandlisten]     distmod: ubuntu1804
mongo-db_1       | 2020-03-18T22:06:12.917+0000 I  CONTROL  [initandlisten]     distarch: x86_64
mongo-db_1       | 2020-03-18T22:06:12.917+0000 I  CONTROL  [initandlisten]     target_arch: x86_64
mongo-db_1       | 2020-03-18T22:06:12.917+0000 I  CONTROL  [initandlisten] options: { net: { bindIp: "*" }, security: { authorization: "enabled" } }
mongo-db_1       | 2020-03-18T22:06:12.917+0000 W  STORAGE  [initandlisten] Detected unclean shutdown - /data/db/mongod.lock is not empty.
mongo-db_1       | 2020-03-18T22:06:12.918+0000 I  STORAGE  [initandlisten] Detected data files in /data/db created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
mongo-db_1       | 2020-03-18T22:06:12.918+0000 W  STORAGE  [initandlisten] Recovering data from the last clean checkpoint.
mongo-db_1       | 2020-03-18T22:06:12.918+0000 I  STORAGE  [initandlisten]
mongo-db_1       | 2020-03-18T22:06:12.918+0000 I  STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
mongo-db_1       | 2020-03-18T22:06:12.918+0000 I  STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
mongo-express_1  | Waiting for mongo:27017...
mongo-express_1  | /docker-entrypoint.sh: line 14: mongo: Name does not resolve
mongo-db_1       | 2020-03-18T22:06:12.919+0000 I  STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=486M,cache_overflow=(file_max=0M),session_max=33000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000,close_scan_interval=10,close_handle_minimum=250),statistics_log=(wait=0),verbose=[recovery_progress,checkpoint_progress],
mongo-db_1       | 2020-03-18T22:06:13.820+0000 I  STORAGE  [initandlisten] WiredTiger message [1584569173:820960][1:0x7fc8832f2b00], txn-recover: Recovering log 6 through 7
mongo-db_1       | 2020-03-18T22:06:13.852+0000 I  STORAGE  [initandlisten] WiredTiger message [1584569173:852058][1:0x7fc8832f2b00], txn-recover: Recovering log 7 through 7
mongo-db_1       | 2020-03-18T22:06:13.905+0000 I  STORAGE  [initandlisten] WiredTiger message [1584569173:905743][1:0x7fc8832f2b00], txn-recover: Main recovery loop: starting at 6/5760 to 7/256
mongo-db_1       | 2020-03-18T22:06:13.987+0000 I  STORAGE  [initandlisten] WiredTiger message [1584569173:987253][1:0x7fc8832f2b00], txn-recover: Recovering log 6 through 7
mongo-db_1       | 2020-03-18T22:06:14.031+0000 I  STORAGE  [initandlisten] WiredTiger message [1584569174:31988][1:0x7fc8832f2b00], txn-recover: Recovering log 7 through 7
mongo-db_1       | 2020-03-18T22:06:14.064+0000 I  STORAGE  [initandlisten] WiredTiger message [1584569174:64661][1:0x7fc8832f2b00], txn-recover: Set global recovery timestamp: (0, 0)
mongo-db_1       | 2020-03-18T22:06:14.106+0000 I  RECOVERY [initandlisten] WiredTiger recoveryTimestamp. Ts: Timestamp(0, 0)
mongo-db_1       | 2020-03-18T22:06:14.115+0000 I  STORAGE  [initandlisten] Timestamp monitor starting
mongo-db_1       | 2020-03-18T22:06:14.122+0000 I  SHARDING [initandlisten] Marking collection local.system.replset as collection version: <unsharded>
mongo-db_1       | 2020-03-18T22:06:14.126+0000 I  STORAGE  [initandlisten] Flow Control is enabled on this deployment.
mongo-db_1       | 2020-03-18T22:06:14.126+0000 I  SHARDING [initandlisten] Marking collection admin.system.roles as collection version: <unsharded>
mongo-db_1       | 2020-03-18T22:06:14.126+0000 I  SHARDING [initandlisten] Marking collection admin.system.version as collection version: <unsharded>
mongo-db_1       | 2020-03-18T22:06:14.130+0000 I  SHARDING [initandlisten] Marking collection local.startup_log as collection version: <unsharded>
mongo-db_1       | 2020-03-18T22:06:14.130+0000 I  FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
mongo-db_1       | 2020-03-18T22:06:14.132+0000 I  SHARDING [LogicalSessionCacheRefresh] Marking collection config.system.sessions as collection version: <unsharded>
mongo-db_1       | 2020-03-18T22:06:14.133+0000 I  SHARDING [LogicalSessionCacheReap] Marking collection config.transactions as collection version: <unsharded>
mongo-db_1       | 2020-03-18T22:06:14.133+0000 I  NETWORK  [listener] Listening on /tmp/mongodb-27017.sock
mongo-db_1       | 2020-03-18T22:06:14.133+0000 I  NETWORK  [listener] Listening on 0.0.0.0
mongo-express_1  | /docker-entrypoint.sh: line 14: /dev/tcp/mongo/27017: Invalid argument
mongo-express_1  | Wed Mar 18 22:06:14 UTC 2020 retrying to connect to mongo:27017 (2/5)
mongo-express_1  | /docker-entrypoint.sh: line 14: mongo: Name does not resolve
mongo-db_1       | 2020-03-18T22:06:14.133+0000 I  NETWORK  [listener] waiting for connections on port 27017
mongo-db_1       | 2020-03-18T22:06:15.054+0000 I  FTDC     [ftdc] Unclean full-time diagnostic data capture shutdown detected, found interim file, some metrics may have been lost. OK
mongo-express_1  | /docker-entrypoint.sh: line 14: /dev/tcp/mongo/27017: Invalid argument
mongo-express_1  | Wed Mar 18 22:06:15 UTC 2020 retrying to connect to mongo:27017 (3/5)
mongo-express_1  | /docker-entrypoint.sh: line 14: mongo: Name does not resolve
mongo-express_1  | /docker-entrypoint.sh: line 14: /dev/tcp/mongo/27017: Invalid argument
mongo-express_1  | Wed Mar 18 22:06:16 UTC 2020 retrying to connect to mongo:27017 (4/5)
mongo-express_1  | /docker-entrypoint.sh: line 14: mongo: Name does not resolve
mongo-express_1  | /docker-entrypoint.sh: line 14: /dev/tcp/mongo/27017: Invalid argument
mongo-express_1  | Wed Mar 18 22:06:18 UTC 2020 retrying to connect to mongo:27017 (5/5)
mongo-express_1  | /docker-entrypoint.sh: line 14: mongo: Name does not resolve
mongo-express_1  | /docker-entrypoint.sh: line 14: /dev/tcp/mongo/27017: Invalid argument
mongo-express_1  | Welcome to mongo-express
mongo-express_1  | ------------------------
mongo-express_1  |
mongo-express_1  |
mongo-express_1  | Mongo Express server listening at http://0.0.0.0:8081
mongo-express_1  | Server is open to allow connections from anyone (0.0.0.0)
mongo-express_1  |
mongo-express_1  | /node_modules/mongodb/lib/server.js:265
mongo-express_1  |         process.nextTick(function() { throw err; })
mongo-express_1  |                                       ^
mongo-express_1  | Error [MongoError]: failed to connect to server [mongo:27017] on first connect
mongo-express_1  |     at Pool.<anonymous> (/node_modules/mongodb-core/lib/topologies/server.js:326:35)
mongo-express_1  |     at Pool.emit (events.js:311:20)
mongo-express_1  |     at Connection.<anonymous> (/node_modules/mongodb-core/lib/connection/pool.js:270:12)
mongo-express_1  |     at Object.onceWrapper (events.js:418:26)
mongo-express_1  |     at Connection.emit (events.js:311:20)
mongo-express_1  |     at Socket.<anonymous> (/node_modules/mongodb-core/lib/connection/connection.js:175:49)
mongo-express_1  |     at Object.onceWrapper (events.js:418:26)
mongo-express_1  |     at Socket.emit (events.js:311: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  |   name: 'MongoError',
mongo-express_1  |   message: 'failed to connect to server [mongo:27017] on first connect'
mongo-express_1  | }
express_mongo-express_1 exited with code 1

根据 docs,您应该使用名为 ME_CONFIG_MONGODB_SERVER

的 ENV

ME_CONFIG_MONGODB_SERVER : MongoDB container name. Use comma delimited list of host names for replica sets.

  services:
  mongo-express:
    image: mongo-express
    ports:
      - 8081:8081
    environment:
      ME_CONFIG_MONGODB_SERVER: mongo-server
      ME_CONFIG_BASICAUTH_USERNAME: admin
      ME_CONFIG_BASICAUTH_PASSWORD: q
      ME_CONFIG_MONGODB_PORT: 27017
      ME_CONFIG_MONGODB_ADMINUSERNAME: root
      ME_CONFIG_MONGODB_ADMINPASSWORD: rootpassword
    links:
      - mongo-db
    networks:
      - mongo-compose-network

  mongo-db:
    image: mongo:latest
    container_name: mongo-server
    environment:
      MONGO_INITDB_DATABASE: demodb  
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: rootpassword
    ports:
      - "27017:27017"
    volumes:
      - mongodb_data_container:/data/db
    networks:
      - mongo-compose-network

networks: 
    mongo-compose-network:
      driver: bridge

volumes:
  mongodb_data_container:

注意它已达到最大尝试次数,5/5:

"mongo-express_1 | 3 月 18 日星期三 22:06:18 UTC 2020 重试连接到 mongo:27017 (5/5)"

关于这个问题的 Whosebug 上有很多文章,许多人试图猜测多种答案。我能够通过更改 max_tries 的数量来解决我的问题,但这需要 patch in the mongo-express-docker project.

我遇到了同样的问题

我使用了 docker-compose 来自 mongo docker official image documentation

的示例
# Use root/example as user/password credentials
version: '3.1'

services:

mongo:
  image: mongo
  restart: always
  environment:
    MONGO_INITDB_ROOT_USERNAME: root
    MONGO_INITDB_ROOT_PASSWORD: example

mongo-express:
  image: mongo-express
  restart: always
  ports:
    - 8081:8081
  environment:
    ME_CONFIG_MONGODB_ADMINUSERNAME: root
    ME_CONFIG_MONGODB_ADMINPASSWORD: example

我在最后添加 depends_on: - mongo 解决了我的问题

# Use root/example as user/password credentials
version: '3.1'

services:

mongo:
  image: mongo
  restart: always
  environment:
    MONGO_INITDB_ROOT_USERNAME: root
    MONGO_INITDB_ROOT_PASSWORD: example

mongo-express:
  image: mongo-express
  restart: always
  ports:
    - 8081:8081
  environment:
    ME_CONFIG_MONGODB_ADMINUSERNAME: root
    ME_CONFIG_MONGODB_ADMINPASSWORD: example
  depends_on:
    - mongo