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

后端 未结 8 1661
野性不改
野性不改 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:52

    You can run the following command

    . <(xargs -0 bash -c 'printf "export %q\n" "$@"' -- < /proc/1/environ)
    

    This is exceptionally works when you have the environment variables with special characters like ' " =

    0 讨论(0)
  • 2021-01-30 20:54

    I would recommend using declare to export your environment and avoid escaping issues (use CMD or ENTRYPOINT or directly in a wrapper script which might be called by one of them):

    declare -p | grep -Ev 'BASHOPTS|BASH_VERSINFO|EUID|PPID|SHELLOPTS|UID' > /container.env

    Grep -v takes care of filtering out read-only variables.

    You can later easily load this environment like this:

    SHELL=/bin/bash
    BASH_ENV=/container.env
    * * * * * root /test-cron.sh
    
    0 讨论(0)
提交回复
热议问题