Skip to content

Commit

Permalink
Fixed automated cron task processing in Alpine Docker containers
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianLeChat committed Apr 18, 2024
1 parent f78fe6e commit aca9049
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
13 changes: 6 additions & 7 deletions docker/Dockerfile.dev
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
# https://hub.docker.com/_/php
FROM php:alpine

# Install dependencies
RUN apk update && apk add --no-cache bash
# Install system dependencies
RUN apk update && apk add --no-cache bash supercronic

# Install some PHP extensions
RUN curl -sSLf -o /usr/local/bin/install-php-extensions \
Expand All @@ -21,16 +21,15 @@ COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer

# Add some cronjobs for Symfony custom commands
# https://github.com/webdevops/Dockerfile/issues/280#issuecomment-1311681838
RUN echo "* * * * * /usr/local/bin/php /var/www/html/bin/console app:tasks-executor > /dev/null 2>&1" >> /var/spool/cron/crontabs/root
RUN echo "0 * * * * /usr/local/bin/php /var/www/html/bin/console app:statistics-collector > /dev/null 2>&1" >> /var/spool/cron/crontabs/root
RUN echo "* * * * * /usr/local/bin/php /var/www/html/bin/console app:tasks-executor > /dev/null 2>&1" >> /var/spool/cron/crontabs/www-data
RUN echo "0 * * * * /usr/local/bin/php /var/www/html/bin/console app:statistics-collector > /dev/null 2>&1" >> /var/spool/cron/crontabs/www-data

# Add wait script to wait for other services to be ready
ADD https://github.com/ufoscout/docker-compose-wait/releases/latest/download/wait /wait
RUN chmod +x /wait

# Install Symfony CLI
RUN curl -1sLf https://dl.cloudsmith.io/public/symfony/stable/setup.alpine.sh | bash && \
apk add symfony-cli
RUN curl -1sLf https://dl.cloudsmith.io/public/symfony/stable/setup.alpine.sh | bash && apk add symfony-cli

# Copy all files to the working directory
COPY --chown=www-data:www-data . .
Expand All @@ -50,7 +49,7 @@ RUN --mount=type=cache,target=.composer composer install

# Use the PHP custom entrypoint
# https://symfony.com/doc/current/deployment.html / https://symfony.com/doc/current/setup/file_permissions.html
RUN echo '/wait && mkdir -p var/cache var/log && \
RUN echo '/wait && mkdir -p var/cache var/log && supercronic /etc/crontabs/www-data && \
/usr/local/bin/php bin/console doctrine:database:create --no-interaction --if-not-exists && \
/usr/local/bin/php bin/console doctrine:schema:update --complete --force && \
/usr/local/bin/php bin/console app:udp-server 127.0.0.1:8443 & \
Expand Down
9 changes: 6 additions & 3 deletions docker/Dockerfile.prod
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ RUN npm prune --production
# https://hub.docker.com/_/php
FROM php:fpm-alpine

# Install system dependencies
RUN apk update && apk add --no-cache supercronic

# Install some PHP extensions
RUN curl -sSLf -o /usr/local/bin/install-php-extensions \
https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions && \
Expand All @@ -43,8 +46,8 @@ COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer

# Add some cronjobs for Symfony custom commands
# https://github.com/webdevops/Dockerfile/issues/280#issuecomment-1311681838
RUN echo "* * * * * /usr/local/bin/php /var/www/html/bin/console app:tasks-executor > /dev/null 2>&1" >> /var/spool/cron/crontabs/root
RUN echo "0 * * * * /usr/local/bin/php /var/www/html/bin/console app:statistics-collector > /dev/null 2>&1" >> /var/spool/cron/crontabs/root
RUN echo "* * * * * /usr/local/bin/php /var/www/html/bin/console app:tasks-executor > /dev/null 2>&1" >> /var/spool/cron/crontabs/www-data
RUN echo "0 * * * * /usr/local/bin/php /var/www/html/bin/console app:statistics-collector > /dev/null 2>&1" >> /var/spool/cron/crontabs/www-data

# Add wait script to wait for other services to be ready
ADD https://github.com/ufoscout/docker-compose-wait/releases/latest/download/wait /wait
Expand All @@ -68,7 +71,7 @@ RUN --mount=type=cache,target=.composer composer install --no-dev --optimize-aut

# Use the PHP custom entrypoint
# https://symfony.com/doc/current/deployment.html / https://symfony.com/doc/current/setup/file_permissions.html
RUN echo '/wait && mkdir -p var/cache var/log && \
RUN echo '/wait && mkdir -p var/cache var/log && supercronic /etc/crontabs/www-data \
/usr/local/bin/php bin/console cache:clear && composer dump-env prod && \
/usr/local/bin/php bin/console doctrine:database:create --no-interaction --if-not-exists && \
/usr/local/bin/php bin/console doctrine:schema:update --complete --force && \
Expand Down

0 comments on commit aca9049

Please sign in to comment.