简体   繁体   English

Docker最佳实践:容器的单个进程

[英]Docker best practices: single process for a container

The Docker best practices guide states that: Docker 最佳实践指南指出:

"...you should only run a single process in a single container..." “......你应该只在一个容器中运行一个进程......”

Should Nginx and PHP-FPM run in separate containers? Nginx和PHP-FPM应该在不同的容器中运行吗? Or does that mean that micro service architectures only run one service or "app" in a container? 或者这是否意味着微服务架构只在容器中运行一个服务或“app”?

Having these services in a single container seems easier to deploy and maintain. 将这些服务放在单个容器中似乎更容易部署和维护。

Depending on the use case, you can run multiple processes inside a single container, although I won't recommend that. 根据用例,您可以在单个容器中运行多个进程,但我不建议这样做。

In some sense it is even simpler to run them in different containers. 从某种意义上说,在不同的容器中运行它们更简单。 Keeping containers small, stateless, and around a single job makes it easier to maintain them all. 保持容器小,无状态,并且只需一个工作就可以更容易地维护它们。 Let me tell you how my workflow with containers is in a similar situation. 让我告诉你我的容器工作流程是如何处于类似的情况。

So: 所以:

  1. I have one container with nginx that is exposed to the outside world (:443, :80). 我有一个容器,其中nginx暴露给外界(:443,:80)。 At this level it is straightforward to manage the configurations, tls certificates, load balancer options etc. 在此级别,可以直接管理配置,证书,负载均衡器选项等。
  2. One (or more) container(s) with the application. 一个(或多个)容器与应用程序。 In that case a php-fpm container with the app. 在这种情况下,一个php-fpm容器与应用程序。 Docker image is stateless, the containers mount and share the volumes for static files and so on. Docker镜像是无状态的,容器会挂载并共享静态文件的卷,依此类推。 At this point, you can at any time to destroy and re-create the application container, keeping the load-balancer up and running. 此时,您可以随时销毁并重新创建应用程序容器,从而保持负载均衡器的正常运行。 Also, you can have multiple applications behind the same proxy (nginx), and managing one of them would not affect the others. 此外,您可以在同一代理(nginx)后面拥有多个应用程序,并且管理其中一个应用程序不会影响其他应用程序。
  3. One or more containers for the database... Same benefits apply. 数据库的一个或多个容器......同样的好处适用。
  4. Redis, Memcache etc. Redis,Memcache等

Having this structure, the deployment is modular, so each and every "service" is separated and logically independent from the rest of the system. 具有这种结构,部署是模块化的,因此每个“服务”都是分离的,并且在逻辑上独立于系统的其余部分。

As a side effect, in this particular case, you can do zero-downtime deployments (updates) to the application. 作为副作用,在此特定情况下,您可以对应用程序执行零停机部署 (更新)。 The idea behind this is simple. 这背后的想法很简单。 When you have to do an update, you create a docker image with the updated application, run the container, run all the tests and maintenance scripts and if everything goes well, you add the newly created container to the chain (load balancer), and softly kill the old one. 当您必须进行更新时,使用更新的应用程序创建docker镜像,运行容器,运行所有测试和维护脚本,如果一切顺利,则将新创建的容器添加到链(负载均衡器),并且轻轻地杀死旧的。 That's it, you have the updated application and users didn't even notice it at all. 就是这样,你有更新的应用程序,用户根本没有注意到它。

This means process in the Linux/Unix sense of the word. 这意味着在Linux / Unix意义上的过程。 That said, there's nothing stopping you from running multiple processes in a container, it's just not a recommended paradigm. 也就是说,没有什么能阻止你在容器中运行多个进程,这不是推荐的范例。

We have found that we can run multiple services using Supervisord . 我们发现我们可以使用Supervisord运行多个服务。 It makes the architecture pretty easy, requiring only that you have an additional supervisor.conf file. 它使架构变得非常简单,只需要你有一个额外的supervisor.conf文件。 For instance: 例如:

supervisord.conf supervisord.conf

[supervisord]
nodaemon=true

[program:apache2]
command=/bin/bash -c "source /etc/apache2/envvars && exec /usr/sbin/apache2 -DFOREGROUND"

[program:udpparser]
command=bin/bash -c "exec /usr/bin/php -f /home/www-server/services/udp_parser.php"

From Dockerfile: 来自Dockerfile:

FROM ubuntu:14.04

RUN apt-get update
RUN apt-get install -y apache2 supervisor php5 php5-mysql php5-cli

RUN mkdir -p /var/lock/apache2 /var/run/apache2 /var/log/supervisor

RUN a2enmod rewrite
RUN a2enmod ssl

COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf

ADD 000-default.conf /etc/apache2/sites-enabled/
ADD default-ssl.conf /etc/apache2/sites-enabled/
ADD apache2.conf /etc/apache2/
ADD www-server/ /home/www-server/

EXPOSE 80 443 30089

CMD ["/usr/bin/supervisord"]

As a best practice we only do this in cases where the services benefit from running together while all other containers are stand-alone micro-services. 作为最佳实践,我们只在服务受益于一起运行而所有其他容器都是独立的微服务的情况下才这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM