使用Docker-in-Docker的Docker容器网络 [英] Docker Container Networking with Docker-in-Docker

查看:153
本文介绍了使用Docker-in-Docker的Docker容器网络的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过父docker设置与父docker容器中的子docker容器联网.

I would like to network with a child docker container from a parent docker container, with a docker-in-docker setup.

假设我正在尝试连接到简单的Apache httpd服务器.当我在主机上运行httpd容器时,一切正常:

Let's say I'm trying to connect to a simple Apache httpd server. When I run the httpd container on my host machine, everything works fine:

asnyder:~$ docker run -d -p 8080:80 httpd:alpine
asnyder:~$ curl localhost:8080
<html><body><h1>It works!</h1></body></html>

但是当我在docker-in-docker设置中执行相同操作时,出现Connection refused错误:

But when I do the same from a docker-in-docker setup, I get a Connection refused error:

asnyder:~$ docker run -d --name mydind --privileged docker:dind
asnyder:~$ docker run -it --link mydind:docker docker:latest sh
/ # docker run -d -p 8080:80 httpd:alpine
/ # curl localhost:8080
curl: (7) Failed to connect to localhost port 8080: Connection refused

我没有运气就尝试了几次改动.指定0.0.0.0界面:

I have tried a couple alterations without luck. Specifying the 0.0.0.0 interface:

asnyder:~$ docker run -d --name mydind --privileged docker:dind
asnyder:~$ docker run -it --link mydind:docker docker:latest sh
/ # docker run -d -p 0.0.0.0:8080:80 httpd:alpine
/ # curl 0.0.0.0:8080
curl: (7) Failed to connect to 0.0.0.0 port 8080: Connection refused

使用主机网络:

asnyder:~$ docker run -d --name mydind --privileged docker:dind
asnyder:~$ docker run -it --link mydind:docker docker:latest sh
/ # docker run -d --network host httpd:alpine
/ # curl localhost:80
curl: (7) Failed to connect to localhost port 80: Connection refused

令人惊讶的是,我找不到与此相关的任何现有文章.这里有人有见识吗?

Surprisingly, I was unable to find any existing articles on this. Does anyone here have some insight?

谢谢!

推荐答案

DinD和绑定安装Docker套接字都有优点和缺点,并且肯定都有用例.例如,请查看这组博客文章,这样做很好.解释其中一种用例的工作.

There are pros and cons for both DinD and bind mounting the Docker socket and there are certainly use cases for both. As an example, check out this set of blog posts, which does a good job of explaining one of the use cases.

鉴于上面的示例docker-in-docker设置,您可以通过以下两种方式之一访问Apache httpd服务器:

Given your example docker-in-docker setup above, you can access Apache httpd server in one of two ways:

1)从docker:dind容器内部,它将在localhost:8080上可用.

1) From inside the docker:dind container, it will be available on localhost:8080.

2)从docker:latest容器内部(最初尝试访问它的地方),该容器将在为docker:dind容器设置的任何主机名上可用.在这种情况下,您使用的是--name mydind,因此curl mydind:8080将为您提供标准的Apache <html><body><h1>It works!</h1></body></html>.

2) From inside the docker:latest container, where you were trying to access it originally, it will be available on whatever hostname is set for the docker:dind container. In this case, you used --name mydind, therefore curl mydind:8080 would give you the standard Apache <html><body><h1>It works!</h1></body></html>.

希望有道理!

这篇关于使用Docker-in-Docker的Docker容器网络的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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