0

I have Windows 10 machine where MongoDB is installed. I can connect it from a command line. I run NodeJS app with sam local. When I use a production environment, the app can access Mongo Atlas cloud instance. But when I switch to a dev environment with localhost MongoDB it fails to connect.

The sam command starts Docker so it is clear why it cannot connect Mongo running on windows localhost. I found relevant question: From inside of a Docker container, how do I connect to the localhost of the machine?. The problem is that I still cannot connect my local MongoDB, even if I try:

"MONGODB_URI": "mongodb://docker.for.win.localhost:27018/bud?retryWrites=true&w=majority"

or

"MONGODB_URI": "mongodb://host.docker.internal:27018/bud?retryWrites=true&w=majority"

Error:

Request failed { MongoNetworkError: failed to connect to server [docker.for.win.localhost:27018] on first connect [MongoNetworkError: connect ECONNREFUSED 192.168.65.2:27018]

Has anybody faced this issue as well and overcome it? Mongo is installed directly to windows, not in Docker.

Leos Literak
  • 8,805
  • 19
  • 81
  • 156
  • Docker was running on port 27017 in fact – Leos Literak Feb 02 '20 at 12:30
  • I think I should have asked first which version of docker you are using. I am using docker 19.03.5 Community Edition on Windows 10, and my docker container seems to find `localhost`. Try: `docker run --rm nginx:alpine sh -c "ping localhost"` or `docker run --rm ubuntu sh -c "apt-get update && apt-get install -yq iputils-ping && ping localhost"` – abmblob Feb 02 '20 at 13:46
  • 1
    **Workaround:** Connect to Net / Wifi, get IP address and modify your docker to point to the ip address – Valijon Feb 02 '20 at 17:57
  • Yes, this works. But IMHO it makes my instance public, does not it? – Leos Literak Feb 02 '20 at 20:13

2 Answers2

0

If MongoDB is installed and running directly from windows, it should be accessible via localhost:27017. Default port for mongod is 27017, as described in mongoDB documentation page.

Try using:

"MONGODB_URI": "mongodb://localhost:27017/bud?retryWrites=true&w=majority"

If you are using NETWORKS_DRIVER other than bridge for your NodeJS docker container, which is set by default. Refer to Docker Network drivers

Other cases:

  • The default port for mongod is 27018 when running with --shardsvr command-line option or the shardsvr value for the clusterRole setting in a configuration file.

  • The default port for mongod is 27019 when running with --configsvr command-line option or the configsvr value for the clusterRole setting in a configuration file.

abmblob
  • 361
  • 2
  • 14
  • docker container is running its own linux which overrides localhost/127.0.0.1. It was neccessary to change the URI to host.docker.internal and pass the correct port 27017. – Leos Literak Feb 03 '20 at 07:47
  • I guess, I missed that part. Thanks for letting me know :) – abmblob Feb 03 '20 at 10:49
0

Remember, that localhost (or any name) is just for your convinience. Tcp stack works on ip addresses. If you configure dns service (e.g. via hosts file) to resolve name to 127.0.0.1 for container it doesn't mean your host, but 127.0.0.1 points to the container, always.

You could make mongo service to listen on your main ip and use it for docker app, but you can also leverage hyper-v virtual network cards and setup mongo to listen not only on host's loopback interface, but also on the virtual one and give docker app ip of that interface. It remains on your virtual lan, therefore it's not exposed to public. However, windows firewall might block it, so make sure you set it up as private network (it will be marked as unidentified and by default is public, which usually has stuff blocked).

Miq
  • 3,931
  • 2
  • 18
  • 32