April 15, 2016
Docker starts one process, only. So if you want to put an application in a docker container which requires more than one process, you need to manage the processes yourself as processes may exit or crash unexpectedly. Supervisord is a toll which will help in these events. Here's how to use it.
Supervisord is a client/server system which allows users to control processes on UNIX-like operating systems. Its a process, which allows to run other processes as child processes. For example if you need to start or stop a group of them. Also it restarts crashed or exited sub-processes, thus ensuring the specified child processes are running.
To set up supervisord a configuration file is needed:
[supervisord]
nodaemon=true
childlogdir=/var/log/supervisord/
[inet_http_server]
port = 9001
username = admin
password = pass
[include]
files = /etc/supervisor/conf.d/*.conf
To have supervisord to manage services, add them by putting config files into the specified /etc/supervisord/conf.d
directory.
If you wanted to start postgresql, put for example the following psql.conf
file into the directory:
[program:psql]
priority=80
command=/usr/lib/postgresql/9.4/bin/postgres -D /var/lib/postgresql/9.4/main -c config_file=/etc/postgresql/9.4/main/postgresql.conf
user=postgres
autostart=true
autorestart=true
startretries=3
stdout_logfile_backups=5
stderr_logfile_backups=5
To have supervisord manage all the required sub-processes in a docker container, you need to add it to your Dockerfile:
# Update and clean image
RUN apt-get update \
&& apt-get -y install supervisor curl vim \
&& apt-get -y clean \
&& rm -rf /var/lib/apt/lists/*
# create dir for app configs and logs
RUN mkdir -p /etc/supervisor/conf.d/ \
&& mkdir -p /var/log/supervisord/
# initial config file
COPY supervisord.conf /etc/supervisord.conf
# run with config file #and not as daemon "--nodaemon"
CMD ["supervisord", "--configuration", "/etc/supervisord.conf"]
Obviously, you also need to copy the .conf
files for your child processes.
Is is not possible to define rules regarding the start order, e.g. delay the start of a java application until the database was started.