Reputation: 3580
I have a setup with docker-compose
using nginx with php7-fpm. This is running and my PHP page is working fine. Now I need to execute some php scripts in a Cron Job, but I do not want to install php on the host machine, where I plan to install the Cron Jobs.
How can I run the php scripts using the php7-fpm docker container?
On my old system I had installed php7 so I could setup a cron task like this:
0 2 * * * /usr/bin/php /var/www/page/cron/my_php_script.php >> /var/log/cron.log 2>&1
Upvotes: 2
Views: 4807
Reputation: 345
There are a couple of ways, but I will do it navigating to the folder of the project and executing docker-compose exec
. Example:
0 2 * * * cd /my/project/folder && docker-compose exec -T php-fpm-container php -f /my/project/folder-inside-container/cron/my_php_script.php >> /var/log/cron.log 2>&1
Some important points to here:
docker-compose.yml
with the environment you setted up (if not you can use the flag -f
to point to the docker-compose file)php-fpm-container
should be the name of your service/container with the php-fpm installation/dev/stdout
so it can be accessed by docker-compose logs
command.Upvotes: 3
Reputation: 861
You might want to rebuild your docker-container according to your requirements & add crontab to inside the container if you're planning on using it more.
Alternatively, you can use dockers exec command, this allows you to execute a command inside a specified container. Your crontab could run the docker exec
with your PHP-Command.
Upvotes: 1