title: mongoose connection failure using docker-compose
body: <p>I have a Node express server consuming a Mongo database.</p>
  
  <p>I'm trying to create a container for each of them using <code>docker-compose</code>.</p>
  
  <p>Here's my <code>docker-compose.yml</code> file:</p>
  
  <pre><code>version: "2"
   services:
    server:
      container_name: server
      restart: always
      build: .
      ports:
        - "3000:3000"
      depends_on:
        - db
    db:
      container_name: db
      image: mongo
      volumes:
        - /var/lib/mongodb:/data/db
      ports:
        - "27017:27017"
  </code></pre>
  
  <p>And my <code>Dockerfile</code>:</p>
  
  <pre><code>FROM node:latest
  RUN mkdir -p /usr/src/app
  WORKDIR /usr/src/app
  COPY package.json /usr/src/app
  RUN npm install
  COPY . /usr/src/app
  RUN npm run build-run
  EXPOSE 3000
  </code></pre>
  
  <p>I saw on many tutorials that, when using Docker to create a Mongo container, the connection string should be updated in <code>mongoose.connect</code> to use Docker containers naming resolution.</p>
  
  <p>So I changed my connection string according to my <code>docker-compose</code> file:</p>
  
  <pre><code>private readonly CONNECTION_STRING: String = 'mongodb://db/search-people-db'
  
  public connect(): void {
      mongoose.connect(this.CONNECTION_STRING)
  
      this._db.on('error', (err) =&gt; {
          console.log(`mongoose server failed to start: ${err}`)
      })
  
      this._db.once('open', () =&gt; {
          console.log(`mongoose server running using ${this.CONNECTION_STRING}`)
      })
  }'
  </code></pre>
  
  <p>However, when running <code>sudo docker-compose up</code>, I keep getting the following error:</p>
  
  <p><strong>Mongoose server failed to start: MongoNetworkError: failed to connect to server [db:27017] on first connect [MongoNetworkError: getaddrinfo ENOTFOUND db db:27017]</strong></p>
  
  <p>What am I doing wrong ? Thanks in advance</p>