从Docker容器连接到主机mongodb [英] Connect to host mongodb from docker container

查看:690
本文介绍了从Docker容器连接到主机mongodb的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想连接到在主机上运行的mongodb(DO Drop,Ubuntu 16.04).它在本地主机上的默认27017端口上运行.

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.

然后我使用 mup 将我的Meteor应用部署到使用docker的DO Drop上在容器中运行我的Meteor应用程序.到目前为止,一切都很好. 标准的mongodb://...连接URL用于将应用程序连接到mongodb. 现在,我遇到了以下问题:

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...显然在docker容器内不起作用,因为localhost不是主机的本地主机.

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

我已经阅读了很多关于stackoverflow的文章,我已经尝试使用:

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

  • --network="host"-无法正常运行,因为它说0.0.0.0:80已经在使用中或类似的东西(nginx代理)
  • --add-host="local:<MY-DROPLET-INTERNET-IP>"并通过mongodb://...@local:27017...连接:也无法正常工作,因为我只能从本地主机而不是公共IP访问mongodb
  • --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 -在docker容器中公开主机localhost的正确方法是什么,以便我可以连接到主机上运行的服务? (包括其端口,例如27017).

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).

我希望有人能帮忙!

推荐答案

您可以使用:172.17.0.1,因为它是容器可以看到的默认主机ip.但是您需要配置Mongo来收听0.0.0.0.

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.

从docker 18.03开始

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

对于以前的版本,您可以使用DNS名称docker.for.mac.localhostdocker.for.windows.localhost.

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

这篇关于从Docker容器连接到主机mongodb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