Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cant connect to existing mongo container #2

Open
guitar9 opened this issue Nov 13, 2018 · 2 comments
Open

Cant connect to existing mongo container #2

guitar9 opened this issue Nov 13, 2018 · 2 comments

Comments

@guitar9
Copy link

guitar9 commented Nov 13, 2018

here is my docker-compose.yml:

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

my index.js

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

and this when i run docker container ls -a

6f14a98e2d51        dockernodemongo_app       "npm start"              7 minutes ago       Up 7 minutes            0.0.0.0:3001->3000/tcp                     docker-node-mongo

f3fb55dad886        mongo                     "docker-entrypoint.s…"   19 minutes ago      Up 19 minutes           127.0.0.1:27017->27017/tcp                 mongo

this is the error when i type docker logs docker-node-mongo

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)]: {} }
@radek3911
Copy link

radek3911 commented Nov 23, 2018

If you already have existing mongodb container make sure you have

external_links:
  - mongo

in your dockerfile, then just connect to it as mongodb://mongo:27017/test2

@Esom
Copy link

Esom commented Feb 24, 2020

this just helped me. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants