3

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:

  • An Nginx container (based on nginx:1.12-alpine image)
  • A PHP-7.0 FPM container (based on php:7.0-fpm-alpine image)
  • A MySQL container (based on alpine:3.6 image)

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!

maGz
  • 787
  • 3
  • 8
  • 25

1 Answers1

10

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.

maGz
  • 787
  • 3
  • 8
  • 25
  • I'm working through a similar setup for Magento. Were you able to figure out how to communicate from the cron container to execute the cron.sh / cron.php file in a different container? That's where I'm stuck. – CMG Mar 15 '18 at 14:43
  • 3
    `RUN /usr/bin/crontab -u www-data /crontab` should be replaced by `RUN /usr/bin/crontab -u www-data /mycrontab` in your example – Django23 Feb 06 '19 at 13:45
  • Answering to "whether or not to ensure that the new Cron image has the same PHP/PECL extensions as the php fpm image". My guess is that if the php cli process is going to run similar pieces of code than the web app (converting images, sending emails, doing cryptographic operations...), then the same php extensions are probably needed. – Mitxel Nov 06 '21 at 20:12