泊坞窗1.9:添加--link目标运行Nginx的容器 [英] Docker 1.9: Adding --link target to a running nginx container

查看:184
本文介绍了泊坞窗1.9:添加--link目标运行Nginx的容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问:

我在部署1.9泊坞窗

我第一次运行nginx的容器:

 泊坞窗运行\\
  --name = nginx的\\
  --link = php1:php1 \\
  --link = PHP2:PHP2 \\
  --restart =总是\\
  -p 80:80 -p 443:443 \\
  -v /var/docker/nginx/conf.d:/etc/nginx/conf.d \\
  -d nginx的:最新

请注意,我相连的两个存在于PHP的容器: php1 PHP2

现在,我开始一个新的容器 PHP3

我可以添加一个链接 nginx的 PHP3 与主机上的单个bash脚本?


我为什么要这么做?

由于我有很多不同的PHP应用程序部署,每个实例我将创建一个新的PHP:FPM泊坞窗容器运行code

和它们共享相同的 nginx的服务。

我想打一个动态的部署bash脚本的PHP应用程序,使一键部署。

但我阻止了nginx的不知道新创建的PHP的主机名。


解决方案

添加动态链接问题3155 。结果
pre-libnetwork(1.9主要很快1.10),你可以使用 jwilder / nginx的代理为了生成正确的 nginx.conf 并重新启动nginx的服务每一个新的容器需要被链接的时间。

但随着 libnetwork (和它的 0.6版本月初),搬运工1.10附带了新的泊坞窗网络功能

这意味着你将可以将您的容器重视的用户定义的网络,通过名字使它们自动可见每个人的!

  $泊坞窗运行-itd --name = container2的busybox的
498eaaaf328e1018042c04b2de04036fc04719a6e39a097a4f4866043a2c2152


  

然后创建一个孤立的,桥网络进行测试。


  $泊坞窗网创建-d桥--subnet 172.25.0.0/16 isolated_nw
06a62f1c73c4e3107c0f555b7a5f163309827bfbbf999840166065a8f35455a8$搬运工运行-itd --name = container2的busybox的
498eaaaf328e1018042c04b2de04036fc04719a6e39a097a4f4866043a2c2152
$搬运工网络连接isolated_nw container2的$搬运工运行--net = isolated_nw --ip = 172.25.3.3 -itd --name = container3的busybox


  

选择的IP地址是网络容器配置的一部分,将是整个集装箱装载pserved $ P $。结果
  该功能仅适用于用户定义的网络可用,因为他们保证他们的子网的配置不会在后台更改重装


  
  

isolated_nw 这是用户定义的,泊坞窗内嵌DNS服务器实现名称解析为网络中的其他容器。里面的 container2的,可以ping通 container3 按名称


它的意思是,与码头工人1.10,附加到用户定义的网络容器可以通过名称看到对方。结果
但是,还有更多:<一href=\"https://github.com/docker/docker/blob/master/docs/userguide/networking/work-with-networks.md#linking-containers-in-user-defined-networks\"相对=nofollow> 链接在用户定义的网络容器

  $泊坞窗运行--net = isolated_nw -itd --name = container4 --link container5:C5的busybox
01b5df970834b77a9eadbaff39051f237957bd35c4c56f11193e0594cfd5117c


  

使用的帮助 - 链接 container4 将能够达到 container5 使用别名名称 C5 以及


  
  

请注意,在创建 container4 ,我们链接到名为 container5 容器未创建但结果
  这是在用户定义的网络默认桥接网络传统链路和新链路功能之间的行为差​​异之一。


  
  

<强> - 遗留链路在本质上是静态的,与别名它硬结合在容器和它不容忍链接容器重新启动搜索。
   - 而在用户定义的网络新链接的功能在本质上是动态的,并支持链接的容器,包括重新启动容忍链接的容器上的IP地址更改



