title: NestJs / Mongoose cannot make MongoDB database connection while on Docker compose
body: <p>While running my docker-compose of a frontend, backend and mongo instance, the backend is not able to connect to the mongo db container.</p>
  <p>I am using Mongoose and on NestJs on the backend, the connection code looks like this:</p>
  <pre><code>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,
      &quot;user&quot;: process.env.MONGO_USER,
      &quot;pass&quot;: process.env.MONGO_PASSWORD....
  </code></pre>
  <p>And the corresponding docker-compose.yaml section looks like this:</p>
  <pre><code>  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
  </code></pre>
  <p>In the logs of the container I can see a connection is being attempted, but the authentication fails.</p>
  <p>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.</p>
  <pre><code>backend          | [Nest] 34   - 01/05/2021, 9:09:49 PM   [InstanceLoader] JwtModule dependencies initialized +1ms
  mongo_1          | {&quot;t&quot;:{&quot;$date&quot;:&quot;2021-01-05T21:09:49.815+00:00&quot;},&quot;s&quot;:&quot;I&quot;,  &quot;c&quot;:&quot;NETWORK&quot;,  &quot;id&quot;:22943,   &quot;ctx&quot;:&quot;listener&quot;,&quot;msg&quot;:&quot;Connection accepted&quot;,&quot;attr&quot;:{&quot;remote&quot;:&quot;172.18.0.5:47566&quot;,&quot;connectionId&quot;:4,&quot;connectionCount&quot;:3}}
  mongo_1          | {&quot;t&quot;:{&quot;$date&quot;:&quot;2021-01-05T21:09:49.847+00:00&quot;},&quot;s&quot;:&quot;I&quot;,  &quot;c&quot;:&quot;NETWORK&quot;,  &quot;id&quot;:51800,   &quot;ctx&quot;:&quot;conn4&quot;,&quot;msg&quot;:&quot;client metadata&quot;,&quot;attr&quot;:{&quot;remote&quot;:&quot;172.18.0.5:47566&quot;,&quot;client&quot;:&quot;conn4&quot;,&quot;doc&quot;:{&quot;driver&quot;:{&quot;name&quot;:&quot;nodejs|Mongoose&quot;,&quot;version&quot;:&quot;3.6.3&quot;},&quot;os&quot;:{&quot;type&quot;:&quot;Linux&quot;,&quot;name&quot;:&quot;linux&quot;,&quot;architecture&quot;:&quot;x64&quot;,&quot;version&quot;:&quot;4.19.76-linuxkit&quot;},&quot;platform&quot;:&quot;'Node.js v14.15.4, LE (unified)&quot;,&quot;version&quot;:&quot;3.6.3|5.11.9&quot;}}}
  mongo_1          | {&quot;t&quot;:{&quot;$date&quot;:&quot;2021-01-05T21:09:49.856+00:00&quot;},&quot;s&quot;:&quot;I&quot;,  &quot;c&quot;:&quot;NETWORK&quot;,  &quot;id&quot;:22943,   &quot;ctx&quot;:&quot;listener&quot;,&quot;msg&quot;:&quot;Connection accepted&quot;,&quot;attr&quot;:{&quot;remote&quot;:&quot;172.18.0.5:47568&quot;,&quot;connectionId&quot;:5,&quot;connectionCount&quot;:4}}
  mongo_1          | {&quot;t&quot;:{&quot;$date&quot;:&quot;2021-01-05T21:09:49.876+00:00&quot;},&quot;s&quot;:&quot;I&quot;,  &quot;c&quot;:&quot;NETWORK&quot;,  &quot;id&quot;:51800,   &quot;ctx&quot;:&quot;conn5&quot;,&quot;msg&quot;:&quot;client metadata&quot;,&quot;attr&quot;:{&quot;remote&quot;:&quot;172.18.0.5:47568&quot;,&quot;client&quot;:&quot;conn5&quot;,&quot;doc&quot;:{&quot;driver&quot;:{&quot;name&quot;:&quot;nodejs|Mongoose&quot;,&quot;version&quot;:&quot;3.6.3&quot;},&quot;os&quot;:{&quot;type&quot;:&quot;Linux&quot;,&quot;name&quot;:&quot;linux&quot;,&quot;architecture&quot;:&quot;x64&quot;,&quot;version&quot;:&quot;4.19.76-linuxkit&quot;},&quot;platform&quot;:&quot;'Node.js v14.15.4, LE (unified)&quot;,&quot;version&quot;:&quot;3.6.3|5.11.9&quot;}}}
  mongo_1          | {&quot;t&quot;:{&quot;$date&quot;:&quot;2021-01-05T21:09:49.877+00:00&quot;},&quot;s&quot;:&quot;I&quot;,  &quot;c&quot;:&quot;ACCESS&quot;,   &quot;id&quot;:20251,   &quot;ctx&quot;:&quot;conn5&quot;,&quot;msg&quot;:&quot;Supported SASL mechanisms requested for unknown user&quot;,&quot;attr&quot;:{&quot;user&quot;:&quot;undefined@admin&quot;}}
  mongo_1          | {&quot;t&quot;:{&quot;$date&quot;:&quot;2021-01-05T21:09:49.877+00:00&quot;},&quot;s&quot;:&quot;I&quot;,  &quot;c&quot;:&quot;ACCESS&quot;,   &quot;id&quot;:20249,   &quot;ctx&quot;:&quot;conn5&quot;,&quot;msg&quot;:&quot;Authentication failed&quot;,&quot;attr&quot;:{&quot;mechanism&quot;:&quot;SCRAM-SHA-256&quot;,&quot;principalName&quot;:&quot;undefined&quot;,&quot;authenticationDatabase&quot;:&quot;admin&quot;,&quot;client&quot;:&quot;172.18.0.5:47568&quot;,&quot;result&quot;:&quot;UserNotFound: Could not find user \&quot;undefined\&quot; for db \&quot;admin\&quot;&quot;}}
  mongo_1          | {&quot;t&quot;:{&quot;$date&quot;:&quot;2021-01-05T21:09:49.880+00:00&quot;},&quot;s&quot;:&quot;I&quot;,  &quot;c&quot;:&quot;ACCESS&quot;,   &quot;id&quot;:20249,   &quot;ctx&quot;:&quot;conn5&quot;,&quot;msg&quot;:&quot;Authentication failed&quot;,&quot;attr&quot;:{&quot;mechanism&quot;:&quot;SCRAM-SHA-1&quot;,&quot;principalName&quot;:&quot;undefined&quot;,&quot;authenticationDatabase&quot;:&quot;admin&quot;,&quot;client&quot;:&quot;172.18.0.5:47568&quot;,&quot;result&quot;:&quot;UserNotFound: Could not find user \&quot;undefined\&quot; for db \&quot;admin\&quot;&quot;}}
  mongo_1          | {&quot;t&quot;:{&quot;$date&quot;:&quot;2021-01-05T21:09:49.883+00:00&quot;},&quot;s&quot;:&quot;I&quot;,  &quot;c&quot;:&quot;NETWORK&quot;,  &quot;id&quot;:22944,   &quot;ctx&quot;:&quot;conn4&quot;,&quot;msg&quot;:&quot;Connection ended&quot;,&quot;attr&quot;:{&quot;remote&quot;:&quot;172.18.0.5:47566&quot;,&quot;connectionId&quot;:4,&quot;connectionCount&quot;:3}}
  backend          | [Nest] 34   - 01/05/2021, 9:09:49 PM   [MongooseModule] Unable to connect to the database. Retrying (1)... +73ms
  </code></pre>