1

While running my docker-compose of a frontend, backend and mongo instance, the backend is not able to connect to the mongo db container.

I am using Mongoose and on NestJs on the backend, the connection code looks like this:

const mongoUser = process.env.MONGO_USER;
const mongoPassword = process.env.MONGO_PASSWORD;
const mongoHostname = process.env.MONGO_HOSTNAME;
const mongoPort = process.env.MONGO_PORT;


const url = `mongodb://${mongoUser}:${mongoPassword}@${mongoHostname}:${mongoPort}`;

@Module({
  imports: [MongooseModule.forRoot(url, {
    useNewUrlParser: true,
    "user": process.env.MONGO_USER,
    "pass": process.env.MONGO_PASSWORD....

And the corresponding docker-compose.yaml section looks like this:

  backend:
    container_name: backend
    build:
      context: ./backend
      dockerfile: Dockerfile
    ports:
      - 4001:4001
    depends_on:
      - mongo
    links:
      - mongo
    environment:
      MONGO_USER: root
      MONGO_PASSWORD: example
      MONGO_HOSTNAME: mongo
      MONGO_PORT: 27017
      PORT: 4001
    command: npm run start:dev

  mongo:
    image: mongo
    restart: always
    ports:
     - 27017:27017
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: example

In the logs of the container I can see a connection is being attempted, but the authentication fails.

Here are the logs. It's strange because the environment ports work and point it at the database, but apparently mongoDB is not recognizing the username and password.

backend          | [Nest] 34   - 01/05/2021, 9:09:49 PM   [InstanceLoader] JwtModule dependencies initialized +1ms
mongo_1          | {"t":{"$date":"2021-01-05T21:09:49.815+00:00"},"s":"I",  "c":"NETWORK",  "id":22943,   "ctx":"listener","msg":"Connection accepted","attr":{"remote":"172.18.0.5:47566","connectionId":4,"connectionCount":3}}
mongo_1          | {"t":{"$date":"2021-01-05T21:09:49.847+00:00"},"s":"I",  "c":"NETWORK",  "id":51800,   "ctx":"conn4","msg":"client metadata","attr":{"remote":"172.18.0.5:47566","client":"conn4","doc":{"driver":{"name":"nodejs|Mongoose","version":"3.6.3"},"os":{"type":"Linux","name":"linux","architecture":"x64","version":"4.19.76-linuxkit"},"platform":"'Node.js v14.15.4, LE (unified)","version":"3.6.3|5.11.9"}}}
mongo_1          | {"t":{"$date":"2021-01-05T21:09:49.856+00:00"},"s":"I",  "c":"NETWORK",  "id":22943,   "ctx":"listener","msg":"Connection accepted","attr":{"remote":"172.18.0.5:47568","connectionId":5,"connectionCount":4}}
mongo_1          | {"t":{"$date":"2021-01-05T21:09:49.876+00:00"},"s":"I",  "c":"NETWORK",  "id":51800,   "ctx":"conn5","msg":"client metadata","attr":{"remote":"172.18.0.5:47568","client":"conn5","doc":{"driver":{"name":"nodejs|Mongoose","version":"3.6.3"},"os":{"type":"Linux","name":"linux","architecture":"x64","version":"4.19.76-linuxkit"},"platform":"'Node.js v14.15.4, LE (unified)","version":"3.6.3|5.11.9"}}}
mongo_1          | {"t":{"$date":"2021-01-05T21:09:49.877+00:00"},"s":"I",  "c":"ACCESS",   "id":20251,   "ctx":"conn5","msg":"Supported SASL mechanisms requested for unknown user","attr":{"user":"undefined@admin"}}
mongo_1          | {"t":{"$date":"2021-01-05T21:09:49.877+00:00"},"s":"I",  "c":"ACCESS",   "id":20249,   "ctx":"conn5","msg":"Authentication failed","attr":{"mechanism":"SCRAM-SHA-256","principalName":"undefined","authenticationDatabase":"admin","client":"172.18.0.5:47568","result":"UserNotFound: Could not find user \"undefined\" for db \"admin\""}}
mongo_1          | {"t":{"$date":"2021-01-05T21:09:49.880+00:00"},"s":"I",  "c":"ACCESS",   "id":20249,   "ctx":"conn5","msg":"Authentication failed","attr":{"mechanism":"SCRAM-SHA-1","principalName":"undefined","authenticationDatabase":"admin","client":"172.18.0.5:47568","result":"UserNotFound: Could not find user \"undefined\" for db \"admin\""}}
mongo_1          | {"t":{"$date":"2021-01-05T21:09:49.883+00:00"},"s":"I",  "c":"NETWORK",  "id":22944,   "ctx":"conn4","msg":"Connection ended","attr":{"remote":"172.18.0.5:47566","connectionId":4,"connectionCount":3}}
backend          | [Nest] 34   - 01/05/2021, 9:09:49 PM   [MongooseModule] Unable to connect to the database. Retrying (1)... +73ms

1 Answer 1

3

I beautified the JSON output from mongo and I found UserNotFound: Could not find user \"undefined\" for db \"admin\":

{
  "t": {
    "$date": "2021-01-05T21:09:49.877+00:00"
  },
  "s": "I",
  "c": "ACCESS",
  "id": 20251,
  "ctx": "conn5",
  "msg": "Supported SASL mechanisms requested for unknown user",
  "attr": {
    "user": "undefined@admin"
  }
} {
  "t": {
    "$date": "2021-01-05T21:09:49.877+00:00"
  },
  "s": "I",
  "c": "ACCESS",
  "id": 20249,
  "ctx": "conn5",
  "msg": "Authentication failed",
  "attr": {
    "mechanism": "SCRAM-SHA-256",
    "principalName": "undefined",
    "authenticationDatabase": "admin",
    "client": "172.18.0.5:47568",
    "result": "UserNotFound: Could not find user \"undefined\" for db \"admin\""
  }
}

According to the documentation the database name should be at the end of the url like:

url = mongodb://${mongoUser}:${mongoPassword}@${mongoHostname}:${mongoPort}/DB-NAME-HERE

Since you can use the options "user", "pass" and "dbName", I would suggest to prune from the url of the credentials or remove the options user and pass.

Sign up to request clarification or add additional context in comments.

4 Comments

I've tried about every combination, but I get exactly the same errors. It looks like it's not receiving the credentials at all. Also, pruning them from the URL wouldn't be possible since then the URL wouldn't exist?
Are you sure you added the database name at the end of the url? Still the same error? I have a code that works with the credentials in the url and no credential in the options. It's the same architecture than you, connection to a mongo container.
Hey @bidetaggle, thanks for your help. Your answer is correct, but at the time it wasn't working because I forgot to rebuild my docker image like a clown. So the code changes I was making weren't being reflected in the image.
haha this is a good one, I'm glad you solved your problem. Thanks for letting me know!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.