Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

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?

like image 472
john Avatar asked May 26 '17 09:05

john


1 Answers

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
like image 183
Robert Avatar answered Sep 30 '22 01:09

Robert