How can I access Docker set Environment Variables From a Cron Job

后端 未结 8 1675
野性不改
野性不改 2021-01-30 20:24

I\'ve recently tried running a cron job from within a linked docker container and run into an issue. My main docker container is linked to a postgres container and its port numb

8条回答
  •  心在旅途
    2021-01-30 20:43

    One can append the system environment variables to the top of a crontab file by using wrapper shell script to run the cron daemon. The following example is from CentOs 7,

    In the Dockerfile

    COPY my_cron /tmp/my_cron
    COPY bin/run-crond.sh run-crond.sh
    RUN chmod -v +x /run-crond.sh
    CMD ["/run-crond.sh"]
    

    run_cron.sh:

    #!/bin/bash
    
    # prepend application environment variables to crontab
    env | egrep '^MY_VAR' | cat - /tmp/my_cron > /etc/cron.d/my_cron
    
    # Run cron deamon
    # -m off : sending mail is off 
    # tail makes the output to cron.log viewable with the $(docker logs container_id) command
    /usr/sbin/crond -m off  && tail -f /var/log/cron.log
    

    This is based on a great blog post somewhere, but I lost the link.

提交回复
热议问题