service-cli
service-cli copied to clipboard
Make Docker-set environment variables accessible in cron jobs
Docker set environment variables (from Dockerfile ENV
and docker-compose environment
) are currently not visible in the environment of a cron job process.
We need to figure out a way to pass them correctly to cron jobs/etc. We could write to /etc/profile.d/docker-environment.sh
, similar to how we handle secrets' conversion:
https://github.com/docksal/service-cli/blob/113ca9fd22b888b520c2b9bd50e7b2127207a226/7.3/startup.sh#L107-L108
References:
- https://stackoverflow.com/questions/27771781/how-can-i-access-docker-set-environment-variables-from-a-cron-job
Related issues: #128 , #187, https://github.com/docksal/docksal/issues/1401
We needed VIRTUAL_HOST
to be exposed to our cron jobs (because we run some tasks that need to build absolute URLs) so we used this workaround in docksal.yml
:
cli:
environment:
- SECRET_VIRTUAL_HOST=${VIRTUAL_HOST}
This is not very clean because it hijacks the secret system for something else, so it would be better if Docksal could handle this automatically.
We noticed a related problem. Our container was spammed with requests like this:
web_1 | 192.168.32.9 - - [18/Mar/2022:18:12:19 +0000] "GET /exec_in_progress_inside_cli HTTP/1.1" 200 21
web_1 | 192.168.32.9 - - [18/Mar/2022:18:12:19 +0000] "GET /exec_in_progress_inside_cli HTTP/1.1" 200 21
web_1 | 192.168.32.9 - - [18/Mar/2022:18:12:19 +0000] "GET /exec_in_progress_inside_cli HTTP/1.1" 200 21
web_1 | 192.168.32.9 - - [18/Mar/2022:18:12:19 +0000] "GET /exec_in_progress_inside_cli HTTP/1.1" 200 21
web_1 | 192.168.32.9 - - [18/Mar/2022:18:12:19 +0000] "GET /exec_in_progress_inside_cli HTTP/1.1" 200 21
It seems the problem is that we have a crontab calling bash, which loads ~/.profile
which calls /opt/ping-web.sh
but without the WEB_KEEPALIVE
environment variable being set so the script calls curl in a loop.
This fixed the problem:
cli:
environment:
- SECRET_WEB_KEEPALIVE=0
I make a workaround by transfer ENV to crobtab through env file:
-
Dockerfile:
CMD (env > /run/crontab.env) && (cron -f)
-
crobtab:
* * * * * BASH_ENV=/run/crontab.env echo $(pwd) System online at
date> /proc/1/fd/1 2>&1