title: Can't connect to mongo in Docker
body: <p>I'm practicing Docker with a dummy app but I can't connect with the database. If anyone can give a clue about what the problem could be, please. Thanks in advance for any help. If any more info is needed please let me know.</p>
  <p>Here is my Dockerfile for api:</p>
  <p><strong>Dockerfile in server</strong></p>
  <pre><code>FROM node:14-alpine
  
  WORKDIR usr/app
  
  COPY package*.json ./
  
  RUN npm install
  
  COPY . .
  
  EXPOSE 3400
  
  CMD [&quot;npm&quot;, &quot;start&quot;]
  
  </code></pre>
  <p><strong>Here is my connection to db in db.js</strong></p>
  <pre><code>const mongoose = require('mongoose');
  
  const uri = 'mongodb://mongo:27017/prueba-docker';
  const connect = async () =&gt; {
    try {
      await mongoose.connect(uri, {});
      console.log('conected to db');
    } catch (err) {
      console.log('error connecting to db', err);
    }
  };
  module.exports = { connect };
  </code></pre>
  <p><strong>And here docker-compose</strong></p>
  <pre><code>version: &quot;3&quot;
  services:
    prueba-react:
      image: prueba-react
      build: ./client/
      stdin_open: true
      ports: 
        - &quot;3000:3000&quot;
      networks: 
        - mern-app
    
    prueba-api:
      image: prueba-api
      build: ./server/
      ports: 
        - &quot;3400:3400&quot;
      networks:
        - mern-app
      depends_on:
        - db
    db:
      image: mongo:4.4-bionic
      ports:
        - &quot;27017:27017&quot;
      networks:
        - mern-app
      volumes:
        - mongo-data:/data/db
  
  networks:
    mern-app:
      driver: bridge
  
  volumes:
    mongo-data:
      driver: local
  
  </code></pre>
  <p>First I found out that I should  stop mongo wiht the command because I had a port conflict.</p>
  <pre><code>sudo systemctl stop mongod
  </code></pre>
  <p>But now I don't understand why It gives an error when I do docker-compose up</p>
  <p>This is the error I get</p>
  <pre><code>prueba-api_1    | error connecting to db MongooseServerSelectionError: getaddrinfo EAI_AGAIN mongo
  prueba-api_1    |     at NativeConnection.Connection.openUri (/usr/app/node_modules/mongoose/lib/connection.js:796:32)
  prueba-api_1    |     at /usr/app/node_modules/mongoose/lib/index.js:328:10
  prueba-api_1    |     at /usr/app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:32:5
  prueba-api_1    |     at new Promise (&lt;anonymous&gt;)
  prueba-api_1    |     at promiseOrCallback (/usr/app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:10)
  prueba-api_1    |     at Mongoose._promiseOrCallback (/usr/app/node_modules/mongoose/lib/index.js:1149:10)
  prueba-api_1    |     at Mongoose.connect (/usr/app/node_modules/mongoose/lib/index.js:327:20)
  prueba-api_1    |     at Object.connect (/usr/app/db.js:6:20)
  prueba-api_1    |     at Object.&lt;anonymous&gt; (/usr/app/app.js:6:4)
  prueba-api_1    |     at Module._compile (internal/modules/cjs/loader.js:1072:14) {
  prueba-api_1    |   reason: TopologyDescription {
  prueba-api_1    |     type: 'Unknown',
  prueba-api_1    |     servers: Map(1) { 'mongo:27017' =&gt; [ServerDescription] },
  prueba-api_1    |     stale: false,
  prueba-api_1    |     compatible: true,
  prueba-api_1    |     heartbeatFrequencyMS: 10000,
  prueba-api_1    |     localThresholdMS: 15,
  prueba-api_1    |     logicalSessionTimeoutMinutes: undefined
  prueba-api_1    |   }
  
  </code></pre>