Repository navigation
Conversation
This was referenced Nov 21, 2026
Kanti
changed the base branch from
bugfix/logs-to-container-log
to
bugfix/cron-job-output
November 23, 2026 08:18
Kanti
force-pushed
the
feature/cron-jobs-from-environment
branch
from
November 23, 2026 08:18
c25a8c9 to
2cc08a9
Compare
Kanti
force-pushed
the
feature/cron-jobs-from-environment
branch
from
November 23, 2026 08:23
2cc08a9 to
e405a9a
Compare
…ge build A cron job needed an image build: docker-cronjob only works in a RUN line, and compose.yaml had no way to add one. Mounting /etc/cron.d or files into /opt/docker/etc/cron works on Debian only; BusyBox crond on Alpine reads one crontab per user in /etc/crontabs (#280). Each variable CRONJOB_<NAME> now holds one job in the format of docker-cronjob: the time, the user and the command. At every start, the new entrypoint script 10-cronjobs.sh runs docker-cronjob --environment, which writes the jobs, sorted by name, in place of those of the last start: on Debian to /etc/cron.d/webdevops-environment, on Alpine into a marked block in the crontab of each user, so a restart does not add them twice and the jobs of docker-cronjob stay. On Debian, the file begins with SHELL=/usr/local/bin/docker-cronjob-shell, as webdevops-docker does, so the output of the jobs reaches the container log. Where cron is switched off, a container that starts supervisord switches it on for them. All values are checked first with the parser of #653, which now also checks the time fields against what both crons take: cron in Debian ignores a whole file in /etc/cron.d when one line has an error, so one bad variable would switch off all of them. A value that is not a job, or a user that does not exist, stops the entrypoint with an error that names the variable. docker-cronjob at build time on Alpine gets the same checks; on Debian it is unchanged. The docs present the variables as the way to add cron jobs, with docker-cronjob for jobs that belong in the image. New image tests in ContainerEnvironmentChecks run a job of a variable and find its output in the container log, restart a container with cron switched off, and start containers with invalid values. Closes #280
Cron in Debian passes none of the variables of the container on to a job: a job saw only HOME, LOGNAME, SHELL, PWD and PATH=/usr/bin:/bin, so php needed /usr/local/bin/ and a console command found no variable of compose.yaml. BusyBox crond on Alpine already passes on its own variables, which are those of the container. Each time cron starts, the new hook 20-job-environment.sh runs docker-cronjob --job-environment, which writes the environment of the start script of cron, NUL-separated, into /run/docker-cronjob/<user> for each user of a job in a crontab in /etc/cron.d with SHELL=/usr/local/bin/docker-cronjob-shell, readable by that user and root only. Running at the start of cron, it also covers jobs that scripts in /entrypoint.d or a restart of cron add. docker-cronjob-shell, which /etc/cron.d/webdevops-docker and webdevops-environment already name as their shell to send the output of the jobs to syslog-ng, now first adds the variables of the file of its user. The values arrive exactly, also with quotes, $, line breaks and trailing spaces. HOME, LOGNAME, USER and SHELL stay those that cron sets for the user; PWD, OLDPWD, SHLVL and _ of the writing shell are left out. /etc/environment and NAME=value lines in the crontabs cannot carry such values: pam_env ends a value at # and mangles quotes, and cron drops trailing spaces, takes no value with both kinds of quotes and ignores a whole file on a line it cannot read. The new image test runs jobs of CRONJOB_* variables as nobody and application and one of docker-cronjob, and compares what they see of a variable with spaces, quotes and $, and their PATH, with the container.
Kanti
force-pushed
the
feature/cron-jobs-from-environment
branch
from
November 23, 2026 08:25
e405a9a to
90f7df0
Compare
…ture/cron-jobs-from-environment # Conflicts: # docker/base/ubuntu-22.04/conf/bin/usr-bin/docker-cronjob # docker/base/ubuntu-22.04/conf/bin/usr-bin/docker-cronjob-shell # docker/php/8.2-alpine/conf/bin/usr-bin/docker-cronjob # docker/php/8.2-alpine/conf/bin/usr-bin/docker-cronjob-shell # docker/php/8.2/conf/bin/usr-bin/docker-cronjob # docker/php/8.2/conf/bin/usr-bin/docker-cronjob-shell # docker/php/8.3-alpine/conf/bin/usr-bin/docker-cronjob # docker/php/8.3-alpine/conf/bin/usr-bin/docker-cronjob-shell # docker/php/8.3/conf/bin/usr-bin/docker-cronjob # docker/php/8.3/conf/bin/usr-bin/docker-cronjob-shell # docker/php/8.4-alpine/conf/bin/usr-bin/docker-cronjob # docker/php/8.4-alpine/conf/bin/usr-bin/docker-cronjob-shell # docker/php/8.4/conf/bin/usr-bin/docker-cronjob # docker/php/8.4/conf/bin/usr-bin/docker-cronjob-shell # docker/php/8.5-alpine/conf/bin/usr-bin/docker-cronjob # docker/php/8.5-alpine/conf/bin/usr-bin/docker-cronjob-shell # docker/php/8.5/conf/bin/usr-bin/docker-cronjob # docker/php/8.5/conf/bin/usr-bin/docker-cronjob-shell # docker/php/8.6-alpine/conf/bin/usr-bin/docker-cronjob # docker/php/8.6-alpine/conf/bin/usr-bin/docker-cronjob-shell # docker/php/8.6/conf/bin/usr-bin/docker-cronjob # docker/php/8.6/conf/bin/usr-bin/docker-cronjob-shell # docs/customization.md # docs/environment-variables.md # docs/known-issues.md # provisioning/base/general/bin/usr-bin/docker-cronjob # provisioning/base/general/bin/usr-bin/docker-cronjob-shell
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Why
This builds on #667, whose branch is its base (stack: #667 → this). #667 runs the cron jobs of
docker-cronjobin Debian with/usr/local/bin/docker-cronjob-shell, which sends their output to the container log, unless aMAILTOline asks for cron's mail. This pull request uses the same script for the jobs of the new variables and lets it add the variables of the container; withMAILTO, the script still adds them and leaves the output to cron.Until 2026-11-23 the base was #657, which then held the output of cron jobs; that part moved to #667. "#657" below, in the tests and checks made before, means that part.
A cron job needs an image build today:
docker-cronjob '<line>'works in aRUNline of a Dockerfile, andcompose.yamlhas no way to add one. Mounting a directory over/etc/cron.d, or files into/opt/docker/etc/cron(which the start script of cron copies to/etc/cron.d), works in the Debian images only: BusyBoxcrondin the Alpine images reads no/etc/cron.d, but one crontab per user in/etc/crontabs, without the user column.And a job did not see the variables of the container in the Debian images: cron there passes on only
HOME,LOGNAME,SHELL,PWDandPATH=/usr/bin:/bin, sophpneeded/usr/local/bin/and a console command found noDATABASE_URLor other variable ofcompose.yaml. BusyBoxcrondin the Alpine images already passes on its own variables, which are those of the container.#280 asks exactly this: how to set up cron jobs without an image build, on
php-apache:alpine, without mounting/etc/cron.d, which did not work there.Closes #280
What
CRONJOB_<NAME>, one job each, any name afterCRONJOB_, in the format ofdocker-cronjob: the time, the user, the command.provisioning/base/general/provision/entrypoint.d/10-cronjobs.sh(new): runsdocker-cronjob --environmentat every start, for every command; forsupervisord, with--enable-cron.provisioning/base/general/bin/usr-bin/docker-cronjob:New mode
--environment: reads allCRONJOB_*variables (withenvListVars/envGetValue, so names such asCRONJOB_app.cleanupwork too), sorted by name, checks all of them first, then writes them in place of those of the last start. An empty variable adds no job. Each job gets a comment line with its variable./etc/cron.d/webdevops-environment(0644), written anew; removed when there is no job. It begins withSHELL=/usr/local/bin/docker-cronjob-shell, aswebdevops-dockerdoes since [BUGFIX] Send the remaining logs of the services to the container log #657, so the output of the jobs reaches the container log too.# webdevops environment beginand# webdevops environment endat the end of/etc/crontabs/<user>(0600). The blocks of the last start are removed from every crontab first, so a restart does not duplicate a job, a removed variable removes its job, and the lines ofdocker-cronjobstay. A crontab that held nothing but the block is removed.--enable-cron: if there is a job and cron is not enabled (autostart = trueincron.conf), printsEnabling cron for the jobs of the CRONJOB_* variablesand runsdocker-service enable cron. Only forsupervisord, the command that starts cron: a one-off command such asdocker compose run --rm app php -vdoes not change the services.One parser for both modes and for Alpine at build time (
parseJob), extended from [BUGFIX] Let docker-cronjob add jobs on Alpine #653:*, a value, a range or a list,*and ranges with a step; values in their range (minute0-59, hour0-23, day of month1-31, month1-12orjan-dec, day of week0-7orsun-sat, on Alpine0-6); a keyword one of@reboot,@yearly,@annually,@monthly,@weekly,@daily,@midnight,@hourly;These are the forms that both cron in Debian and BusyBox
crondtake (checked with both, see below). The check of the values matters on Debian: cron there ignores a whole file in/etc/cron.dwhen one line has an error, so one bad variable would have switched off all jobs of the variables.An error names the variable, e.g.
[ERROR] CRONJOB_CLEANUP: '24' is not a valid hour (0-23): 0 24 * * * application …, goes to stderr (also for a one-off command, whose start scripts write their stdout to/dev/null) and stops the entrypoint with exit code1, before the command runs.docker-cronjob '<line>'at build time on Debian still appends the lines as they are to/etc/cron.d/webdevops-docker, after theSHELLline of [BUGFIX] Send the remaining logs of the services to the container log #657, so comments andMAILTO=""keep working.docker-cronjoband of every crontab in/etc/cron.dwithSHELL=/usr/local/bin/docker-cronjob-shell, as the services get them (the variables ofenvironment/docker run -eand theENVof the image, as the entrypoint and supervisord pass them on):/opt/docker/bin/service.d/cron.d/20-job-environment.shrunsdocker-cronjob --job-environment(new option) each time cron starts, after10-init.shhas copied the files of/opt/docker/etc/cron. It writes the environment of the start script of cron, NUL-separated, into/run/docker-cronjob/<user>, one file for each user of a job in a crontab in/etc/cron.dwith thatSHELLline (directory0711, file0400of that user). Writing it when cron starts, not in the entrypoint, also covers jobs that scripts in/entrypoint.dadd, as the image-test hook of [BUGFIX] Send the remaining logs of the services to the container log #657 does, and afterdocker-service restart cronjobs added in a running container./usr/local/bin/docker-cronjob-shell(of [BUGFIX] Send the remaining logs of the services to the container log #657) first reads the file of its user and then runsenv -- <variables> /bin/sh -c <command>, with the output to syslog as before and the exit code of the command. So the values arrive exactly, with spaces, both kinds of quotes,$,=,#, backslashes, line breaks and trailing spaces. Without a file (a user whose first jobdocker-cronjobadds in a running container, until cron starts again) the job runs with the variables of cron, as before.HOME,LOGNAME,USERandSHELL, which cron sets for the user of the job (HOME=/rootof the container would be wrong forapplication), andPWD,OLDPWD,SHLVLand_, which belong to the shell that writes the file.HOSTNAME,TERMand theSUPERVISOR_*variables of supervisord stay, as on Alpine.crondpasses on what it inherits from supervisord.root, the same as get the variables anyway: the jobs of that user,root(also through/proc/1/environ), and onapplicationalso the PHP-FPM workers (clear_env = no). Other users, for examplewww-dataor a user with a job of its own, can neither read nor list them./runis no tmpfs in a container, so the files stay in its writable layer, wheredocker inspectshows the same variables to whoever may run it./etc/environment(read bypam_envfor cron): a#ends a value there (x # ybecamex), quotes are mangled ("a b" cbecamea b" c), a name such asphp.dottedis dropped, andsuandloginread it too. LinesNAME=valuein the crontabs: cron in Debian drops trailing spaces, takes no value with both'and"and no line over about 1000 characters, and ignores the whole file with all its jobs on such a line (each checked).docs/customization.md, "Cron jobs": the variables first, with the compose example, where the jobs go on Debian and Alpine, that a restart does not duplicate them, the forms of the time, the behaviour on errors, that a variable switches cron on again wheredocker-service disable cronswitched it off, and, in one paragraph with the output of the jobs from [BUGFIX] Send the remaining logs of the services to the container log #657, which variables of the container the jobs get and how; this replaces the sentence onPATH=/usr/bin:/binand absolute paths. A crontab of your own with theSHELLline gets the variables too.docker-cronjobfollows as the way for jobs that belong in the image ("Cron jobs in the image"), now also with the check of the time on Alpine and that Debian takes the lines as they are. The tools table points fromdocker-cronjobto the variables; "Services" says that a variable switches cron on again.docs/known-issues.md: the known issue of [BUGFIX] Send the remaining logs of the services to the container log #657 on crontab files of your own is now "Output and variables of cron jobs in crontab files of your own": cron in Debian also passes no variable of the container on to their jobs; theSHELLline helps for both.docs/environment-variables.md: a row forCRONJOB_<NAME>; the introduction says that cron jobs get the variables too;LOG_STDOUTnames the output of the jobs of the variables next to those ofdocker-cronjob.docs/images/php.md: the row of the Debian/Alpine table names both files on Debian, the crontab of the user on Alpine, the day of week0-7/0-6and how the jobs get the variables;php.md,php-nginx.mdandphp-apache.mdpoint to the variables under "Services".README.md: a short example at the end of "Quick start", and that the jobs get the variables of the container.ci/image-tests/Docker/Container.php:restart(),docker restartand wait for the entrypoint again.make generate.Not changed: files in
/opt/docker/etc/cron, which the start script of cron copies to/etc/cron.d, still have no effect on Alpine, and a mounted/etc/cron.dneither; that mechanism stays undocumented. The variables are the way that works on both. Jobs in other files in/etc/cron.don Debian without theSHELLline still get only the variables of cron, and cron mails their output (the known issue above).Changes for existing users
CRONJOB_is now read as a cron job. A container that has such a variable for something else exits at start with an[ERROR]line, also for one-off commands; an empty one is ignored.docker-service disable cron, aCRONJOB_*variable switches it on again when the container startssupervisord.docker-cronjoband of crontabs of your own with theSHELLline of [BUGFIX] Send the remaining logs of the services to the container log #657 see the variables of the container, e.g.TZ,LANGand thePATHof the container instead of/usr/bin:/bin. ASHELL=line of your own inwebdevops-dockerswitches that off for the jobs after it, as it does for their output.docker-cronjobat build time on Alpine now also fails on a time thatcronddoes not take: a value out of range,7as day of week, an unknown keyword such as@dayly, a step of0or after a single value. Before, it added such a line andcrondskipped it. Its error lines now go to stderr.Tests
Four new image tests in
ContainerEnvironmentChecks, which all PHP images use (andbase,base-appfor now), each in a container of its own:testCronRunsTheJobOfACronjobVariableAsItsUser:CRONJOB_IMAGETEST='* * * * * nobody id -un > /tmp/image-test-cronjob-variable; echo image-test-cronjob-output | tr a-z A-Z'; waits until the file saysnobody, and, where the output can reach the container log (Alpine, or Debian with syslog-ng), until the log has the lineIMAGE-TEST-CRONJOB-OUTPUT; in upper case, it differs from the command, which cron logs. A job without the user column on Alpine would write nothing, a job run as rootroot. The job writes the file asnobodyand the test only reads it, so/tmpis fine underfs.protected_regular;nobodymay not write to/app.testCronjobVariablesAreWrittenAnewAtEveryStart: withCRONJOB_IMAGETEST, adds a job withdocker-cronjob(as in an image built with it), switches cron off withdocker-service disable cronand restarts the container (docker restart). Then the log saysEnabling cron …, cron runs, and the job of the variable and the one ofdocker-cronjobare each there once (Debian: both files; Alpine:/etc/crontabs/nobody).env -u CRONJOB_IMAGETEST docker-cronjob --environment, as a start without the variable, removes the job of the variable and keeps the other.testCronJobsGetTheVariablesOfTheContainer:IMAGE_TEST_VALUE='a b "c" $d', andCRONJOB_IMAGETEST_*jobs asnobodyand, where the image has the user,application, plus a job ofdocker-cronjobasnobody; each writesprintenv IMAGE_TEST_VALUEandecho "$PATH"into a file of its own. Each file must hold exactly that value and thePATHof the container.testInvalidCronjobVariableStopsTheContainer(4 cases:not a job, a user that does not exist, hour24,@dayly):root echo image-test-startedwithCRONJOB_BROKENexits with 1, stderr has[ERROR] CRONJOB_BROKEN:, and the command did not run. A command that exits keeps the test from waiting forsupervisordon an image without the feature.The first commit adds the variables and the first three tests, the second the variables of the container for the jobs and
testCronJobsGetTheVariablesOfTheContainer.The images: the published images with the scripts of master, of #657 and of this pull request applied, in the way #657 tested its change (amd64):
php-nginx:8.5(Debian)cat: /tmp/image-test-cronjob-variable: No such file or directory), noEnabling cronin the log after the restart,root echo image-test-startedexits with 0 and printsimage-test-startedphp-nginx:8.5-alpineThe tests of #657, among them
testCronJobOutputReachesTheFilesFromTheEnvironmentandtestServiceOutputReachesTheFilesFromTheEnvironment, pass in both. Apart from the new tests, both runs of an image fail the same tests:testPhpVersion, on Alpine alsotestDockerImageCleanupandtestZicIsInstalled. They come from the older published images (PHP 8.5.12), not from this change.Before the rebase onto #657, with the files of this pull request copied over the published images: the 6 cases of the first three tests failed on the published images and passed with the first commit;
testCronJobsGetTheVariablesOfTheContainerfailed with the first commit on Debian (the job ofnobodywrote only/usr/bin:/bin, no value) and passed on Alpine, whosecrondalready passed the variables on; with both commits, all 7 passed inphp-nginx:8.5,php-nginx:8.5-alpine,base:ubuntu-22.04andbase-app:ubuntu-22.04.How it was verified
In
php-nginx:8.5andphp-nginx:8.5-alpine(amd64). Before the rebase onto #657, with the files of this pull request copied over the published image; the rows "With #657" in images with the scripts of master, #657 and this pull request applied to the published image:docs/customization.mdandREADME.mdcompose.yaml(only the image name changed to the local build),/app/bin/consolea script that logs the time,id -unand its argument;docker compose up -d05:40:01 application app:cleanup(Debian),05:40:00 application app:cleanup(Alpine); the job in/etc/cron.d/webdevops-environment(Debian) and in the block of/etc/crontabs/application(Alpine)docker compose restart;docker restartwith three variables and a job ofdocker-cronjobdocker-cronjobbefore the block,/etc/crontabs/rootunchangedCRONJOB_B,CRONJOB_A,CRONJOB_C,CRONJOB_EMPTY=0644 root, header,# CRONJOB_A… in that order; Alpine:0600 root, one block per user;CRONJOB_EMPTYadds nothingCRONJOB_CRONJOB_app.cleanup-1,CRONJOB_lowerenv -u … docker-cronjob --environmentwebdevops-environmentgone; Alpine: block gone, a crontab with nothing else (application) removed,docker-cronjoblines stay0,15,30 8-18/2 1-31 jan-dec sun-sat,*/10 * * 1-12 0-6,0 0 * * 7(Debian) in one file with a job that writes a file every minutecrond -d 0parsed every line withoutparse error24, minute61,5/10,*/0,1,,x,fooas month,@dayly,@DAILY, a quote,* * * * 7on Alpine; the same forms by hand against both crons[ERROR]line that names the field; Debiancronignored a file with0 24,5/10or*/0with all its jobs; BusyBox:parse errorfor24,61,7as day of week, and@daylyskipped without a wordCRONJOB_BROKEN='not a job';root truewith a value of two lines[ERROR] CRONJOB_BROKEN: Not a cron job line …in the log; same for the value of two linesCRONJOB_TRAILending in\ndocker-service disable cronis switched on again forsupervisordRUN docker-service disable cron, started with aCRONJOB_*Enabling cron for the jobs of the CRONJOB_* variables,autostart = true,crondRUNNING, the job ran; without the variableautostart = falsedocker-cronjobget the variables of the container, values exactly as setIMAGE_TEST_VALUEwith spaces, both kinds of quotes,$e,=,#, a backslash, a line break and two trailing spaces; jobs ofCRONJOB_*asroot,applicationandnobody, and one ofdocker-cronjob, that runprintenv IMAGE_TEST_VALUE,echo "$PATH"andenvPATHof the container, on both. Before (first commit, Debian): onlyHOME,LOGNAME,PATH=/usr/bin:/bin,PWD,SHELLphpworks without/usr/local/bin/application php -r "echo PHP_VERSION;"8.5.12on bothHOME,LOGNAME,SHELLof the user;PWD,SHLVLof the shell of the jobenvof the jobsHOME=/home/application,LOGNAME=application,SHELL=/bin/sh,PWD=/home/application(nobody:/nonexistent,/var/spool/cron); Alpine the same withUSERandHOME=/fornobodyphp.memory_limitdoes not reach the command; Alpine: it does, and addsUSER; both get theSUPERVISOR_*variables-e php.memory_limit=1G,envof the jobs; the names in a file of/run/docker-cronjobrootcan read/run/docker-cronjob/<user>sutoapplication,nobodyandwww-data,catof each file andlsof the directory0400of the user, directory0711docker restartSHELLline once in each crontabdocker-cronjobin a running container, before cron starts again) runs with the variables of cronCRONJOB_*, thendocker-cronjob '* * * * * www-data env > …'HOME,LOGNAME,PATH=/usr/bin:/bin,PWD,SHELL,SHLVL/etc/cron.dget only the variables of cronSHELLline, with a job that runsenvHOME,LOGNAME,PATH=/usr/bin:/bin,PWD,SHELLCRONJOB_OUT='* * * * * application printenv IMAGE_TEST_VALUE; echo "path=$PATH"; exit 3';docker-cronjob-shell -c "exit 3"aswww-data[SYSLOG] cron-job[112]: a b "c" $dand[SYSLOG] cron-job[112]: path=/usr/local/sbin:…; Alpine: the two lines as they are; exit code3/entrypoint.dadds, for a crontab of your own with theSHELLline, and afterdocker-service restart cronfor a new user/entrypoint.dscript withdocker-cronjobforwww-data, a mounted/etc/cron.d/minewith theSHELLline and a job formail, thendocker-cronjobfornobodyanddocker-service restart cron; each job writesprintenv IMAGE_TEST_VALUESHELL=line of your own switches the variables off for the jobs after itdocker-cronjobwith a job,SHELL=/bin/sh, a jobdocker-cronjobon Debian adds the lines as they are; on Alpine it fails on a time cron does not takeRUN docker-cronjob "# a comment" 'MAILTO=""' '* * * * * application …';docker-cronjob "0 24 * * * nobody true"webdevops-docker,0 24accepted as before; Alpine:[ERROR] '24' is not a valid hour (0-23), exit code 1ci/: the PHPUnit tests ofci/(341) pass, and PHPStan (level 8) and Rector with the configuration ofci/find nothing.php-nginx:8.5: linesNAME=valuein a crontab (cron dropped the trailing spaces of' lead and trail 'and ignored the whole file with"a'b\"c",'it''s'or a value of 2000 characters, but took one of 990), and/etc/environment(see What).baseandbase-app, which do not have cron installed, aCRONJOB_*variable makesdocker-service enable croninstall cron at the first start ofsupervisord(without network, the container exits at start with the error of apt); a one-off command does not install it.php,php-apacheand the-devimages by hand (same files throughphp), the other PHP versions, arm64. This pull request's CI runs the new tests on all of them on amd64 and arm64.