如何在单个docker容器中运行多个进程

如何在单个docker容器中运行多个进程,docker,dockerhub,docker-image,Docker,Dockerhub,Docker Image,是否可以生成一个包含多个应用程序的docker映像?或者至少有一个图像包含多个图像 我的情况是,在我们公司,我们有许多应用程序,我们知道实际上我们需要将它们部署在同一台服务器上,而且我们需要非常简单的部署,所以逐映像部署不是我们想要的,所以,我想知道,如何对它们进行分组?只有一个命令才能执行所有操作?Docker的最佳实践是为堆栈上要运行的每个进程使用单独的容器(即使用单独的Dockerfile创建的单独映像) 所有这些容器都在同一个docker服务器上创建/部署,因此最终整个堆栈都以容器化方式

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


我的情况是,在我们公司,我们有许多应用程序,我们知道实际上我们需要将它们部署在同一台服务器上,而且我们需要非常简单的部署,所以逐映像部署不是我们想要的,所以,我想知道,如何对它们进行分组?只有一个命令才能执行所有操作?

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

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

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

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

# 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"]
这将在创建容器时启动监控器配置文件中定义的所有应用程序。从上面的脚本中可以注意到,在与Dockerfile相同的目录中,您具有Supervisor配置的静态目录结构,即,您具有如下文件夹结构:
/Supervisor/conf.d/

在该conf.d文件夹中,您需要名为supervisord.conf的主管理器配置文件,该文件包含:

[supervisord]                                                                                                                                                                 
nodaemon=true
在同一个conf.d文件夹中,每个要运行的应用程序都有一个配置文件,名为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容器中运行多个进程?”只是为了澄清一下,在一个容器中运行多个进程并不是必须的。通过使用docker init,我们可以确保不会留下僵尸进程。