Cronjobs im Docker Container


Warning: preg_match(): Compilation failed: invalid range in character class at offset 4 in /home/users/stefanwarnat/www/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 1384

Warning: preg_match(): Compilation failed: invalid range in character class at offset 4 in /home/users/stefanwarnat/www/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 1384

Warning: preg_match(): Compilation failed: invalid range in character class at offset 4 in /home/users/stefanwarnat/www/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 1384

Warning: preg_match(): Compilation failed: invalid range in character class at offset 4 in /home/users/stefanwarnat/www/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 1384

Warning: preg_match(): Compilation failed: invalid range in character class at offset 4 in /home/users/stefanwarnat/www/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 1384

Warning: preg_match(): Compilation failed: invalid range in character class at offset 4 in /home/users/stefanwarnat/www/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 1384

Warning: preg_match(): Compilation failed: invalid range in character class at offset 4 in /home/users/stefanwarnat/www/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 1384

Warning: preg_match(): Compilation failed: invalid range in character class at offset 4 in /home/users/stefanwarnat/www/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 1384

Warning: preg_match(): Compilation failed: invalid range in character class at offset 4 in /home/users/stefanwarnat/www/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 1384

Warning: preg_match(): Compilation failed: invalid range in character class at offset 4 in /home/users/stefanwarnat/www/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 1384

Warning: preg_match(): Compilation failed: invalid range in character class at offset 4 in /home/users/stefanwarnat/www/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 1384

Warning: preg_match(): Compilation failed: invalid range in character class at offset 4 in /home/users/stefanwarnat/www/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 1384

Warning: preg_match(): Compilation failed: invalid range in character class at offset 4 in /home/users/stefanwarnat/www/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 1384

Warning: preg_match(): Compilation failed: invalid range in character class at offset 4 in /home/users/stefanwarnat/www/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 1384

Warning: preg_match(): Compilation failed: invalid range in character class at offset 4 in /home/users/stefanwarnat/www/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 1384

Warning: preg_match(): Compilation failed: invalid range in character class at offset 4 in /home/users/stefanwarnat/www/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 1384

Warning: preg_match(): Compilation failed: invalid range in character class at offset 4 in /home/users/stefanwarnat/www/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 1384

Warning: preg_match(): Compilation failed: invalid range in character class at offset 4 in /home/users/stefanwarnat/www/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 1384

Warning: preg_match(): Compilation failed: invalid range in character class at offset 4 in /home/users/stefanwarnat/www/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 1384

Warning: preg_match(): Compilation failed: invalid range in character class at offset 4 in /home/users/stefanwarnat/www/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 1384

Warning: preg_match(): Compilation failed: invalid range in character class at offset 4 in /home/users/stefanwarnat/www/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 1384

Warning: preg_match(): Compilation failed: invalid range in character class at offset 4 in /home/users/stefanwarnat/www/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 1384

Warning: preg_match(): Compilation failed: invalid range in character class at offset 4 in /home/users/stefanwarnat/www/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 1384

Warning: preg_match(): Compilation failed: invalid range in character class at offset 4 in /home/users/stefanwarnat/www/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 1384

Warning: preg_match(): Compilation failed: invalid range in character class at offset 4 in /home/users/stefanwarnat/www/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 1384

Warning: preg_match(): Compilation failed: invalid range in character class at offset 4 in /home/users/stefanwarnat/www/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 1384

Warning: preg_match(): Compilation failed: invalid range in character class at offset 4 in /home/users/stefanwarnat/www/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 1384

Warning: preg_match(): Compilation failed: invalid range in character class at offset 4 in /home/users/stefanwarnat/www/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 1384

Warning: preg_match(): Compilation failed: invalid range in character class at offset 4 in /home/users/stefanwarnat/www/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 1384

Warning: preg_match(): Compilation failed: invalid range in character class at offset 4 in /home/users/stefanwarnat/www/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 1384

Warning: preg_match(): Compilation failed: invalid range in character class at offset 4 in /home/users/stefanwarnat/www/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 1384

Warning: preg_match(): Compilation failed: invalid range in character class at offset 4 in /home/users/stefanwarnat/www/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 1384

Warning: preg_match(): Compilation failed: invalid range in character class at offset 4 in /home/users/stefanwarnat/www/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 1384

Warning: preg_match(): Compilation failed: invalid range in character class at offset 4 in /home/users/stefanwarnat/www/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 1384

Warning: preg_match(): Compilation failed: invalid range in character class at offset 4 in /home/users/stefanwarnat/www/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 1384

Warning: preg_match(): Compilation failed: invalid range in character class at offset 4 in /home/users/stefanwarnat/www/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 1384

Warning: preg_match(): Compilation failed: invalid range in character class at offset 4 in /home/users/stefanwarnat/www/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 1384

