如何在单个Docker容器中运行多个进程 [英] How to run multiple processes in a single docker container

查看:292
本文介绍了如何在单个Docker容器中运行多个进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以生成一个包含许多应用程序的docker映像?或至少一个包含一组图像的图像?

Is it possible to generate one docker image that will contain many applications? or at least one image that a group many images?

我的情况是,在我们公司中,我们有许多应用程序,我们实际上知道我们需要将它们全部部署在同一服务器上,并且我们需要非常简单地进行部署,因此逐个映像进行部署不是什么我们想要,所以,我想知道如何将它们分组?只需一个命令就可以执行所有操作?

My situation is that in our company we have many applications that we know in fact that we will need to deploy them all in the same server, and we need to make very simple the deployment, so deploy image by image is not what we want, so, I am wondering, how can I group them? that only with one command everything should be executed?

推荐答案

Docker的最佳实践是为要在堆栈上运行的每个进程使用单独的容器(即,使用单独的Dockerfile创建的单独映像).

The best practice with Docker is to use a separate container (i.e. separate image created with separate Dockerfile) for each process that is going to be running on your stack.

所有这些容器都在同一台docker服务器上创建/部署,因此最终,整个堆栈都以容器化的方式在同一台服务器上运行.

All of those containers get created/deployed on the same docker server, so in the end the whole stack is running in a containerized fashion on the same server.

但是,如果要将多个应用程序添加到同一个容器中,可以通过添加所有安装,配置,启动这些应用程序的命令到单个Dockerfile中,然后在容器位于容器中时使用Supervisor来启动应用程序来做到这一点.已创建.

However, if you want to add multiple applications into the same container, you can do that by adding all the commands to install, configure, start those apps into a single Dockerfile, and using Supervisor to start the applications when the container is created.

以下是我通常使用的示例Dockerfile内容:

Here is a sample Dockerfile content for what I normally use:

# Inherit ubuntu base image
FROM ubuntu:latest

# Update linux package repo and install desired dependencies
RUN apt-get update -y
RUN apt-get -y install nginx git supervisor etc... (install multiple apps here)

# Create new linux group and user to own apps
RUN groupadd --system apps
RUN useradd --system --gid apps --shell /bin/bash --home /apps

# Create directory for app logs
RUN mkdir -p /apps/logs

# RUN any other configuration or dependency setup commands for apps
RUN ...

# Copy in any static dependencies
COPY xxx /apps/...

# Copy in supervisor configuration files
COPY ./supervisord/conf.d/* /etc/supervisor/conf.d/

# Open connectivity on container port X to the docker host
EXPOSE X

# Create empty log files
RUN touch /apps/logs/xxxx.log

# Set app directory owners and permissions
RUN chown -R apps:apps /apps
RUN chmod -R u+rwx apps
RUN chmod -R g+rx apps
RUN chmod -R o+rx apps

# Run supervisor configuration file on container startup
CMD ["supervisord", "-n"]

这将在创建容器时启动在Supervisor配置文件中定义的所有应用程序.从上面的脚本中注意到,在与Dockerfile相同的目录中,您具有Supervisor配置的静态目录结构,即,具有以下文件夹结构: ./supervisord/conf.d/

This will start all apps defined in the Supervisor configuration files on container creation. Notice from the script above that in the same directory as the Dockerfile, you have the static directory structure of the Supervisor configuration, i.e. you have folder structure like: ./supervisord/conf.d/

在该 conf.d 文件夹中,您需要一个名为 supervisord.conf 的主Supervisor配置文件,其中包含:

Inside that conf.d folder you need the main Supervisor configuration file called supervisord.conf that contains:

[supervisord]                                                                                                                                                                 
nodaemon=true

在同一 conf.d 文件夹中,每个要运行的应用程序都有1个配置文件,称为 app_name.conf .

In the same conf.d folder you will have 1 config file for each app you want to run, called app_name.conf.

[program:appname]                                                                                                                                                               
command = /usr/sbin/command_name -flags "keywords"
autostart = true                                                      ; Start app automatically
stdout_logfile = /apps/logs/xxxx.log     ; Where to write log messages
redirect_stderr = true                                                ; Save stderr in the same log 
username = apps                                                 ; Specify user to run nginx
autorestart = true                                                    ; Restart app automatically

这篇关于如何在单个Docker容器中运行多个进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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