Docker Laravel and Cron

Posted: 2018-02-16 13:28:31

This to this post here

for the idea.

Basically running cron in Docker is easy BUT once the Host had more than one Docker client cron stopped working. No clue why but to solve this I did two things

One made a script that was on the Docker Client

/scheduler.sh

#!/bin/bash
php /app/artisan schedule:run --verbose --no-interaction >> /dev/null 2>&1
sleep 60

Then I did this in the Dockerfile

COPY scheduler.sh /
RUN chmod 755 /scheduler.sh

Finally supervisord manages it:

[supervisord]
nodaemon=true


[program:scheduler]
command=/scheduler.sh
autostart=true
autorestart=true
user=root
startsecs = 0
numprocs=1
redirect_stderr=true
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr

the startsecs allows it not to be seen as a failing script

Full Dockerfile

FROM ubuntu:16.04

#https://github.com/shipping-docker/dockerized-app/blob/master/docker/app/Dockerfile
MAINTAINER Alfred Nutile

RUN apt-get update \
    && apt-get install -y locales \
    && locale-gen en_US.UTF-8

ENV LANG en_US.UTF-8
ENV LANGUAGE en_US:en
ENV LC_ALL en_US.UTF-8

## had to leave both update requests
RUN apt-get update \
    && DEBIAN_FRONTEND=noninteractive apt-get install -y cron anacron nginx curl zip unzip git software-properties-common supervisor sqlite3 \
    && add-apt-repository -y ppa:ondrej/php \
    && apt-get update \
    && apt-get install -y screen vim php7.1-fpm php7.1-cli php7.1-mcrypt php7.1-gd php7.1-mysql \
    php7.1-pgsql php7.1-imap php-memcached php7.1-mbstring php7.1-xml php7.1-curl \
    php7.1-sqlite3 php7.1-xdebug imagemagick php7.1-intl php7.1-zip \
    && php -r "readfile('http://getcomposer.org/installer');" | php -- --install-dir=/usr/bin/ --filename=composer \
    && mkdir /run/php \
    && apt-get remove -y --purge software-properties-common \
    && apt-get -y autoremove \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

RUN mkdir /etc/nginx/ssl
COPY nginx.crt /etc/nginx/ssl/nginx.crt
COPY nginx.key /etc/nginx/ssl/nginx.key
COPY default /etc/nginx/sites-available/default
COPY php-fpm.conf /etc/php/7.1/fpm/php-fpm.conf
COPY xdebug.ini /etc/php/7.1/mods-available/xdebug.ini

# 2018 02 16 09 13 39
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
COPY start-container /usr/local/bin/start-container
RUN chmod +x /usr/local/bin/start-container
## Deal with cron issues
COPY scheduler.sh /
RUN chmod 755 /scheduler.sh
CMD [ "start-container" ]