How to run multiple processes in a single docker c

2019-08-07 17:02发布

问题:

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?

回答1:

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.

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.

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.

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"]

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/

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

[supervisord]                                                                                                                                                                 
nodaemon=true

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