docker-compose service name problem using mongo official image

I followed official mongo image description trying to using it with docker-compose.But i’ve noticed that the mongo service name can not be the same as the image name.Here is the situation:

  1. created a docker-compose.yml file which filled with this:
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
      ME_CONFIG_MONGODB_URL: mongodb://root:example@mongo:27017/

  1. execute docker-compose
docker-compose -f stack.yml up

Then the error appears:

Could not connect to database using connectionString: mongodb://root:example@mongo:27017/"
mongo-express_1  | (node:8) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [mongo:27017] on first connect [MongoError: Authentication failed.
mongo-express_1  |     at Connection.messageHandler (/node_modules/mongodb/lib/core/connection/connection.js:364:19)
mongo-express_1  |     at Connection.emit (events.js:314:20)

But when I change the service name mongo to myMongo,it just work out!:

version: '3.1'

services:

  myMongo:
    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
      ME_CONFIG_MONGODB_URL: mongodb://root:example@myMongo:27017/

So, where am i wrong?

Hi :slight_smile:

I just tried it and had only 1 error, which was because mongo wasn’t completely running before express tried to connect.

Im wondering if you’re experiencing the same thing, since the error you’re getting is “Authentication failed.” and not something connection related.