Docker网络在单个主机上组合 [英] Docker networking on single host with compose
问题描述
基本上,为了简化,我有两个服务器,每个在容器中。我们称它们为S1和S2。 S1全局暴露,必须可以从S2访问。我想通过docker-with -x-networking标志运行S2(为什么我想要的是,S2实际上比这里假设的更复杂一些,有几个容器,我希望它们运行在单个子网)。我可以从Docker网络了解到,任何容器都可以从同一个网络到达其他容器,或者可以达到全球的任何内容。曝光通过主机端口映射,对吗?
所以我的方案是:
- 我通过端口映射手动启动S1,例如-p 7210:7202(7202在docker文件中公开)
- S2是从一个简单的文件文件创建的,并与flag -x -networking
对于我的测试用例,我刚刚创建了一个非常简约的撰写文件,例如:
S2:
build:。
ports:
- 8080:80
结果:
- S1从localhost的S2不可见(这是非常预期的)
- S1在172.17.0.1(= interface docker0)下的S2中不可见。
我预计将能够达到它172.17.0.1,因为我使用docker0,因为我明白。
此外,如果我开始S2没有撰写,但手动与码头运行,那么我可以使用172.17.0.1访问S1。
那么为什么它不能用于撰写?由于网络功能仍然是实验性的,这是一个限制吗?
注意: / strong>
这是旧内容。 -x网络已在docker-compose
1.6中删除。另外Docker compose有一个新的文件格式。
文档说明了网络功能 Docker撰写的实验。
注意:Compose的网络支持是实验性的,必须使用docker-compose -x-networking标志显式启用
。
它是实际实现新功能的Docker 1.9:
https://docs.docker.com/engine/userguide/networking/dockernetworks/
示例
└─ - 演示
└──docker-compose.yml
我使用的是docker 1.9。 0和docker-compose 1.5.0
docker-compose.yml
声明两个容器被称为web1和web2。在这种情况下,我正在运行tomcat无论什么图像。
web1:
image:tomcat:8.0
ports:
- 8080
web2:
image:tomcat:8.0
ports:
- 8080
创建容器
使用docker撰写
$ cd demo
$ docker-compose --x-networking up -d
用驱动程序无创建网络demo
创建demo_web2_1
创建demo_web1_1
请注意如何创建一个名为demo的网络您指定了新的 - x-networking 参数。
演示发现如何工作
每个容器都在创建的演示网络上运行,每个容器都作为其他主机文件中的条目放置。
$ docker-compose ps
名称命令状态端口
------------------------------ ----- ----------------------------
demo_web1_1 catalina.sh run Up 0.0.0.0:32773->8080/tcp
demo_web2_1 catalina.sh run Up 0.0.0.0:32772->8080/tcp
$ docker exec -it demo_web1_1 cat / etc / hosts
..
172.18 .0.2 demo_web2_1
$ docker exec -it demo_web2_1 cat / etc / hosts
..
172.18.0.3 demo_web1_1
在compose之外运行一个额外的容器
启动另一个容器,并指定要将其附加到演示网络:
$ docker run --net demo --name helloworld -d tomcat:8.0
查看其他容器中的主机文件如何自动更新
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
$ docker exec -it demo_web2_1 cat / etc / hosts
172.18.0.3 demo_web1_1
172.18.0.4 helloworld
$ docker exec -it helloworld cat / etc / hosts
172.18.0.2 demo_web2_1
172.18.0.3 demo_web1_1
I am trying to run docker networking with compose in docker 1.9. I know this is still experimental in 1.9, but I'd like to know if my use case could work.
Basically and to simplify, I have two servers, each one being in a container. Let's call them S1 and S2. S1 is exposed globally and must be accessible from S2. I want to run S2 through docker-compose with the --x-networking flag (the reason why I want this is that S2 is actually a bit more complexe than what is assumed here, having several containers, and I want them to run in a single subnetwork). S1 can run with or without compose.
What I understand from docker networks is that any container can reach other from the same network, or can reach anything that is "globally" exposed through host port mapping, right?
So my scenario is:
- I start manually S1 with port mapping such as "-p 7210:7202" (7202 is exposed in dockerfile)
- S2 is created from a simple compose file and gone up with flag --x-networking For my test case I just created a very minimalistic compose file, such as:
S2: build: . ports: - "8080:80"
Results:
- S1 is NOT visible from S2 under "localhost" (this is quite expected)
- S1 is NOT visible from S2 under "172.17.0.1" (= interface docker0)
I would have expected to be able to reach it under 172.17.0.1, since S1 uses docker0 as I understand.
Moreover, if I start S2 without compose but manually with "docker run", then I can access S1 using 172.17.0.1
So why doesn't it work with compose? Is it a limitation due to networking features being still experimental?
Note:
This is old content. The --x-networking has been removed in docker-compose 1.6. Additionally Docker compose has a new file format.
The documentation states that the networking feature is experimental in Docker compose.
Note: Compose’s networking support is experimental, and must be explicitly enabled with the docker-compose --x-networking flag.
It's Docker 1.9 that actually implements the new feature:
https://docs.docker.com/engine/userguide/networking/dockernetworks/
Perhaps an example would help.
Example
└── demo
└── docker-compose.yml
I'm using docker 1.9.0 and docker-compose 1.5.0
docker-compose.yml
Declare two containers callled "web1" and "web2". Doesn't matter what image in this case I'm running tomcat.
web1:
image: tomcat:8.0
ports:
- 8080
web2:
image: tomcat:8.0
ports:
- 8080
Create the containers
Start this using docker compose
$ cd demo
$ docker-compose --x-networking up -d
Creating network "demo" with driver "None"
Creating demo_web2_1
Creating demo_web1_1
Note how a network called "demo" is created when you specified the new --x-networking parameter.
Demonstrate how discovery works
Each container is run on the "demo" network that was created and each container is placed as an entry in the other hosts file.
$ docker-compose ps
Name Command State Ports
---------------------------------------------------------------
demo_web1_1 catalina.sh run Up 0.0.0.0:32773->8080/tcp
demo_web2_1 catalina.sh run Up 0.0.0.0:32772->8080/tcp
$ docker exec -it demo_web1_1 cat /etc/hosts
..
172.18.0.2 demo_web2_1
$ docker exec -it demo_web2_1 cat /etc/hosts
..
172.18.0.3 demo_web1_1
Run an additional container outside of compose
Start another container, and specify you want it to be attached to the "demo" network:
$ docker run --net demo --name helloworld -d tomcat:8.0
And see how the hosts files in the other containers is updated automatically
$ docker exec -it demo_web1_1 cat /etc/hosts
..
172.18.0.2 demo_web2_1
172.18.0.4 helloworld
$ docker exec -it demo_web2_1 cat /etc/hosts
172.18.0.3 demo_web1_1
172.18.0.4 helloworld
$ docker exec -it helloworld cat /etc/hosts
172.18.0.2 demo_web2_1
172.18.0.3 demo_web1_1
这篇关于Docker网络在单个主机上组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!