Example of a Supervisor process running on the host that is hosting the container:
[Unit]
Description=BillRun Worker (Docker)
After=docker.service
Requires=docker.service
[Service]
# Install pkill (procps) if missing
ExecStartPre=/usr/bin/docker exec billrun-app sh -c "command -v pkill >/dev/null 2>&1 || (apt-get update && apt-get install -y procps)"
# Start the worker
ExecStart=/usr/bin/docker exec billrun-app php /billrun/public/index.php --env container --worker
# Stop the worker cleanly
ExecStop=/usr/bin/docker exec --user root billrun-app pkill -9 -f 'php /billrun/public/index.php --env container --worker'
Restart=always
RestartSec=10
User=billrun
WorkingDirectory=/home/billrun
StandardOutput=null
StandardError=null
[Install]
WantedBy=multi-user.target
Then enable and run the supervisor:
sudo systemctl daemon-reload
sudo systemctl enable billrun-worker
sudo systemctl start billrun-worker
Create file: /etc/supervisor/conf.d/php-worker.conf
[program:billrun_worker]
command=php /billrun/index.php --env container --worker
autostart=true
autorestart=true
startretries=3
user=www-data
apt update && apt install -y supervisor
Create shell script to run on container load:
mkdir -p /var/log/supervisor
echo_supervisord_conf > /etc/supervisor/supervisord.conf
echo "[include]" >> /etc/supervisor/supervisord.conf
echo "files = /etc/supervisor/conf.d/*.conf" >> /etc/supervisor/supervisord.conf
CMD ["supervisord", "-n", "-c", "/etc/supervisor/supervisord.conf"]