简体   繁体   中英

Connection time out when mongoose attempts to connect to a mongodb instance running in docker

I am developing a Nodejs application locally in a Windows machine. For that I decided to run Mongodb inside a Docker container simply to learn how to use Docker. I am also running Mongo-express in another container in order to manage the database.

The problem is when I execute the file to connect to the database, a connection time out occours.

This is the file with the Database code:

let mongoose = require('mongoose');
let debug = require('debug')('s-optimizer:mongoDb')

mongoose.connect("mongodb://192.168.99.100:27017/local", {
   user: "Lab",
   pass: "123123as",
   useNewUrlParser: true,
   useUnifiedTopology: true
})
.then(() => debug("Success"))
.catch((error) => debug(error))

let db = mongoose.connection
db.on('error', () => debug("Connection Error"));
db.once('open', () => debug("Connection established"))

And this is the error :

 s-optimizer:mongoDb MongooseTimeoutError: Server selection timed out after 30000 ms
  s-optimizer:mongoDb     at new MongooseTimeoutError (C:\Users\vmari\Projects\S-optimazer\node_modules\mongoose\lib\error\timeout.js:22:11)
  s-optimizer:mongoDb     at NativeConnection.Connection.openUri (C:\Users\vmari\Projects\S-optimazer\node_modules\mongoose\lib\connection.js:763:19)
  s-optimizer:mongoDb     at Mongoose.connect (C:\Users\vmari\Projects\S-optimazer\node_modules\mongoose\lib\index.js:332:15)
  s-optimizer:mongoDb     at Object.<anonymous> (C:\Users\vmari\Projects\S-optimazer\mongo\Database.js:5:10)
  s-optimizer:mongoDb     at Module._compile (internal/modules/cjs/loader.js:959:30)
  s-optimizer:mongoDb     at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)
  s-optimizer:mongoDb     at Module.load (internal/modules/cjs/loader.js:815:32)
  s-optimizer:mongoDb     at Function.Module._load (internal/modules/cjs/loader.js:727:14)
  s-optimizer:mongoDb     at Function.Module.runMain (internal/modules/cjs/loader.js:1047:10)
  s-optimizer:mongoDb     at internal/main/run_main_module.js:17:11 +0ms
  s-optimizer:mongoDb Connection Error +7ms

Information about my environment

I am using docker-machine and virtual box to run Docker in Windows.

This is the enviroment for docker-machine when I run: docker-machine env .

$Env:DOCKER_TLS_VERIFY = "1"
$Env:DOCKER_HOST = "tcp://192.168.99.100:2376"
$Env:DOCKER_CERT_PATH = "C:\Users\vmari\.docker\machine\machines\default"
$Env:DOCKER_MACHINE_NAME = "default"
$Env:COMPOSE_CONVERT_WINDOWS_PATHS = "true"

And the Docker containers running when I do: Docker ps

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                      NAMES
1d9788a4c5da        mongo-express       "tini -- /docker-ent…"   About an hour ago   Up About an hour    0.0.0.0:8081->8081/tcp     fervent_chebyshev      
c7bab68f0725        mongo               "docker-entrypoint.s…"   About an hour ago   Up About an hour    0.0.0.0:27017->27017/tcp   bold_noether

I must point out that I ran mongo-express using docker run -it --rm -d -p 8081:8081 --link bold_noether:mongo mongo-express and that I can access it on the browser on: http://192.168.99.100:8081/ . But when I try to connect to the database container through my code I get the timeout.

Dude white list your IP then check

https://github.com/docker-library/mongo/issues/177

Check the thread it can help you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM