在docker中使用docker时如何共享主机网桥

时间:2015-08-30 23:40:34

标签: docker

我正在使用https://github.com/jpetazzo/dind泊坞窗图片将码头工具停靠在泊坞窗中。在父docker中启动docker容器时,是否可以使用父docker的桥接器,以便我可以在docker容器内的容器和父docker容器之间共享网络?

我想要做的是直接通过IP从主机访问父docker容器内的容器,为它们分配域名。

更新 - >主要观点

我升级了免费的在线Java编译器,以允许用户使用docker运行任何程序。因此,我使用dind(docker image中的docker)来启动一个主容器,该容器内部有一个接收请求并在其中启动docker容器的Java程序。 所以我想要做的是让用户选择运行暴露端口的程序,让他们使用子域访问他们的容器。 所以图形上我有这个层次结构

Internet -> My Host -> Main Docker Container -> User Docker Container 1
                                             -> User Docker Container 2
                                             -> User Docker Container n

我想要做的是给用户一个子域名来访问他的"用户Docker容器"例如:www.user_25.compiler1.browxy.com

所以他可以有一个程序在他的用户Docker容器中公开一个端口"并可以使用子域www.user_25.compiler1.browxy.com

访问它

令我困惑的是访问"用户Docker容器"我需要在主Docker容器之前访问。我试图找到一种方法来访问"用户Docker容器"直接,所以我认为如果用户Docker容器和主Docker容器可以共享同一个网络,我可以直接从主机访问用户Docker容器,并为"用户Docker容器" IP更新主机上的/ etc / hosts文件。

非常感谢任何建议或建议:)

2 个答案:

答案 0 :(得分:0)

如果你运行"主码头集装箱"使用--net=host,您的配置简化为:

Internet -> Host -> User Docker Container 1
                 -> User Docker Container 2
                 -> User Docker Container n

虽然您可能希望为子容器使用除docker0之外的桥接器(例如,创建一个新桥docker1,然后使用-b docker1启动您的dind Docker守护程序。)< / p>

如果两个用户尝试在相同的IP地址的同一端口上发布服务,那么是的,您将遇到端口冲突。有几种方法可以解决这个问题:

  1. 如果您可以在主机上支持多个公共IP地址,那么您可以&#34;分配&#34; (在引号中,因为这不是自动的)每个容器一个。您需要将绑定ip显式化,而不是docker run -p 80:80 ...,而不是docker run -p 80:80:1.2.3.4。这要求人们要好好玩耍#34 ;;也就是说,没有什么可以阻止某人忘记指定绑定地址或指定错误的地址。

  2. 如果您明确运行 web 服务,那么您可以使用某种前端代理将子域名映射到使用基于名称的虚拟主机的容器。此过程有几个组件,使其自动可能需要一些工作。手动操作相对容易(例如,只更新/etc/hosts),但是很脆弱,因为当重新启动容器时,它将具有新的IP地址。像a dynamic dns service这样的东西可以帮助解决这个问题。

  3. 这些主要是建议而非解决方案,但如果您想了解更多详情,请与我们联系。可能还有其他方法可以破解这种特殊的坚果,所以希望其他人可以参与其中。

答案 1 :(得分:0)

Finally I took many ideas that larsks gave me and this is what I did

  1. Start docker in docker container with a name (--name compiler)
  2. Execute this command in the host -> sudo route add -net 10.0.0.0 gw docker inspect --format '{{ .NetworkSettings.IPAddress }}' compiler netmask 255.255.255.0

For this to work I added a custom bridge in the docker in docker container that ensure that the ip range is 10.0.0.0/24

Now I can ping containers created inside the docker in docker container from the host

To have name resolution I installed docker-dns as larsks suggested into the docker in docker container and added the IP of it to /etc/resolv.conf in the host

The result is that from the host I can access containers by name that are created inside the docker in docker container.

One possible updgrade thing that I'd like to have is to configure everything with docker and don't add custom stuff into the host but by now I don't know how to do that and I can live with this solution