Running crontab in docker with web application

Viewed 18

Currently I have a docker container with a file structure that looks like this

Dockerfile
src > config.json
    > index.php
    > log.json

with the docker file containing

FROM php:7.2-apache
COPY src/ /var/www/html/

When built and launched works, the two json files can be read by index.php which I can open as a webpage.

I am trying to add a crontab that will execute a php script that can update the log.json file.

The problem is that what I have added does not execute the script but just makes the container stop after running it.

I added a few files to the project.

cron.txt
Dockerfile
src > config.json
    > index.php
    > log.json
    > process.php

process.php when run in the command line (# php process.php) will read the config.json, and will read and write to log.json. I added more lines to the dockerfile, so the full dockerfile now looks like:

FROM php:7.2-apache
COPY src/ /var/www/html/
RUN apt-get update && apt-get -y install cron

COPY cron.txt /etc/cron.d/cron.txt
RUN chmod 0644 /etc/cron.d/cron.txt
RUN crontab /etc/cron.d/cron.txt

CMD ["cron","-f"]

cron.txt contains

0 * * * * /user/local/bin/php /var/www/html/process.php
# second line to make cron valid

The final result I am expecting is that process.php will run every hour to update log.json, and I can still visit the site, but currently it the container just exits.

0 Answers
Related