G
G
glader2018-10-04 15:22:21
Docker
glader, 2018-10-04 15:22:21

How to get environment variables in docker cron?

Building cron container:
Dockerfile

FROM ubuntu:latest

RUN apt-get update && apt-get -y install cron

# Add crontab file in the cron directory
ADD crontab /etc/cron.d/hello-cron

# Give execution rights on the cron job
RUN chmod 0644 /etc/cron.d/hello-cron

# Apply cron job
RUN crontab /etc/cron.d/hello-cron

CMD ["cron", "-f"]

crontab
* * * * * echo "TEST ${TEST}" > /proc/1/fd/1 2>/proc/1/fd/2

I run to check the visibility of a variable
$ docker build . -t cron
$ docker run --env TEST=xxx cron env | grep TEST
TEST=xxx

I run cron
$ docker run --env TEST=xxx cron
TEST

How to make a script run from cron see the passed variables?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Q
qq1, 2018-10-04
@glader

Cron always runs commands with an empty environment, docker has nothing to do with it. There are a couple of solutions here:
https://serverfault.com/questions/337631/crontab-e...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question