Warning: preg_match(): Compilation failed: invalid range in character class at offset 4 in /home/users/stefanwarnat/www/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 1384

Warning: preg_match(): Compilation failed: invalid range in character class at offset 4 in /home/users/stefanwarnat/www/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 1384

Warning: preg_match(): Compilation failed: invalid range in character class at offset 4 in /home/users/stefanwarnat/www/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 1384

Warning: preg_match(): Compilation failed: invalid range in character class at offset 4 in /home/users/stefanwarnat/www/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 1384

Warning: preg_match(): Compilation failed: invalid range in character class at offset 4 in /home/users/stefanwarnat/www/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 1384

Warning: preg_match(): Compilation failed: invalid range in character class at offset 4 in /home/users/stefanwarnat/www/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 1384

Warning: preg_match(): Compilation failed: invalid range in character class at offset 4 in /home/users/stefanwarnat/www/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 1384

Warning: preg_match_all(): Compilation failed: invalid range in character class at offset 4 in /home/users/stefanwarnat/www/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 700

Warning: foreach() argument must be of type array|object, null given in /home/users/stefanwarnat/www/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 707

Warning: preg_match_all(): Compilation failed: invalid range in character class at offset 4 in /home/users/stefanwarnat/www/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 700

Warning: foreach() argument must be of type array|object, null given in /home/users/stefanwarnat/www/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 707

Der folgende Post ist für mich als Notiz gedacht, da ich die erwähnten Links und Inhalte bei jedem Einsatz brauche und suche.

Es ist nicht die offizielle Empfehlung diesen Weg zu gehen, aber um Software als „simple to use“ Docker Image an Benutzer auszuliefern, ist es doch sehr hilfreich: Cronjobs vorkonfiguriert innerhalb des Docker Containers laufen zu lassen.

Zuerst dachte ich: Das ist schnell gemacht, da crontab unter Unix basierten Images schnell eingebunden und konfiguriert ist.

Sobald sich aber der Admin eines Server meldet und das Image gern rootless laufen lassen möchte, stellt man schnell fest, dass die originale crontab Anwendung ohne umständliche Bastel-Konfiguration, zwingend einen root User benötigt.
Und sich auch sonst recht schlecht in das Container Zeitalter integriert.

Da ich auch einen Weg brauchte, automatische Aufgaben zur Laufzeit anzupassen und ich, um mich nicht weiter vom Cloud-Native Gedanken zu entfernen, nicht mit SIGUSR1 im Container beschäftigen wollte, habe ich mich auf die Suche nach einer Lösung begeben, welche mir grundsätzlich weiterhelfen kann.

Dabei fand ich yacron von Gustavo J. A. M. Carneiro.
Ein relativ simples aber mächtiges Tool, welches die folgenden Punkte meiner Anforderungen erfüllte:

  • Einfach ausführbare Datei, welche im Image hinterlegt werden kann
  • Aktualisiert seine crons selbstständig bei Anpassung
  • Läuft unter jedem unix user
  • Nutzt eine Konfiguration per Yaml
  • viele weitere coole Spielereien, welche ich aber nicht benötigt

Als Beispiel für die Konfiguration von yacron, mein Beispiel in einem Image für WordPress Hosting, worin ich nun den WordPress Cron direkt per CLI ausführe:

defaults:
    environment:
      - key: PATH
        value: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
    shell: /bin/sh
    utc: false

jobs:
  - name: WP-Cron
    command: cd /var/www/html/; wp cron event run --due-now
    schedule:
      minute: "*/10"
      dayOfMonth: "*"
      month: "*"
      year: "*"
      dayOfWeek: "*"
    captureStdout: trueCode-Sprache: YAML (yaml)

Als Prozessmanager im Image nutze ich Supervisor und folgende Konfiguration von yacron:

[supervisord]
nodaemon=true
logfile=/dev/null
logfile_maxbytes=0
pidfile=/run/supervisord.pid

[program:php-fpm]
command=php-fpm -F
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
autorestart=false
startretries=0

[program:nginx]
command=nginx -g 'daemon off;'
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
autorestart=false
startretries=0

[program:yacron]
# We wait 30 seconds after check to allow 
command=/scripts/yacron -c /var/www/crons/cronjobs.yaml
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
autorestart=true
startretries=30Code-Sprache: PHP (php)

Die binary von yacron lege ich einfach bereits beim Bauen in das Image:

https://github.com/swarnat/wordpress-docker-composer/tree/master/scripts

Die Dokumentation von yacron gibt noch viele weitere Beispiele, wie umfangreich dieses Tool genutzt werden kann.

Evtl. hilft das ganze auch anderen Architekten von Docker Images. Viel Spaß damit!

Schreibe einen Kommentar