How To Setup Cron And Docker Correctly [A How To Guide] by@ifomin

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

Read on Terminal Reader
Open TLDR
react to story with heart
react to story with light
react to story with boat
react to story with money
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.
image
Igor Fomin HackerNoon profile picture

Igor Fomin

Full stack web developer, tech lead, project manager

linkedin social icongithub social icon

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
react to story with heart
react to story with light
react to story with boat
react to story with money
L O A D I N G
. . . comments & more!