Search code examples
phpdockercronalpine-linuxfpm

How to run cron jobs inside php-fpm-alpine docker container?


Hi I don't know how can I run a cron job inside this container.

I've found this: How to run a cron job inside a docker container

But that overrides the CMD, I don't know hot to keep php-fpm working


Solution

  • When you need to run multiple processes in your docker container a solution is to use supervisord as the main instruction. Docker will start and monitor supervisord which in turn will start your other processes.

    Docker File Example:

    FROM debian:9
    ...
    CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/my.conf"]
    

    Supervisord config example (/etc/supervisor/my.conf):

    [supervisord]
    nodaemon=true
    
    [program:cron]
    command=/usr/sbin/crond -f -l 8
    stdout_logfile=/dev/stdout
    stderr_logfile=/dev/stderr
    stdout_logfile_maxbytes=0
    stderr_logfile_maxbytes=0
    autorestart=true
    
    [program:php-fpm]
    command=docker-php-entrypoint php-fpm
    

    Note that it is desirable to configure supervisord to output the logs to /dev/stdout and /dev/stderr to allow docker to handle these logs. Otherwise you risk your container to slow down over time as the amount of file writes increases.