问题
I've got some cronjobs in my debian docker container. They don't start automatically why?
Do I have to do some workarounds?
回答1:
If you are running containers in docker, you can add cron tasks on the docker host machine to execute commands in the docker containers.
For example, to run 'stress' application in your container every 5 minutes you can add the following (substituting your container ID of course) to your crontab:
*/5 * * * * docker exec c78ddbed4ad9 /bin/sh -c 'stress -d 1 --hdd-bytes 64M --cpu 1 --io 2 --vm 2 --vm-bytes 64M --timeout 60s' >> /tmp/cronstress.log 2>&1
I am running this as root user on the docker host.
or just run cron:
root@dockerhost:cron
回答2:
Run cron daemon in docker container first. When login to container, in shell run follow command:
cron
Then jobs will start regularly.
回答3:
Where did you start cron? In a Dockerfile? There doing it within a RUN
won't work because that process will not keep running. You need to do that e.g. via CMD
. A better idea would be using a suzpervisord.
回答4:
cronjob is not starting the logrotation in docker container. my cronjob looks as below:
30 9 * * * root /usr/sbin/logrotate /etc/logrotate.d/apache2
Also I have added below script as ENTRYPOINT in docker file to start cron.
!/bin/bash
/etc/init.d/cron start
来源:https://stackoverflow.com/questions/31654125/cronjobs-in-docker-container-how-get-them-running