service-cli icon indicating copy to clipboard operation
service-cli copied to clipboard

Make Docker-set environment variables accessible in cron jobs

Open lmakarov opened this issue 4 years ago • 3 comments

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

lmakarov avatar Jun 16 '20 17:06 lmakarov

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.

prudloff-insite avatar Mar 18 '22 16:03 prudloff-insite

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

prudloff-insite avatar Mar 23 '22 17:03 prudloff-insite

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

nguyenlouis90 avatar May 16 '24 08:05 nguyenlouis90