Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Docker uses Supervisor to manage processes


May 22, 2021 Docker From entry to practice



The Docker container starts a single process at startup, such as a daemon service for ssh or apache. But we often need to turn on multiple services on a single machine, and there are many ways to do this, the simplest of which is to put multiple startup commands into a startup script, start the script directly at startup, and install process management tools.

This section uses the process management tool supervisor to manage multiple processes in the container. S upervisor allows you to better control, manage, and restart the processes we want to run. Here we show you how to use both the ssh and apache services.

Configuration

First create a Dockerfile, the content and sections are explained below.

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 softwares 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 profile for supervisord and copy the profile below the corresponding directory.

EXPOSE 22 80
CMD ["/usr/bin/supervisord"]

Here we map ports 22 and 80 to start the service using the executable path of supervisord.

Supervisor profile content

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

Profiles contain directories and processes, the first supervsord configures the software itself, and runs using the nodaemon parameter. T he second paragraph contains 2 services to control. Each paragraph contains a directory of the service and the command to start the service.

How to use it

Create a mirror.

$ 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 start the container we created. Use multiple -p to map multiple ports so that we can access both ssh and apache services.

You can use this method to create a base image that has only the ssh service, and then create a mirror that you can use to create a mirror-based image