简体   繁体   English

Docker:连接到容器(不暴露端口)

[英]Docker: Connect to container (without exposing ports)

First things first: I did use the search, both Google and SO.第一件事:我确实使用了搜索,包括 Google 和 SO。

Task任务

Trying to connect a MongoGUIClient from my Host (OSX) to the MongoDB inside my container.尝试将 MongoGUIClient 从我的主机 (OSX) 连接到我容器内的 MongoDB。

What did I do?我做了什么?

  1. Version: docker run --name some-mongo -d mongo版本: docker run --name some-mongo -d mongo
  2. Version: docker run --name some-mongo -d -p 127.0.0.1:27017:27017 mongo版本: docker run --name some-mongo -d -p 127.0.0.1:27017:27017 mongo

What's the problem?有什么问题?

Using the 2. version I am able to connect from the host without any further problems.使用 2. 版本,我可以从主机连接而不会出现任何其他问题。 But that means I need to expose that port on my system which I do not want to do.但这意味着我需要在我的系统上公开我不想这样做的端口。

What I am trying to achieve我正在努力实现的目标

I want to use the 1. version given above, without exposing ports and then do docker inspect to get the container ip (which in my case is 172.17.0.2 ) and then I want to connect to 172.17.0.2:27017 but that does not work and that is the problem .我想使用上面给出的 1. 版本,不暴露端口,然后执行docker inspect以获取容器 ip(在我的情况下为172.17.0.2 ),然后我想连接到172.17.0.2:27017但这并没有工作,这就是问题所在

What am I doing wrong here?我在这里做错了什么? Is there a conceptual mistake of mine?有我的概念错误吗? I just want to connect to that container sub network without exposing ports.我只想连接到那个容器子网络而不暴露端口。 Is that not possible?那不可能吗?

Even if you could contact directly your container, it exposed port would only be visible by other containers, not by your docker host (xhyve VM) or your actual host (Mac).即使你可以直接联系你的容器,它暴露的端口也只能被其他容器看到,而不是你的 docker 主机(xhyve VM)或你的实际主机(Mac)。

So a mapping is still needed.所以仍然需要映射。
You could then try and use the XHyve Alpine VM IP but... this thread mentions a couple of current (Q4 2016) limitations preventing to do that.然后,您可以尝试使用 XHyve Alpine VM IP,但是…… 该线程提到了一些当前(2016 年第 4 季度)限制阻止这样做。

That is why the current application example only shows access through localhost.这就是当前应用程序示例仅显示通过 localhost 访问的原因。

https://docs.docker.com/docker-for-mac/images/hello-world-nginx.png

From this thread :这个线程

Currently the only way to access containers is by exposing a port.目前访问容器的唯一方法是暴露端口。
There is no docker0 on the Mac. Mac 上没有docker0

Use defreitas/dns-proxy-server .使用defreitas/dns-proxy-server Then you can access the container with its hostname.然后您可以使用其主机名访问容器。

Example:例子:

# First run DPS
$ docker run --rm --hostname dns.mageddo \
-v /var/run/docker.sock:/var/run/docker.sock \
-v /etc/resolv.conf:/etc/resolv.conf \
defreitas/dns-proxy-server

# Then run the container
$ docker run --hostname some-mongo --name some-mongo -d mongo

You can check if it works using netcat:您可以使用 netcat 检查它是否有效:

$ nc -z some-mongo 27017
$ echo $?
0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM