maGz
maGz

Reputation: 827

Docker - Using PHP Cli base image for Cron container

So I've been having loads of fun with Docker and Docker-Compose recently, but need the community's advice here. I have a docker-compose file comprised of:

In keeping with Docker's principles of separating processes into individual containers, I would now like to add a fourth container to run Cron. The crontab will be simply:

*/5 * * * * www-data sh /var/www/html/cron.sh

This is for a Magento site, and cron usually requires decent computing power - therefore I figure it would be great to have it in its own container.

I have 3 questions regarding this ...

1) Magento's cron.sh runs a few checks and then runs cron.php, so I assume that my new cron container has to have php installed and running - correct?

2) Assuming that I do need php running in the cron container, then I'm uncertain if I should create a Dockerfile based off the php:7.0-cli-alpine image or to use an alternative? I've read somewhere that CLI images don't run in daemon mode, so this container may exit immediately after running docker-compose up -d.

3) Assuming that I am correct on (1) and (2) above, will I need to ensure that any PHP/PECL extensions I've installed in my custom PHP-7.0 FPM image are also installed in this new cron image?

Thank you for time!

Upvotes: 3

Views: 5834

Answers (1)

maGz
maGz

Reputation: 827

Ok so I made some headway with this. I created a new image based on php:7.0-cli-alpine here is what my Dockerfile looks like:

FROM php:7.0-cli-alpine

COPY mycrontab /mycrontab

RUN mkdir /var/log/cron
RUN touch /var/log/cron/cron.log

RUN chown -R www-data:www-data /var/log/cron

RUN /usr/bin/crontab -u www-data /crontab

CMD ["crond", "-f", "-l", "8"]

here is what mycrontab looks like:

*/1 * * * * echo "This is a test" >> /var/log/cron/cron.log 2>&1

Built it: docker build -t test/cron:latest .

Ran it: docker run --rm -it -v /home/magz/devtest/cron/cron.log:/var/log/cron/cron.log

After a minute (or so), I got an expected output into cron.log :)

So, so far, so good. I'm yet to test this with my docker-compose setup for my Magento site, but I'm confident this will work.

The only question I have remaining is whether or not to ensure that the new Cron image has the same PHP/PECL extensions installed as my PHP-FPM image.

Upvotes: 10

Related Questions