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

问题描述

我需要为复杂的Web应用程序使用单独的Docker容器,或者我可以将所有必需的服务放在一个容器中?
任何人都可以解释为什么我应该将我的应用程序分成许多容器(例如 php-fpm container, mysql container, mongo container)

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使用您在 CMD (和 ENTRYPOINT 中指定的命令)替换PID 1,该命令稍微复杂一些)在你的Docker文件。 PID 1通常是您的init系统生活(sysvinit,runit,systemd,any)。你的容器生活和死亡的任何过程在那里开始。当过程死亡时,您的容器死亡。当您输入 docker log 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如何用你在Dockerfiles中的 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的安装,并且我将WordPress安装的Nginx和HHVM / php-fpm安装在一个容器中,而将MariaDB的东西放在另一个容器中。将来,这将让我放弃一个替换的Wordpress安装,直接在我的MariaDB数据前面几乎没有麻烦。它是值得的,以容纳每个应用程序。祝你好运!

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!

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