paint-brush
How To Setup Cron And Docker Correctly [A How To Guide] by@ifomin
3,988 reads
3,988 reads

How To Setup Cron And Docker Correctly [A How To Guide]

by Igor FominJanuary 29th, 2020
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

How To Setup Cron And Docker Correctly [A How To Guide] How To Set up Cron and Docker properly is a guide by Igor Fomin. Fomin is a Full stack web developer, tech lead, project manager. He uses docker to run his own crontab and run a command towards other container. He will need to rebuild this image every time I need to change my cron command or add a new one. It will be quite problematic to make calls from my container to other containers, where I really need command to run.

Company Mentioned

Mention Thumbnail
featured image - How To Setup Cron And Docker Correctly [A How To Guide]
Igor Fomin HackerNoon profile picture

What do I do if I need to use linux cron and execute command in one of my containers?

When I started to google this, I found 2 solutions right away, but both seemed quite bad:

Bad option 1:

Build custom crontab image, copy crontab file inside, and then run a command towards other container.

Problem with this approach is - I will need to rebuild this image every time I need to change my cron command or add a new one. Also it will be quite problematic to make calls from my cron container to other containers, where I really need command to run.

Bad option 2:

Modify container to run crontab.

Problem with this approach is - my container will have to run 2 commands (crontab + whatever container used to run) - this contradicts philosophy of docker, each container is supposed to be responsible only for 1 task.

Good option:

Run cron on host machine where I start my docker containers.

I can do it using command like this, right in crontab:

* * * * * docker exec -t {containerID} {command} >> /dev/null 2>&1

For example:


* * * * * docker exec -t {containerID} {command} >> /dev/null 2>&1

For example:
* * * * * docker exec -t $(docker ps -qf "name=docker_php_1") php artisan schedule:run >> /dev/null 2>&1

Notes:

  • normally I would run "docker exec -t <container_id> php artisan schedule:run" - but container id will change every time I restart container, and I do not want to change crontab everytime
  • $(docker ps -qf "name=docker_php_1") - allows to get container id based on search by container name
  • $(docker ps -qf "ancestor=php:7.2-fpm") - allows to get container id based on search by image name
  • php artisan schedule:run - Laravel specific command to run scheduled tasks, used here just as example
바카라사이트 바카라사이트 온라인바카라