I'm Dockerizing ssh-key-authority and made an Apache2 container that serves the PHP application. To sync users, the script scripts/ldap_update.php
should run periodically every xx min. So I created a second container in my docker-compose.yml
file that reuses the Apache image containing phps binary:
version: '2'
services:
ska:
image: ska
build:
context: .
args:
# Allow fetching packages from the internet in corporate environments
http_proxy: ${http_proxy}
env_file: docker.env
ports:
- 80:80
ska-db:
image: mariadb:10.5
env_file: docker.env
cron:
image: ska
volumes_from:
- ska
env_file: docker.env
depends_on:
- ska-db
entrypoint: |
bash -c '
./prepare-docker-configs.sh
# Wait for DB connection
sleep 10
while true
do
echo Running update task
php /ska/scripts/ldap_update.php
sleep 10
done'
10s are for testing only. Productive I'd increase this to lets say 1800 (30 min). While this works, it has a problem: Docker couldn't stop this while true
loop, e.g. when executing docker-compose down
. As result, Docker waits 10s and then kill the container process.
This causes delays during development and it seems not to be a clean way for handling this. I found trap, which worked well outside Docker using this script:
#!/bin/bash
trap 'kill -TERM -s SIGKILL $PID' SIGINT SIGTERM
PID=$$
echo pid: $PID
while true
do
echo sleep run
sleep 10
done
Result:
$ ./trap.sh
pid: 26135
sleep run
^CKilled
But when trying this in the ENTRYPOINT
(even with exec
), the container just exited:
entrypoint: |
bash -c '
./prepare-docker-configs.sh
set -x
PID=$$
exec "trap 'kill -TERM -s SIGKILL $PID' SIGINT SIGTERM"
echo "pid is $$"
while true
do
sleep 10
echo sync ldap users
php /ska/scripts/ldap_update.php
done'
The only alternative I see is installing cron
and running it in the foreground, but that seems a bit overkill to me.