如何从Cron作业中访问Docker设置环境变量 [英] How can I access Docker set Environment Variables From a Cron Job
问题描述
谢谢!
我遇到同样的问题。我有一个运行cron的docker容器定期执行一些shell脚本。我也很难找出当我在容器内手动执行时,为什么我的脚本会运行正常。我尝试了创建一个首先运行环境的shell脚本的所有技巧,但是它们从未对我有用(很可能我做错了)。但是我继续寻找并发现了这一点,并且它的工作正常。
- 为您的cron容器设置一个启动或入口点shell脚本
- 使它成为执行
printenv |的第一行grep -vno_proxy>> / etc / environment
这里的诀窍是 / etc / / code>文件。当容器建成时,文件是空的,我有意思。在 cron(8)
/ etc /?
文件,您可以使用它们将环境变量提供给子进程。 另外,请注意,我创建了我的docker容器在前台运行cron, cron -f
。这有助于我避免使用 tail
运行以保持容器的其他技巧。
这是我的entrypoint.sh文件作为参考,我的容器是debian:jessie base image。
printenv | grep -vno_proxy>> / etc / environment
cron -f
在 docker运行
命令期间设置的环境变量。
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 number is set as an environment variable by docker upon the containers creation. This environment variable is not set in ~/.profile or any other source file that I could load when running my cron job. How can I then access these environment variables from my cron job?
Thanks!
I ran into this same problem. I have a docker container that runs cron to execute some shell scripts periodically. I too had a hard time finding out why my scripts would run fine when I manually executed them inside the container. I tried all the tricks of creating a shell script that would run first to set the environment, but they never worked for me (most likely I did something wrong). But I continued looking and found this and it does work.
- Setup a start or entry point shell script for your cron container
- Make this the first line to execute
printenv | grep -v "no_proxy" >> /etc/environment
The trick here is the /etc/environment
file. When the container is built that file is empty, I think on purpose. I found a reference to this file in the man pages for cron(8). After looking at all the versions of cron they all elude to an /etc/?
file that you can use to feed environment variables to child processes.
Also, note that I created my docker container to run cron in the foreground, cron -f
. This helped me avoid other tricks with tail
running to keep the container up.
Here is my entrypoint.sh file for reference and my container is a debian:jessie base image.
printenv | grep -v "no_proxy" >> /etc/environment
cron -f
Also, this trick worked even with environment variables that are set during, docker run
commands.
这篇关于如何从Cron作业中访问Docker设置环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!