You'd like a docker container that runs cron jobs, with the output of those cron jobs going to stdout so they are accessible as docker logs. How do you do that?
In your Dockerfile, apt-get -y install cron
Use apk or whatever if you are running on alpine or another distribution
Set the command or entrypoint in your Dockerfile to run cron 9n the foreground
ENTRYPOINT ["/usr/sbin/cron", "-f"]
Copy the crontab with your jobs in it into /etc/crontab
. Each job should redirect it's stdout and stderr to fh 1 of PID 1
(the crontab process). Here's a simple example crontab:
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
* * * * * root echo "HELLO!" > /proc/1/fd/1 2>&1
This just prints "HELLO" every minute, redirecting the echo output to process 1's stdout (file handle 1). This line also redirects stderr, although that is not really necessary in the case of a simple echo.
This works because docker always treats the stdout from process 1 as the docker log stream.
... to run a single job on a regular basis is to use date
and sleep
. This makes for a simpler container (no need for cron) if you only need a command or commands to run at a single interval (say, every morning at 3:00AM). Details
in this gist
In other words (with logging):
docker-compose.yml
:rsyslog.conf
:site
:Output:
Or the alpine version.