title: Unable to connect the mongodb container to node container in docker
body: <p>I made 3 docker containers from 2 images in <a href="https://github.com/Samar-080301/MernCrudApp" rel="nofollow noreferrer">this repo</a> and 1 using MongoDB public image. I turned ON all three containers using <code>sudo docker-compose -f docker-compose.yaml up</code></p>
  <p><code>docker-compose.yaml</code> is:</p>
  <pre><code>version: '3'
  services:
    frontend:
      image: samar080301/mern-frontend:1.0
      ports:
        - 3000:3000
    backend:  
      image: samar080301/mern-backend:1.0
      ports:
        - 5000:5000
    mongodb:
      image: mongo:latest
      ports:
        - 27017:27017
  </code></pre>
  <p>But the MongoDB couldn't connect with the node server and gave this error:</p>
  <pre><code>backend_1   | &gt; crud-app@1.0.0 start /home/app
  backend_1   | &gt; node server.js
  backend_1   | 
  backend_1   | (node:18) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
  backend_1   | (Use `node --trace-deprecation ...` to show where the warning was created)
  backend_1   | App running on port 5000
  backend_1   | Error with the database! MongoNetworkError: failed to connect to server [localhost:27017] on first connect [Error: connect ECONNREFUSED 127.0.0.1:27017
  backend_1   |     at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16) {
  backend_1   |   name: 'MongoNetworkError'
  backend_1   | }]
  backend_1   |     at Pool.&lt;anonymous&gt; (/home/app/node_modules/mongodb/lib/core/topologies/server.js:438:11)
  backend_1   |     at Pool.emit (events.js:315:20)
  backend_1   |     at /home/app/node_modules/mongodb/lib/core/connection/pool.js:562:14
  backend_1   |     at /home/app/node_modules/mongodb/lib/core/connection/pool.js:995:11
  backend_1   |     at /home/app/node_modules/mongodb/lib/core/connection/connect.js:32:7
  backend_1   |     at callback (/home/app/node_modules/mongodb/lib/core/connection/connect.js:280:5)
  backend_1   |     at Socket.&lt;anonymous&gt; (/home/app/node_modules/mongodb/lib/core/connection/connect.js:310:7)
  backend_1   |     at Object.onceWrapper (events.js:422:26)
  backend_1   |     at Socket.emit (events.js:315:20)
  backend_1   |     at emitErrorNT (internal/streams/destroy.js:84:8)
  backend_1   |     at processTicksAndRejections (internal/process/task_queues.js:84:21)
  backend_1   | (node:18) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [localhost:27017] on first connect [Error: connect ECONNREFUSED 127.0.0.1:27017
  backend_1   |     at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16) {
  backend_1   |   name: 'MongoNetworkError'
  backend_1   | }]
  
  </code></pre>
  <p>Code of <code>backend/db.js</code>:</p>
  <pre><code>const mongoose = require('mongoose');
  // Allow Promises
  mongoose.Promise = global.Promise;
  // Connection
  mongoose.connect('mongodb://localhost:27017/db_test', { useNewUrlParser: true });
  // Validation
  mongoose.connection
    .once('open', () =&gt; console.log('Connected to the database!'))
    .on('error', err =&gt; console.log('Error with the database!', err));
  </code></pre>
  <p>Terminal Output of <code>docker inspect mongodb</code>:
  <a href="https://i.stack.imgur.com/V1glM.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/V1glM.png" alt="enter image description here" /></a></p>
  <p>Terminal output after adding the mongo uri as environment variable:</p>
  <pre><code>backend_1   | App running on port 5000
  backend_1   | (node:19) UnhandledPromiseRejectionWarning: MongooseError: The `uri` parameter to `openUri()` must be a string, got &quot;undefined&quot;. Make sure the first parameter to `mongoose.connect()` or `mongoose.createConnection()` is a string.
  backend_1   | (node:19) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
  </code></pre>
  <p>New error:</p>
  <pre><code>backend_1   | Error with the database! MongoNetworkError: failed to connect to server [merncrudapp_mongodb_1:27017] on first connect [Error: connect ECONNREFUSED 172.23.0.3:27017
  backend_1   |     at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16) {
  backend_1   |   name: 'MongoNetworkError'
  backend_1   | }]
  </code></pre>