Cronjobs in docker container

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

Do I have to do some workarounds?

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 !!!

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’

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.

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.

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

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

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

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.

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

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

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