Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
682 views
in Technique[技术] by (71.8m points)

node.js - Connect to host mongodb from docker container

So I want to connect to my mongodb running on my host machine (DO droplet, Ubuntu 16.04). It is running on the default 27017 port on localhost.

I then use mup to deploy my Meteor app on my DO droplet, which is using docker to run my Meteor app inside a container. So far so good. A standard mongodb://... connection url is used to connect the app to the mongodb. Now I have the following problem:

mongodb://...@localhost:27017... obviously does not work inside the docker container, as localhost is not the host's localhost.

I already read many stackoverflow posts on this, I already tried using:

  • --network="host" - did not work as it said 0.0.0.0:80 is already in use or something like that (nginx proxy)
  • --add-host="local:<MY-DROPLET-INTERNET-IP>" and connect via mongodb://...@local:27017...: also not working as I can access my mongodb only from localhost, not from the public IP

This has to be a common problem!

tl;dr - What is the proper way to expose the hosts localhost inside a docker container so I can connect to services running on the host? (including their ports, e.g. 27017).

I hope someone can help!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use: 172.17.0.1 as it is the default host ip that the containers can see. But you need to configure Mongo to listen to 0.0.0.0.

From docker 18.03 onwards the recommendation is to connect to the special DNS name host.docker.internal

For previous versions you can use DNS names docker.for.mac.localhost or docker.for.windows.localhost.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...