From 4e5f940fcc780f08c5025b779c94b0c4d416f48b Mon Sep 17 00:00:00 2001 From: Jason Hildebrand Date: Thu, 11 Jan 2024 14:45:58 -0600 Subject: [PATCH 1/2] Implement persistent/reliable cron service using supervisor. --- compose/bin/cli | 2 +- compose/bin/clinotty | 2 +- compose/bin/cron | 4 ++-- compose/compose.yaml | 1 + compose/env/phpfpm.env | 1 + images/php/8.2/Dockerfile | 10 ++++++++-- images/php/8.2/conf/php-fpm.conf | 5 ++--- images/php/8.2/conf/supervisord.conf | 26 ++++++++++++++++++++++++++ 8 files changed, 42 insertions(+), 9 deletions(-) create mode 100644 images/php/8.2/conf/supervisord.conf diff --git a/compose/bin/cli b/compose/bin/cli index 9763aff9f..890777635 100755 --- a/compose/bin/cli +++ b/compose/bin/cli @@ -1,3 +1,3 @@ #!/usr/bin/env bash [ -z "$1" ] && echo "Please specify a CLI command (ex. ls)" && exit -bin/docker-compose exec phpfpm "$@" +bin/docker-compose exec -u app phpfpm "$@" diff --git a/compose/bin/clinotty b/compose/bin/clinotty index fbb965d53..12fdff839 100755 --- a/compose/bin/clinotty +++ b/compose/bin/clinotty @@ -1,4 +1,4 @@ #!/usr/bin/env bash [ -z "$1" ] && echo "Please specify a CLI command (ex. ls)" && exit # -T: Disable pseudo-tty allocation -bin/docker-compose exec -T phpfpm "$@" +bin/docker-compose exec -u app -T phpfpm "$@" diff --git a/compose/bin/cron b/compose/bin/cron index 70f5689e2..ac08a2525 100755 --- a/compose/bin/cron +++ b/compose/bin/cron @@ -1,5 +1,5 @@ #!/usr/bin/env bash -[ -z "$1" ] && echo "Please specify a service operation (start|stop|status|restart|reload|force-reload)" && exit -bin/root service cron "$@" +[ -z "$1" ] && echo "Please specify a service operation (start|stop|status|restart)" && exit +bin/root supervisorctl "$@" cron bin/root touch /var/www/html/var/.setup_cronjob_status /var/www/html/var/.update_cronjob_status bin/root chown app:app /var/www/html/var/.setup_cronjob_status /var/www/html/var/.update_cronjob_status diff --git a/compose/compose.yaml b/compose/compose.yaml index 2ed3528ad..7618597c9 100644 --- a/compose/compose.yaml +++ b/compose/compose.yaml @@ -34,6 +34,7 @@ services: phpfpm: image: markoshust/magento-php:8.2-fpm-0 volumes: *appvolumes + tty: true env_file: env/phpfpm.env #extra_hosts: *appextrahosts diff --git a/compose/env/phpfpm.env b/compose/env/phpfpm.env index 8d8d626d7..fc95e5eb0 100644 --- a/compose/env/phpfpm.env +++ b/compose/env/phpfpm.env @@ -3,3 +3,4 @@ BLACKFIRE_CLIENT_TOKEN= COMPOSER_DISABLE_XDEBUG_WARN=1 PHP_CS_FIXER_IGNORE_ENV=1 PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true +START_CRON=true diff --git a/images/php/8.2/Dockerfile b/images/php/8.2/Dockerfile index f87faac79..4915e06be 100644 --- a/images/php/8.2/Dockerfile +++ b/images/php/8.2/Dockerfile @@ -3,7 +3,7 @@ MAINTAINER Mark Shust ARG APP_ID=1000 RUN groupadd -g "$APP_ID" app \ - && useradd -g "$APP_ID" -u "$APP_ID" -d /var/www -s /bin/bash app + && useradd -g "$APP_ID" -G tty -u "$APP_ID" -d /var/www -s /bin/bash app RUN mkdir -p /etc/nginx/html /var/www/html /sock \ && chown -R app:app /etc/nginx /var/www /usr/local/etc/php/conf.d /sock @@ -34,8 +34,10 @@ RUN apt-get update && apt-get install -y \ msmtp \ nodejs \ procps \ + tini \ vim \ zip \ + supervisor \ && rm -rf /var/lib/apt/lists/* RUN pecl channel-update pecl.php.net && pecl install \ @@ -91,7 +93,11 @@ COPY conf/msmtprc /etc/msmtprc COPY conf/php.ini $PHP_INI_DIR COPY conf/php-fpm.conf /usr/local/etc/ COPY conf/www.conf /usr/local/etc/php-fpm.d/ +COPY conf/supervisord.conf /etc/supervisor/conf.d/supervisord.conf +# using tini as PID 1 instead of supervisor eliminates the scary but harmless "reaped unknown pid XX" message each time cron runs +ENTRYPOINT ["/usr/bin/tini", "--"] +CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"] + -USER app:app VOLUME /var/www WORKDIR /var/www/html diff --git a/images/php/8.2/conf/php-fpm.conf b/images/php/8.2/conf/php-fpm.conf index 5e875011b..ce4a45122 100644 --- a/images/php/8.2/conf/php-fpm.conf +++ b/images/php/8.2/conf/php-fpm.conf @@ -3,13 +3,12 @@ [global] -error_log = /proc/self/fd/2 +error_log = /dev/console daemonize = no [www] -; if we send this to /proc/self/fd/1, it never appears -access.log = /proc/self/fd/2 +access.log = /dev/console ;user = app ;group = app diff --git a/images/php/8.2/conf/supervisord.conf b/images/php/8.2/conf/supervisord.conf new file mode 100644 index 000000000..ca334e9a8 --- /dev/null +++ b/images/php/8.2/conf/supervisord.conf @@ -0,0 +1,26 @@ +[supervisord] +nodaemon=true +user=root + +[unix_http_server] +file=/var/run/supervisor.sock +chmod=0700 + +[supervisorctl] +serverurl=unix:///var/run/supervisor.sock + +[rpcinterface:supervisor] +supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface + +[program:php] +command=docker-php-entrypoint /usr/local/sbin/php-fpm +user=app + +[program:cron] +command=bash -c 'su - app bash -c "/usr/local/bin/php /var/www/html/bin/magento cron:install"; exec /usr/sbin/cron -f' +autostart=%(ENV_START_CRON)s +stdout_logfile=/dev/console +stdout_logfile_maxbytes=0 +stderr_logfile=/dev/console +stderr_logfile_maxbytes=0 + From d02b17114e6c96e8eb4c88fc8d324639fed0f1e8 Mon Sep 17 00:00:00 2001 From: Jason Hildebrand Date: Thu, 11 Jan 2024 15:13:58 -0600 Subject: [PATCH 2/2] Adjust location of supervisord log and pid files. --- images/php/8.2/conf/supervisord.conf | 2 ++ 1 file changed, 2 insertions(+) diff --git a/images/php/8.2/conf/supervisord.conf b/images/php/8.2/conf/supervisord.conf index ca334e9a8..f8a0c0de7 100644 --- a/images/php/8.2/conf/supervisord.conf +++ b/images/php/8.2/conf/supervisord.conf @@ -1,6 +1,8 @@ [supervisord] nodaemon=true user=root +pidfile=%(here)s/supervisord.pid +logfile=/var/www/html/var/log/supervisord.log [unix_http_server] file=/var/run/supervisor.sock