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

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

问题描述

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

然后我使用 mup 将我的 Meteor 应用程序部署在使用 docker 的 DO 液滴上在容器内运行我的 Meteor 应用程序.到现在为止还挺好.标准的 mongodb://... 连接 url 用于将应用程序连接到 mongodb.现在我有以下问题:

mongodb://...@localhost:27017... 显然在 docker 容器中不起作用,因为 localhost 不是主机的 localhost.p>

我已经阅读了很多关于此的 stackoverflow 帖子,我已经尝试过使用:

  • --network="host" - 没有工作,因为它说 0.0.0.0:80 已经在使用或类似的东西(nginx 代理)
  • --add-host="local:<MY-DROPLET-INTERNET-IP>" 并通过 mongodb://...@local:27017... 连接:也无法正常工作,因为我只能从本地主机访问我的 mongodb,而不是从公共 IP

这一定是个常见问题!

tl;dr - 在 docker 容器中公开主机 localhost 以便我可以连接到主机上运行的服务的正确方法是什么?(包括它们的端口,例如 27017).

希望有人能帮忙!

解决方案

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

从 docker 18.03 开始​​ 建议是连接到特殊的DNS名称host.docker.internal

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

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!

解决方案

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.

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

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