cant connect node docker container to mongo docker container

Viewed 3594

I get this error:

Server running...
{ MongoNetworkError: failed to connect to server [mongo:27017] on first connect [MongoNetworkError: getaddrinfo EAI_AGAIN mongo mongo:27017]
    at Pool.<anonymous> (/usr/src/app/node_modules/mongodb-core/lib/topologies/server.js:562:11)
    at Pool.emit (events.js:182:13)
    at Connection.<anonymous> (/usr/src/app/node_modules/mongodb-core/lib/connection/pool.js:316:12)
    at Object.onceWrapper (events.js:273:13)
    at Connection.emit (events.js:182:13)
    at Socket.<anonymous> (/usr/src/app/node_modules/mongodb-core/lib/connection/connection.js:245:50)
    at Object.onceWrapper (events.js:273:13)
    at Socket.emit (events.js:182:13)
    at emitErrorNT (internal/streams/destroy.js:82:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
    at process._tickCallback (internal/process/next_tick.js:63:19)
  name: 'MongoNetworkError',
  message:
   'failed to connect to server [mongo:27017] on first connect [MongoNetworkError: getaddrinfo EAI_AGAIN mongo mongo:27017]',
  errorLabels: [ 'TransientTransactionError' ],
  [Symbol(mongoErrorContextSymbol)]: {} }

this is my docker-compose.yml

version: '3'
services:
  app:
    container_name: docker-node-mongo
    restart: always
    build: .
    ports:
      - '3001:3000'

this is how i tried to connet from node script:

mongoose
  .connect(
    'mongodb://127.0.0.1:27017/test2',
    { useNewUrlParser: true }
  )
  .then(() => console.log('MongoDB Connected'))
  .catch(err => console.log("errorErrorError"));

this is the output of docker container ls -a

6f14a98e2d51        dockernodemongo_app       "npm start"              11 minutes ago      Up 11 minutes           0.0.0.0:3001->3000/tcp                     docker-node-mongo
f3fb55dad886        mongo                     "docker-entrypoint.s…"   24 minutes ago      Up 24 minutes           127.0.0.1:27017->27017/tcp                 mongo

this is how i run mongo from a another docker-compose.yml file:

    mongo:
        restart: always
        image: mongo
        container_name: mongo
        network_mode: "bridge"
#        expose:
#           - 27017
        volumes:
            - ./mongo_data:/data/db
        ports:
            - "127.0.0.1:27017:27017"
1 Answers

You cannot connect your application to mongo that way. 127.0.0.1 refers to the localhost of the container, not the host. You have two options:

  • Create a network and add both containers to it. Then use mongodb://mongo:27017/test2 to reach the mongo container. Docker will use the service name as a host so you can reach it by that name.

  • Bind mongo to 0.0.0.0 (omit the 127.0.0.1 from the port declaration) and connect to the database using the bridge ip address.

Consider the first option, because the second one will expose your mongo database to the internet if you don't have a firewall in place.

For example:

version: '3.5'
services:
  app:
    container_name: docker-node-mongo
    restart: always
    build: .
    ports:
      - '3001:3000'
    networks:
      - mongo

networks:
  mongo:
    external: true

Then on your mongo compose file:

version: '3.5'
services:
  mongo:
    restart: always
    image: mongo
    container_name: mongo
    expose:
      - 27017
    volumes:
      - ./mongo_data:/data/db
    ports:
      - "127.0.0.1:27017:27017"
    networks:
      - mongo

networks:
  mongo:
    name: mongo

Then on your code:

mongoose
  .connect(
    'mongodb://mongo:27017/test2',
    { useNewUrlParser: true }
  )
  .then(() => console.log('MongoDB Connected'))
  .catch(err => console.log("errorErrorError"));

edit: updated network names

Related