简体   繁体   中英

NodeJS MongoDB API in Docker Container can't connect to Database running on Host

I have a MongoDB Database set up on my host server and a docker container running on that host with a NodeJS application. In that application, I try to connect to the Database on the host but the request always times out.

I set the network to "host" and used the adress "host.docker.internal". I also tried installing mongodb-shell and it actually works. So basically I can connect to the database with the shell in the container but not with the MongoDB NodeJS API.

Code of the NodeJS application:

const mongodb = require('mongodb');
mongodb.MongoClient.connect("mongodb://host.docker.internal:27017/");

using mongo mongodb://host.docker.internal:27017/ in the shell works and I connect correctly.

I finally found out the issue. I had to bind it to the correct IPs / in my config I used the argument --bind_ip_all .

Thanks a lot for the help!

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