从 docker 容器连接到主机 mongodb

编程入门 行业动态 更新时间:2024-10-28 11:32:30
本文介绍了从 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.localhost 或 docker.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

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

发布评论

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

>www.elefans.com

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