Docker Community Forums

Share and learn in the Docker community.

Problem while creating a cron in php:7.2.2-apache

Hello,

First, if i’m not in the good category, please tell me because I don’t know where to post.

My problem is : how to create a cron task in the php:7.2.2-apache container ?

Here is my Dockerfile :

FROM php:7.2.2-apache
RUN docker-php-ext-install pdo pdo_mysql mysqli
RUN apt-get update && apt-get install -y cron
COPY data/ /var/www/html/
RUN chown -R :www-data /var/www && chmod -R 777 /var/www/
RUN chmod +x /var/www/html/crons/cronInit.sh
ENTRYPOINT [ "/var/www/html/crons/cronInit.sh" ]

cronInut.sh :

#!/bin/bash
chmod +x /var/www/html/crons/cron
crontab /var/www/html/crons/cron
service cron reload
service cron restart
chmod +x /var/www/html/crons/cron.php
cron -f &
docker-php-entrypoint apache2 -D FOREGROUND

It does’nt work but only running the container with copying my “data” folder in allow me to access to my app normally (but the cron is not working)

Thanks for help

You can’t use the crontab command here, it is used to edit the file. Instead you could create a script that executes your commands in one of the /etc/cron.* folders.

I found a solution which works pretty good but thanks for the help

1 Like

Could you tell us what the solution was, I’m going through the same case.

Thanks.

Use supervisor to run multiple services and configure cron inside the PHP container but I don’t like to use supervisor just for cron.

You can run cron on your host or in an other container with a Docker client in it and use “docker exec” to execute commands in an any container.

1 Like