Getting cron to run on php:7-fpm image [closed]

2020-07-05 06:19发布

问题:

I'm pretty new to docker. I've set up a dockerfile using the php:7-fpm image. As well as this image being used to run my site, I want to add a cron to be able to perform regular tasks.

I've created a cron, put it in the correct folder, and running docker exec -ti myimage_php_1 /bin/bash then cron or if I tail the log file all works fine. But I can't get this to work when the container is created, I don't want to have to manually start the cron obviously.

From what I understand, I need to use CMD or ENTRYPOINT to run the cron command on startup, but every time I do this it stops my site working due to me overriding the necessary CMD/ENTRYPOINT functionality of the original php:7-fpm image. Is there a way to trigger both the cron command and continue as before with the php:7-fpm CMD/ENTRYPOINTs?

回答1:

Approach #1

Create your custom entrypoint.sh, something like this:

#!/bin/bash

cron -f &
docker-php-entrypoint php-fpm

Note the &, it means "send to background".

Then:

COPY ./entrypoint.sh /
ENTRYPOINT /entrypoint.sh

Approach #2

But, there is a more sophisticated way that is installing supervisor, see docs (a demons manager used in docker):

In Dockerfile:

RUN apt-get update && apt-get install supervisor
COPY ./supervisord.conf /etc/supervisor/conf.d/supervisord.conf
...
CMD ["/usr/bin/supervisord"]

supervisord.conf

[program:cron]
command = cron -f

[program:php]
command = docker-php-entrypoint php-fpm

Some troubleshooting commands:

docker exec <container-id> supervisorctl status
docker exec <container-id> supervisorctl tail -f php
docker exec <container-id> supervisorctl tail -f cron
docker exec <container-id> supervisorctl restart php