如何设置docker容器之间的连接,以便重新启动不会破坏它? [英] How to setup linkage between docker containers so that restarting won't break it?

查看:121
本文介绍了如何设置docker容器之间的连接,以便重新启动不会破坏它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




  • Nginx

  • Web app 1

  • Web app 2

  • PostgreSQL



由于Nginx需要将Web应用程序服务器连接到Web应用程序1和2中,并且Web应用程序需要与postgresql通信,所以我有这样的链接




  • Nginx - - 链接---> Web应用1

  • Nginx --- link ---> Web app 2

  • Web app 1 ---链接---> PostgreSQL

  • Web应用程序2 --- link ---> PostgreSQL



这个起初很好,但是当我开发新版本的web app 1和web app 2时,我需要替换它们。我所做的是删除Web应用程序容器,设置新的容器并启动它们。



对于网络应用程序容器,其IP地址最初将类似于




  • 172.17.0.2

  • 172.17.0.3



更换后,新的IP地址现在




  • 172.17.0.5

  • 172.17.0.6



此时,Nginx容器中暴露的环境变量仍然指向旧的IP地址。这里出现的问题,如何更换容器而不断开其他容器之间的连接? PostgreSQL也会发生同样的问题,如果我想升级PostgreSQL映像版本,我当然需要删除它并运行新的,但是我需要重新构建整个容器图,这不是真正的一个好主意生活服务器操作。

解决方案

- link 的效果是静态的,所以它不会适用于你的方案(目前没有重新链接,虽然你可以删除链接)。



我们在dockerize.it中使用了两种不同的方法来解决这个问题,没有链接或大使(虽然你也可以添加大使)



1)使用动态DNS



一般的想法是,你为您的数据库(或任何其他服务)指定单个名称,并在启动和停止容器时更新具有实际IP的短命DNS服务器。



我们从<一个href =https://github.com/crosbymichael/skydock =noreferrer> SkyDock 。它与两个码头容器,DNS服务器和监视器一起保持自动更新。后来我们使用领事馆(也使用dockerized版本: docker-consul )。



这个演变(我们避开'尝试)将安装etcd或类似的,并使用其自定义API来学习IP和端口。软件应该支持动态重新配置。



2)使用docker bridge ip



当暴露容器端口时,您可以将它们绑定到具有(或可以拥有)一个公知地址的 docker0 桥。



当使用新版本替换容器时,只需使新容器在同一个IP上发布相同的端口。



这更简单,但也是更有限如果您运行类似的软件(例如,两个容器不能在 docker0 bridge)上的3306端口上侦听,则可能会出现端口冲突,因此我们当前最喜欢的选项1。


I have few dockers containers running like

  • Nginx
  • Web app 1
  • Web app 2
  • PostgreSQL

Since Nginx need to connects web application server inside web app 1 and 2, and web apps need to talk to postgresql, so I have linkages like this

  • Nginx --- link ---> Web app 1
  • Nginx --- link ---> Web app 2
  • Web app 1 --- link ---> PostgreSQL
  • Web app 2 --- link ---> PostgreSQL

This works pretty well at beginning, however, when I develop new version of web app 1 and web app 2, I need to replace them. What I do is to remove web app containers, setup new containers and start them.

For web app containers, their IP address at very first would be something like

  • 172.17.0.2
  • 172.17.0.3

And after I replace them, they have new IP addresses now

  • 172.17.0.5
  • 172.17.0.6

At this moment, those exposed environment variables in Nginx container are still pointed to old IP addresses. Here comes the problem, how to replace a container without breaking linkage between other containers? The same issue will also happen to PostgreSQL, if I want to upgrade the PostgreSQL image version, I certainly need to remove it and run the new one, but then I need to rebuild the whole container graph, this is not a good idea for real life server operation.

解决方案

The effect of --link is static, so it will not work for your scenario (there is currently no re-linking, although you can remove links).

We have been using two different approaches at dockerize.it to solve this, without links or ambassadors (although you could add ambassadors too).

1) Use dynamic DNS

The general idea is that you specify a single name for your database (or any other service) and update a short-lived DNS server with the actual IP as you start and stop containers.

We started with SkyDock. It works with two docker containers, the DNS server and a monitor that keeps it updated automatically. Later we moved to something more custom using Consul (also using a dockerized version: docker-consul).

An evolution of this (which we haven't tried) would be to setup etcd or similar and use its custom API to learn the IPs and ports. The software should support dynamic reconfiguration too.

2) Use the docker bridge ip

When exposing the container ports you can just bind them to the docker0 bridge, which has (or can have) a well known address.

When replacing a container with a new version, just make the new container publish the same port on the same IP.

This is simpler but also more limited. You might have port conflicts if you run similar software (for instance, two containers can not listen on the 3306 port on the docker0 bridge), etcétera… so our current favorite is option 1.

这篇关于如何设置docker容器之间的连接,以便重新启动不会破坏它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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