Docker Community Forums

Share and learn in the Docker community.

Cronjobs in docker container


(Holzfelix) #1

I’ve got some cronjobs in my debian docker container. They don’t start automatically why?

Do I have to do some workarounds?


(Ralph Kincade) #2

HI !

Hows it going ?
I assume that your Debian container remains running after the launch (run “docker ps”)

can you share with me the exact command or Dockerfile that you used to launch the Debian container ?

can you also share the results of the “crontab -l” from INSIDE your Debian container?

thanks !!!


(Holzfelix) #3

Hi brikeyes I run it like this:

docker run -d -it -p 3000:3000 -p 80:80 -p 55:22 --name mycontainer mycontainer /bin/bash

And the crontab looks like this:

13,26,39,52 * * * * /bin/bash -l -c ‘cd /var/www/myapplication && RAILS_ENV=development bundle exec rake crons:generate_something --silent >> log/cron.log 2>&1’


(Sven Dowideit) #4

Most Docker containers only run the binary you specify (in this case /bin/bash), so there is no cron daemon running to trigger these jobs.

There are a number of ways you can deal with this - for small adhoc things, you can add the cron entry to your host’s crontab, and trigger the job in the container using docker exec containername ...

You can replace the container entrypoint with a shell script that starts the cron, or if you have a need for several services, use something like supervisord to manage them.

And if you’re going all in, you can make a cron container which you can then use to kick off tasks using docker exec.


(Sam Saffron) #5

To get a cron for say “daily” going properly you need to run both cron and anacron. If you do not run anacron, and restart your container say twice a day your daily/weekly will never fire.


(Holzfelix) #6

normal cronjobs aren’t possible? I generete the cronjobs with rails in my container with rails


(Holzfelix) #7

I need cronjobs every 13min / 20min / half an hour / hour …

can’t restart the container twice a day … I have to run stable


(Gbecedillas) #8

I’m actually using supervisord inside my containers and thought of adding one line to run “cron -f”. But if I did that wouldn’t that conflict with the cron instance running in the host ? Wouldn’t that make cronjobs outside the container, which are also inherited in the container, to run twice ?
Thanks a lot.


(Gbecedillas) #9

Forget my question, it doesn’t make any sense.


(Kuro) #10

yandere simulator, if you have a need for several services, use something like supervisord to manage them.


(Krasi) #11

I created this one for our projects - uses env vars , no mount bind configs , supports emailing the cron results , and can run the commands in a docker image or a running container.

https://www.vip-consult.solutions/post/better-docker-cron#content