如何从Cron作业中访问Docker设置环境变量 [英] How can I access Docker set Environment Variables From a Cron Job

查看:141
本文介绍了如何从Cron作业中访问Docker设置环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近尝试从链接的Docker容器中运行一个cron作业,遇到问题。我的主docker容器链接到postgres容器,并且在容器创建时,其端口号被docker设置为一个环境变量。此环境变量未在〜/ .profile或运行我的cron作业时加载的任何其他源文件中设置。如何才能从我的cron工作中访问这些环境变量?



谢谢!

解决方案

我遇到同样的问题。我有一个运行cron的docker容器定期执行一些shell脚本。我也很难找出当我在容器内手动执行时,为什么我的脚本会运行正常。我尝试了创建一个首先运行环境的shell脚本的所有技巧,但是它们从未对我有用(很可能我做错了)。但是我继续寻找并发现了这一点,并且它的工作正常。


  1. 为您的cron容器设置一个启动或入口点shell脚本

  2. 使它成为执行 printenv |的第一行grep -vno_proxy>> / etc / environment

这里的诀窍是 / etc / / code>文件。当容器建成时,文件是空的,我有意思。在 cron(8)

的手册页中,我找到了对该文件的引用。 / A>。在查看cron的所有版本后,它们都会包含一个 / 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.

  1. Setup a start or entry point shell script for your cron container
  2. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