Using Docker for Mac 1.13.1 with the following Dockerfile:
FROM ubuntu:latest
MAINTAINER [email protected]
#Install packages and clean downloaded packages in the lowest layer
RUN apt-get update && apt-get -y install cron && rm -rf /var/lib/apt/lists/*
# Add crontab file in the cron directory
ADD crontab /etc/cron.d/hello-cron
# Give execution rights on the cron job and create the log file to tail in the next layer
RUN chmod 0644 /etc/cron.d/hello-cron && touch /var/log/cron.log
# Run the command on container startup
CMD echo "starting" && echo "continuing" && (cron) && echo "tailing..." && tail -f /var/log/cron.log
With a contab file of:
* * * * * root echo "Hello world `date`" >> /var/log/cron.log 2>&1
# Don't remove the empty line at the end of this file. It is required to run the cron job
When I build and run it with:
docker build -t docker-cron-master .
docker run docker-cron-master
I see the output:
docker run docker-cron-master
starting
continuing
tailing...
If I wait a minute the tail -f
output doesn't appear. Yet if I login to the running container and tail the file I can see the contents:
$ docker exec -it 4eda6b1fc6ce bash
root@4eda6b1fc6ce:/# tail -f /var/log/cron.log
Hello world Fri May 5 09:53:01 UTC 2017
Hello world Fri May 5 09:54:01 UTC 2017
I tried adding another echo at the end of the CMD to see if it was only the last command who's STDOUT was being swallowed but that didn't help.
I have posted the code is on github at https://github.com/simbo1905/docker-cron
Thanks!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…