从Docker容器连接到主机mongodb

编程入门 行业动态 更新时间:2024-10-28 07:26:05
本文介绍了从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开始建议是连接到特殊的DNS名称host.docker.internal

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

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

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

更多推荐

从Docker容器连接到主机mongodb

本文发布于:2023-10-14 12:27:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1491054.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:连接到   容器   主机   Docker   mongodb

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!