Kersten
Kersten

Reputation: 1722

Running a cronjob or task inside a docker cloud container

I got stuck and need help. I have setup multiple stacks on docker cloud. The stacks are running multiple container like data, mysql, web, elasticsearch, etc.

Now I need to run commands on the web containers. Before docker I did this with cronjob eg:

*/10 * * * * php /var/www/public/index.php run my job

But my web Dockerfile ends with

CMD ["apache2-foreground"]

As I understand the docker concept running two commands on one container would be bad practice. But how would I schedule a job like the one cronjob above?

Should I start cron in the CMD too, something like?

CMD ["cron", "apache2-foreground"] ( should exit with 0 before apache starts)

Should I make a start up script running both commands?

In my opinion the smartest solution would be to create another service like the dockercloud haproxy one, where other services are linked.

Then the cron service would exec commands that are defined in the Stackfile of the linked containers/stacks.

Thanks for your help

Upvotes: 1

Views: 768

Answers (1)

peedee
peedee

Reputation: 3739

With docker in general I see 3 options:

  • run your cron process in the same container
  • run your cron process in a different container
  • run cron on the host, outside of docker

For running cron in the same container you can look into https://github.com/phusion/baseimage-docker

Or you can create a separate container where the only running process inside is the cron daemon. I don't have a link handy for this, but they are our there. Then you use the cron invocations to connect to the other containers and call what you want to run. With an apache container that should be easy enough, just expose some minimal http API endpoint that will do what you want done when it's called (make sure it's not vulnerable to any injections, i.e. don't pass any arguments, keep it simple stupid).

If you have control of the host as well then you can (ab)use the cron daemon running there (I currently do this with my containers). I don't know docker cloud, but something tells me that this might not be an option for you.

Upvotes: 2

Related Questions