Docker uses Supervisor to manage process operations
- 2021-09-20 21:56:27
- OfStack
The Docker container starts a single process at startup time, such as an ssh or an daemon service for apache.
However, we often need to start multiple services on one machine. There are many ways to do this. The simplest way is to put multiple startup commands into one startup script, start this script directly when starting, and install process management tools.
This section uses the process management tool supervisor to manage multiple processes in the container. Using Supervisor can better control, manage and restart the processes we want to run. Here we demonstrate how to use ssh and apache services at the same time under 1.
Configure
First, create an Dockerfile. The contents and explanations of each part are as follows.
FROM ubuntu:13.04
MAINTAINER examples@docker.com
RUN echo "deb http://archive.ubuntu.com/ubuntu precise main universe" > /etc/apt/sources.list
RUN apt-get update
RUN apt-get upgrade -y
Install ssh, apache, and supervisor
RUN apt-get install -y openssh-server apache2 supervisor
RUN mkdir -p /var/run/sshd
RUN mkdir -p /var/log/supervisor
Three pieces of software are installed here, and two directories are created for the ssh and supervisor services to function properly.
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
Add the configuration file of supervisord and copy the configuration file to the corresponding directory.
EXPOSE 22 80
CMD ["/usr/bin/supervisord"]
Here we mapped ports 22 and 80 to start the service using the executable path of supervisord.
supervisor Configuration File Contents
[supervisord]
nodaemon=true
[program:sshd]
command=/usr/sbin/sshd -D
[program:apache2]
command=/bin/bash -c "source /etc/apache2/envvars && exec /usr/sbin/apache2 -DFOREGROUND"
Configuration files contain directories and processes, paragraph 1 supervsord configuration software itself, using nodaemon parameters to run. Paragraph 2 contains two services to be controlled. Each 1 segment contains a directory of 1 service and a command to start this service.
Usage
Create a mirror image.
$ sudo docker build -t test/supervisord .
Start the supervisor container.
$ sudo docker run -p 22 -p 80 -t -i test/supervisords
2013-11-25 18:53:22,312 CRIT Supervisor running as root (no user in config file)
2013-11-25 18:53:22,312 WARN Included extra file "/etc/supervisor/conf.d/supervisord.conf" during parsing
2013-11-25 18:53:22,342 INFO supervisord started with pid 1
2013-11-25 18:53:23,346 INFO spawned: 'sshd' with pid 6
2013-11-25 18:53:23,349 INFO spawned: 'apache2' with pid 7
Use docker run to start the container we created. Use multiple-p to map multiple ports, so that we can access both ssh and apache services at the same time.
You can use this method to create a base image with only ssh services, and then create the image based on this image.