如何设置Docker容器之间的链接,以便重启不会中断它? [英] How do I set up linkage between Docker containers so that restarting won't break it?

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

问题描述

我有一些Docker容器运行如下:

I have a few Docker containers running like:


  • Nginx

  • Web应用程序1

  • Web应用程序2

  • PostgreSQL

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

自Nginx起需要连接到Web应用程序1和2中的Web应用程序服务器,并且Web应用程序需要与PostgreSQL通讯,我有这样的链接:

Since Nginx needs to connect to the web application servers inside web app 1 and 2, and the web apps need to talk to PostgreSQL, I have linkages like this:


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

  • Nginx ---链接---> Web应用程序2

  • Web应用程序1 ---链接---> PostgreSQL

  • Web应用程序2 ---链接---> PostgreSQL

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

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

This works pretty well at first. However, when I develop a new version of web app 1 and web app 2, I need to replace them. What I do is remove the web app containers, set up new containers and start them.

对于Web应用程序容器,它们的IP地址最初是这样的:

For the web app containers, their IP addresses at first would be something like:


  • 172.17.0.2

  • 172.17.0.3

在我替换它们之后,它们将具有新的IP地址:

And after I replace them, they will have new IP addresses:


  • 172.17.0.5

  • 172.17.0.6

现在,Nginx容器中那些暴露的环境变量仍指向旧的IP地址。问题来了。如何更换容器而不破坏容器之间的链接? PostgreSQL也将发生相同的问题。如果要升级PostgreSQL映像版本,我当然需要删除它并运行新的映像,但是然后我需要重建整个容器图,因此对于实际的服务器操作而言,这不是理想的选择。

Now, those exposed environment variables in the Nginx container are still pointing to the old IP addresses. Here comes the problem. How do I replace a container without breaking linkage between 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, so this is not ideal for real-life server operation.

推荐答案

-link 的效果是静态的,因此不适用于您的方案(尽管您可以删除链接,但目前没有重新链接)。

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).

我们在dockerize.it上使用了两种不同的方法来解决此问题,没有链接或大使(尽管您也可以添加大使)。

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

1)使用动态DNS

通常的想法是为数据库指定一个名称(或其他任何服务),并在启动和停止容器时使用实际IP更新短暂的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.

我们从 SkyDock 。它与两个docker容器,DNS服务器和一个可自动更新的监视器配合使用。后来,我们使用领事(也使用了dockerized版本: docker-consul )。

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).

此功能的改进(我们尚未t尝试)将设置etcd或类似的东西,并使用其自定义API来学习IP和端口。该软件也应该支持动态重新配置。

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)使用docker bridge ip

暴露容器端口时,您可以将它们绑定到 docker0 网桥,该网桥具有(或可以具有)众所周知的地址。

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

用新版本替换容器时,只需使新容器在相同IP上发布相同端口即可。

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

这比较简单,但也更有限。如果您运行类似的软件,您可能会遇到端口冲突(例如,两个容器无法在 docker0 桥上的3306端口上侦听),等等……所以我们目前最喜欢的选项是1。

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天全站免登陆