Multiple services in a Docker container with supervisord

Multiple services in a Docker container with supervisord

Docker is mostly used for packing the application, the goal is generally that anyone could execute

docker run <containername>

so how does docker know what to execute? It uses ENTRYPOINT or CMD to start services, Both parameters are used to specify the programs/commands to execute while initializing a container from the docker image.

The container's primary process is responsible for managing all operations that it starts which we define at ENTRYPOINT or CMD at the end of the Dockerfile. Docker always recommends that your areas of concern be by using one service per container.

Supervisor A process control system

A supervisor is a client/server system that allows its users to monitor and control many processes on UNIX-like operating systems.

It is meant to be used to control processes related to a project or a customer and to start like any other program at boot time.

I have detail explained about supervisord in this article.

Multiple services in a container

  1. Dockerfile CMD

    CMD ["supervisord", "-c", "/path/to_your_file/supervisor.conf", "-n"]
    
  2. Supervisor conf file sample

[unix_http_server]
file=/tmp/supervisor.sock
chmod=0755               

[supervisord]
logfile=/tmp/supervisord.log
logfile_maxbytes=50MB        
logfile_backups=10           
loglevel=info               
pidfile=/tmp/supervisord.pid 
nodaemon=false               
minfds=1024                  
minprocs=200
;environment variables
environment=ROOT="/home/sv-admin",
            LD_LIBRARY_PATH="/home/sv-admin/libraries",
            ML_MODEL_LOC="/home/sv-admin/ml-models"

[inet_http_server]         ; inet (TCP) server disabled by default
port=127.0.0.1:9001        ; (ip_address:port specifier, *:port for all iface)

[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[supervisorctl]
serverurl=unix:///tmp/supervisor.sock 

[program:redis-server]
command=redis-server
process_name=%(program_name)s
numprocs=1
directory=/home/sv-admin
autostart=true
autorestart=true
startsecs=10
stopsignal=TERM
stopwaitsecs=5 ; max num secs to wait b4 SIGKILL (default 10)
user=root
priority=1
stopasgroup=true 
killasgroup=true

[program:frameprocessorlogger]
command=/home/sv-admin/python/scripts/frameprocessorlogger
process_name=%(program_name)s
numprocs=1
directory=/home/sv-admin/python/scripts
autostart=true
autorestart=true
startsecs=10
stopsignal=TERM
stopwaitsecs=5
user=root
priority=30
stopasgroup=true 
killasgroup=true

In the above config file, we have two services one is Redis-server and logger services running as root. Each service has priority to start services. for each config param refer to the supervised documentation

In the command section, You can also use a shell script to run your services but be aware of the zombie process created when you kill or restart the services.

Did you find this article valuable?

Support Pratap kute by becoming a sponsor. Any amount is appreciated!