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:
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.
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.
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: