I have laravel project that need to update employees used leaves and remaining leaves on every year begin. So I have created scheduled task to run on every year begin. Also tested it in local setup by executing php artisan schedule:run
command. It worked properly, database get updated. As running artisan commands risky in production environment I’ve asked to try to run that cronjob using docker.
There’s the issue comes.
After create the docker image and run the container, cronjob is not working. Database didn’t get update, which means artisan command not executing. I have no idea where the issue comse from because when runservice cron status
on container CLI it shows cron running.
Senior developer had created dockerfile already. After going through some youtube videos and articles I did some changes for dcokerfile, I will mention that below. As I’m not familiar with docker I can’t figure out the issue, why does cronjob not working properly. I would be very thankful if anyone could point out the issue or how to do this. I have no idea how to set the paths. If you could share your knowledge with me, I’ll be very grateful.
This is my crontab file
* * * * * /usr/local/bin/php /var/www/artisan schedule:run >> /my-log.txt 2>&1
# Don’t remove the empty line at the end of this file. It is required to run the cron job
This is the docker file
# Use the official PHP image as base
FROM php:8.2-apache
# Enable Apache ModRewrite
RUN a2enmod rewrite
# Install required system packages
RUN apt-get update
&& apt-get install -y --no-install-recommends
libz-dev
libpq-dev
libjpeg-dev
libpng-dev
libssl-dev
libzip-dev
unzip
zip
&& apt-get clean
&& docker-php-ext-configure gd
&& docker-php-ext-configure zip
&& docker-php-ext-install
gd
exif
opcache
pdo_mysql
pdo_pgsql
pgsql
pcntl
zip
&& rm -rf /var/lib/apt/lists/*;
# Install cron
RUN apt-get update && apt-get -y install cron
&& apt-get clean;
# Enable SSL module
RUN a2enmod ssl
# Copy SSL certificate and key into the container
COPY ./docker/fullchain.pem /etc/ssl/certs/fullchain.pem
COPY ./docker/privkey.pem /etc/ssl/private/privkey.pem
# Copy custom Apache configuration file
COPY ./docker/laravel_ssl.conf /etc/apache2/sites-available/laravel_ssl.conf
# Enable the new site
RUN a2ensite laravel_ssl
# Restart Apache to apply changes
RUN service apache2 restart
RUN rm -rf /var/www/html/*
# Set the working directory in the container
# WORKDIR /var/www/html
# Copy the Laravel application files into the container
COPY . .
# Install Composer and dependencies
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN composer install --no-dev --optimize-autoloader
# Update Apache document root to point to the public directory
RUN sed -ri -e 's!/var/www/html!/var/www/html/public!g' /etc/apache2/sites-available/*.conf
RUN sed -ri -e 's!/var/www/!/var/www/html/public!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf
# Expose ports for HTTP and HTTPS
ENV APP_KEY=base64:MaRBFYRnBPBD1VP2SAYqYBGoOmgy5dlB23lygXKRTb8=
ENV DB_CONNECTION=mysql
ENV DB_HOST=127.0.0.1
ENV DB_PORT=3306
ENV DB_DATABASE=evicio_hrm
ENV DB_USERNAME=root
ENV DB_PASSWORD=
ENV APP_DEBUG=true
# Expose port 80 to the outside world
EXPOSE 80
EXPOSE 443
# Copy crontab file in the cron directory
COPY cronfile /etc/cron.d/cronfile
#Give execution rights on cron job
RUN chmod 0644 /etc/cron.d/cronfile
# Apply the cron job
RUN crontab /etc/cron.d/cronfile
# Create the log file to be able to run tail
RUN touch /var/log/cron.log
RUN chmod 0666 /var/log/cron.log
# Start Apache when the container starts
CMD ["sh", "-c", "service cron start && apache2-foreground"]
## Set folder perirmissions
RUN chmod -R 777 /var/www/html/bootstrap/cache/
&& chmod -R 777 /var/www/html/storage/