3

Cann't connect to Mongodb. That installed by Docker

let dbRoute = "mongodb://mongo:27017/kgp_news"

const option = {
    socketTimeoutMS: 30000,
    keepAlive: true,
    reconnectTries: 30000,
    useNewUrlParser: true 
};

mongoose.connect(dbRoute, option)

I install mongo by docker. It run port 27017.

And when I run project by script "node index.js". it is working.

But when I build app by write Dockerfile. Cann't connect to Mongo. MongoNetworkError: connection timed out

    at Pool.<anonymous> (/api_kgp/docker_build/node_modules/mongodb-core/lib/topologies/server.js:431:11)
    at Pool.emit (events.js:198:13)
    at connect (/api_kgp/docker_build/node_modules/mongodb-core/lib/connection/pool.js:557:14)
    at makeConnection (/api_kgp/docker_build/node_modules/mongodb-core/lib/connection/connect.js:39:11)
    at callback (/api_kgp/docker_build/node_modules/mongodb-core/lib/connection/connect.js:261:5)
    at Socket.err (/api_kgp/docker_build/node_modules/mongodb-core/lib/connection/connect.js:286:7)
    at Object.onceWrapper (events.js:286:20)
    at Socket.emit (events.js:198:13)
    at Socket._onTimeout (net.js:442:8)
    at ontimeout (timers.js:436:11)
    at tryOnTimeout (timers.js:300:5)
    at listOnTimeout (timers.js:263:5)
    at Timer.processTimers (timers.js:223:10)
(node:20) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:20) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
2
  • mongo in node.js server, can you ping mongo ? and telnet to port 27017 Commented Jul 10, 2019 at 2:17
  • @ThanhNguyenVan i try and this result: PING mongo.com (54.173.82.137) 56(84) bytes of data. It seem working. Commented Jul 10, 2019 at 5:28

2 Answers 2

7

mongo were installed on container so please make sure:

  1. Expose 27017 port when you run to build container,

    docker run -p 27017:27017 ....

  2. In node code, connect to mongo container using : IP address gateway of container network is 172.17.0.1:27017

So:

let dbRoute = "mongodb://172.17.0.1:27017/kgp_news"

Sign up to request clarification or add additional context in comments.

4 Comments

Yes. It's woking now. Thank you very much. So now when connect with any container of docker using this "172.17.0.1", right?
yes, that's right ! its differ from port as belong to service
But docker documentation says you can use dns alias if the containers are under the same user defined bridge network.
While running test cases using mocha I was getting timeout error, i used the above and it worked like a charm.
0

use localhost of the docker container is running.

let dbRoute = "mongodb://localhost:27017/kgp_news"

const option = {
    socketTimeoutMS: 30000,
    keepAlive: true,
    reconnectTries: 30000,
    useNewUrlParser: true 
};

mongoose.connect(dbRoute, option)

Or, you can put the IP address of mongodb condtiner. To get IP off mongodb conatiner use $ docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $INSTANCE_ID

let dbRoute = "mongodb://<MONGODB_IP_ADDRESS>:27017/kgp_news"

const option = {
    socketTimeoutMS: 30000,
    keepAlive: true,
    reconnectTries: 30000,
    useNewUrlParser: true 
};

mongoose.connect(dbRoute, option)

1 Comment

then put the IP of docker container running. however this ip will change everytime you start you mongodb container.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.