本文介绍了我应该为我的 Web 应用程序使用单独的 Docker 容器吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是否需要为复杂的 Web 应用程序使用单独的 Docker 容器,还是可以将所有必需的服务放在一个容器中?谁能解释一下为什么我应该将我的应用程序分成多个容器(例如 php-fpm 容器、​​mysql 容器、​​mongo 容器),当我有能力在一个容器中安装和启动所有东西吗?

Do I need use separate Docker container for my complex web application or I can put all required services in one container? Could anyone explain me why I should divide my app to many containers (for example php-fpm container, mysql container, mongo container) when I have ability to install and launch all stuff in one container?

推荐答案

使用 Docker 时需要考虑的是它内部的工作方式.Docker 将您的 PID 1 替换为您在 Dockerfile 中的 CMD(和 ENTRYPOINT,稍微复杂一些)指令中指定的命令.PID 1 通常是您的 init 系统所在的位置(sysvinit、runit、systemd 等等).您的容器根据在那里启动的任何过程而生和死.当进程终止时,你的容器也就终止了.当您键入 docker logs myContainer 时,容器中该进程的 Stdout 和 stderr 是您在主机上得到的.顺便说一句,这就是为什么您需要跳过箍来启动服务并运行 cronjobs(通常由您的 init 系统完成的事情).这对于理解以某种方式做事的动机非常重要.

Something to think about when working with Docker is how it works inside. Docker replaces your PID 1 with the command you specify in the CMD (and ENTRYPOINT, which is slightly more complex) directive in your Dockerfile. PID 1 is normally where your init system lives (sysvinit, runit, systemd, whatever). Your container lives and dies by whatever process is started there. When the process dies, your container dies. Stdout and stderr for that process in the container is what you are given on the host machine when you type docker logs myContainer. Incidentally, this is why you need to jump through hoops to start services and run cronjobs (things normally done by your init system). This is very important in understanding the motivation for doing things a certain way.

现在,您可以为所欲为.关于执行此操作的正确"方法有很多意见,但您可以将所有这些都扔掉,做您想做的事.所以你可以弄清楚如何在一个容器中运行所有这些服务.但是既然您知道 docker 如何用您在 Dockerfile 中的 CMD(和 ENTRYPOINT)中指定的任何命令替换 PID 1,您可能认为尝试保留您的应用程序是明智的每个都在自己的容器中运行,并让它们通过 容器链接 相互协作.(更新——2017 年 4 月 27 日:容器链接已被弃用,取而代之的是常规的 ole 容器网络,它更健壮,其理念是您只需将单独的应用程序容器加入同一个网络,这样它们就可以相互通信).

Now, you can do whatever you want. There are many opinions about the "right" way to do this, but you can throw all that away and do what you want. So you COULD figure out how to run all of those services in one container. But now that you know how docker replaces PID 1 with whatever command you specify in CMD (and ENTRYPOINT) in your Dockerfiles, you might think it prudent to try and keep your apps running each in their own containers, and let them work with each other via container linking. (Update -- 27 April 2017: Container linking has been deprecated in favor of regular ole container networking, which is much more robust, the idea being that you simply join your separate application containers to the same network so they can talk to one another).

如果您需要一点帮助来决定,我可以根据我自己的经验告诉您,当您将应用程序分离到单独的容器中,然后将它们链接在一起时,它最终会更简洁、更易于维护.刚才我正在从 HHVM 构建 Wordpress 安装,并且我正在安装 Nginx 和 HHVM/php-fpm,在一个容器中安装 Wordpress,在另一个容器中安装 MariaDB.将来,这将使我几乎没有任何麻烦地直接在我的 MariaDB 数据前面插入替换的 Wordpress 安装.对每个应用进行容器化是值得的.祝你好运!

If you want a little help deciding, I can tell you from my own experience that it ends up being much cleaner and easier to maintain when you separate your apps into individual containers and then link them together. Just now I am building a Wordpress installation from HHVM, and I am installing Nginx and HHVM/php-fpm with the Wordpress installation in one container, and the MariaDB stuff in another container. In the future, this will let me drop in a replacement Wordpress installation directly in front of my MariaDB data with almost no hassle. It is worth it to containerize per app. Good luck!

这篇关于我应该为我的 Web 应用程序使用单独的 Docker 容器吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 06:05