mongo-express-docker icon indicating copy to clipboard operation
mongo-express-docker copied to clipboard

Mongo-express docker fails to connect to Mongo docker if DB volume is mounted

Open ruslaniv opened this issue 5 years ago • 6 comments

I have this docker-compose file:

version: '3.7'
services:
  db-mongo:
    image: mongo
    container_name: db-mongo
  environment:
      - MONGO_INITDB_DATABASE=auth
      - MONGO_INITDB_ROOT_USERNAME=admin
      - MONGO_INITDB_ROOT_PASSWORD=pass
  volumes:
     - /Users/John/Documents/Docker/mongoDB/db-mongo:/data/db
   ports:
      - '27017:27017'
  db-mongo-admin:
    image: mongo-express
    container_name: db-mongo-admin
    depends_on:
      - db-mongo
    environment:
      - ME_CONFIG_MONGODB_SERVER=db-mongo
      - ME_CONFIG_MONGODB_ADMINUSERNAME=admin
      - ME_CONFIG_MONGODB_ADMINPASSWORD=pass
      - ME_CONFIG_BASICAUTH_USERNAME=admin
      - ME_CONFIG_BASICAUTH_PASSWORD=fairlylongpassword
    ports:
      - '8081:8081'

When i start it up, it fails with

...
Mon Jan 27 06:11:19 UTC 2020 retrying to connect to db-mongo:27017 (5/5)
/docker-entrypoint.sh: connect: Connection refused
/docker-entrypoint.sh: line 14: /dev/tcp/db-mongo/27017: Connection refused
Welcome to mongo-express
------------------------
/node_modules/mongodb/lib/server.js:265
        process.nextTick(function() { throw err; })
                                      ^
MongoError: failed to connect to server [db-mongo:27017] on first connect
...

But if I remove the bind mount from the Mongo container, without changing anything else, both containers start fine and I can use mongo-express to log to the DB.

ruslaniv avatar Jan 27 '20 06:01 ruslaniv

This sounds like your MongoDB container is actually failing to start -- I don't think the shared directory provided by Docker Desktop / Docker for Mac OS to the Linux VM is going to be sufficient for what MongoDB expects to be able to do with that filesystem.

tianon avatar Jan 29 '20 00:01 tianon

Having the same issue :-( My MongoDB container is running fine and other clients can connect to it without any issues. Did you find a fix for this @ruslaniv?

akwanashie avatar Mar 15 '20 19:03 akwanashie

completely useless with this kind of bugs

mossad-zika avatar Mar 24 '20 15:03 mossad-zika

Having the same issue :-( My MongoDB container is running fine and other clients can connect to it without any issues. Did you find a fix for this @ruslaniv?

Nope, could not fix it. Just had to use it without mounting external fs.

ruslaniv avatar Mar 25 '20 06:03 ruslaniv

I have exactly the same issue. According to the log, if volume is used, mongodb thinks it has started up 100% when it's actually just 90% started up, then mongo_express will try a connection right away, which will fail

mongodb_1  | {"t":{"$date":"2021-12-11T22:01:54.688+00:00"},"s":"I",  "c":"STORAGE",  "id":22430,   "ctx":"initandlisten","msg":"WiredTiger message","attr":{"message":"[1639260114:688810][1:0x7f1b253c6c80], txn-recover: [WT_VERB_RECOVERY_PROGRESS] Recovering log 2 through 2"}}

...
mongo-express_1  | Could not connect to database using connectionString: mongodb://mongodb:27017/"
...
mongodb_1  | {"t":{"$date":"2021-12-11T22:01:56.466+00:00"},"s":"I",  "c":"NETWORK",  "id":23015,   "ctx":"listener","msg":"Listening on","attr":{"address":"/tmp/mongodb-27017.sock"}}

And when you run docker-compose up mongo-express afterwards, it will connect.

I think the workaround may be letting mongo-express sleep for 5 seconds after mongodb thinks it's up, or retry the connection.

A quick fix: (sleep 5 seconds before exec)

  mongoui:
    image: mongo-express:latest
    ...
    depends_on:
      - mongodb
    entrypoint: [sh, -c, "sleep 5s && tini -- /docker-entrypoint.sh mongo-express"]

chenjianjx avatar Dec 11 '21 22:12 chenjianjx

Btw, why is wait_for_tcp() defined but not called in '/docker-entrypoint.sh' ?

chenjianjx avatar Dec 11 '21 23:12 chenjianjx