您将在新的泊坞窗1.10有两种选择:


  • 要么有一个固定的nginx的配置使得反向代理可能尚未创建容器 C1 C2 C3 ...
    每一个新的容器中创建的时候,你的相应链接重新启动你的nginx的容器: - 链接C2:myNewContainer2


  • 或你的nginx容器的主要过程实际上监视用户定义的网络,并为检测到的每个新的集装箱,重新Nginx的CONF和缓慢重新启动Nginx的守护进程。


Question:

I'm deploying on docker 1.9

I run an nginx container first:

docker run \
  --name=nginx \
  --link=php1:php1 \
  --link=php2:php2 \
  --restart=always \
  -p 80:80 -p 443:443 \
  -v /var/docker/nginx/conf.d:/etc/nginx/conf.d \
  -d nginx:latest

Note that I linked two exist php containers: php1 and php2.

Now I started a new container php3.

Can I add a link the nginx to php3 with a single bash script on the host machine?


Why I want to do this?

Because I have many different php apps to deploy, and each instance I will create a new php:fpm docker container to run the code.

And they share the same nginx service.

I want to make a dynamic deploy bash script for the php app, to make one-key deployment.

But I was blocked that the nginx doesn't know the newly created php host name.

解决方案

Adding link dynamically was part of issue 3155.
Pre-libnetwork (1.9 and mainly soon 1.10), you could use jwilder/nginx-proxy in order to generate the right nginx.conf and restart the nginx service each time a new container needs to be linked.

But with libnetwork (and its 0.6 release early february), docker 1.10 comes with new Docker networks features.

That means you will be able to attach your containers to a user defined network, making them automatically visible from each others by name!

$ docker run -itd --name=container2 busybox
498eaaaf328e1018042c04b2de04036fc04719a6e39a097a4f4866043a2c2152

Then create an isolated, bridge network to test with.

$ docker network create -d bridge --subnet 172.25.0.0/16 isolated_nw
06a62f1c73c4e3107c0f555b7a5f163309827bfbbf999840166065a8f35455a8

$ docker run -itd --name=container2 busybox
498eaaaf328e1018042c04b2de04036fc04719a6e39a097a4f4866043a2c2152
$ docker network connect isolated_nw container2

$ docker run --net=isolated_nw --ip=172.25.3.3 -itd --name=container3 busybox

The selected IP address is part of the container networking configuration and will be preserved across container reload.
The feature is only available on user defined networks, because they guarantee their subnets configuration does not change across daemon reload.

On the isolated_nw which was user defined, the Docker embedded DNS server enables name resolution for other containers in the network. Inside of container2 it is possible to ping container3 by name

What it means is, with docker 1.10, containers attached to a user-defined network can see each other by names.
But there is more: Linking containers in user-defined networks

$ docker run --net=isolated_nw -itd --name=container4 --link container5:c5 busybox
01b5df970834b77a9eadbaff39051f237957bd35c4c56f11193e0594cfd5117c

With the help of --link, container4 will be able to reach container5 using the aliased name c5 as well.

Please note that while creating container4, we linked to a container named container5 which is not created yet.
That is one of the differences in behavior between the legacy link in default bridge network and the new link functionality in user defined networks.

- The legacy link is static in nature and it hard-binds the container with the alias and it doesn't tolerate linked container restarts.
- While the new link functionality in user defined networks are dynamic in nature and supports linked container restarts including tolerating ip-address changes on the linked container.


You will have in the new docker 1.10 have two choices:

  • either have a fixed NGiNX config making reverse-proxy to a fixed list of potentially not created yet containers c1, c2, c3... Each time a new container is created, you relaunch your NGiNX container with the appropriate link: --link c2:myNewContainer2

  • Or your NGiNX container main process actually monitors the user defined network, and for each new container detected, regenerate the NGiNX conf, and gracefully restart the NGiNX daemon.

这篇关于泊坞窗1.9:添加--link目标运行Nginx的容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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