I am new to docker. Hardly I have containerized my php application to run it in the web interface. But I have some cron to run with it. I learnt how to create separate cron image and run it from How to run a cron job inside a docker container?. But my use case is different. I need to use the php files from my php application container which seems not possible from my way. I tried creating the docker-compose.yml as follow to see if it would work
docker-compose.yml:
version: "3" services: app: build: context: ./docker/php container_name: 'app' restart: 'always' ports: - "80:80" - "443:443" links: - db volumes: - ${DOCUMENT_ROOT-./src}:/var/www/html - ${PHP_INI-./docker/php/php.ini}:/usr/local/etc/php/php.ini - ${VHOSTS_DIR-./docker/apache2/vhosts}:/etc/apache2/sites-enabled - ${LOG_DIR-./docker/logs/apache2}:/var/log/apache2 extra_hosts: - "test.local:127.0.0.1" hostname: cloudservice.local domainname: local #entrypoint: sh /var/www/html/cron.sh
As I have commented entry point here and if I do docker-compose up, everything works perfectly fine, My Dockerfile is as under
Dockerfile:
FROM php:7.2.27-apache RUN apt-get update RUN apt-get install -y cron RUN apt-get -y update --fix-missing RUN apt-get upgrade -y # Install useful tools RUN apt-get -y install apt-utils nano wget dialog # Install important libraries RUN apt-get -y install --fix-missing apt-utils build-essential git curl libcurl4 libcurl4-openssl-dev zip # Composer RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer # Install xdebug #RUN pecl install xdebug-2.5.0 #RUN docker-php-ext-enable xdebug # Other PHP7 Extensions RUN apt-get -y install libsqlite3-dev libsqlite3-0 default-mysql-client RUN docker-php-ext-install pdo_mysql RUN docker-php-ext-install pdo_sqlite RUN docker-php-ext-install mysqli RUN docker-php-ext-install curl RUN docker-php-ext-install tokenizer RUN docker-php-ext-install json RUN apt-get -y install zlib1g-dev RUN docker-php-ext-install zip RUN apt-get -y install libicu-dev RUN docker-php-ext-install -j$(nproc) intl RUN docker-php-ext-install mbstring RUN apt-get install -y libfreetype6-dev libjpeg62-turbo-dev libpng-dev RUN docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ RUN docker-php-ext-install -j$(nproc) gd RUN pecl install redis-5.1.1 && docker-php-ext-enable redis # Enable apache modules RUN a2enmod rewrite headers
My cron.sh file is as under
#!/usr/bin/env bash # Ensure the log file exists touch /var/www/html/logs/crontab.log # Ensure permission on the command chmod a+x /var/www/html/cron-local/hn-shc-rapid-daily.sh # Added a cronjob in a new crontab echo "* * * * * bash /var/www/html/cron-local/hn-shc-rapid-daily.sh >> /var/www/html/logs/crontab.log 2>&1" > /etc/crontab # Registering the new crontab crontab /etc/crontab # Starting the cron /usr/sbin/service cron start # Displaying logs # Useful when executing docker-compose logs mycron tail -f /var/www/html/logs/crontab.log
But with the entry point commented, i cannot run cron. If i don’t comment entrypoint, then cron runs, my web application doesn’t. Is there any possibility to fix this?
Thanks
Advertisement
Answer
At the end of the docker file i added the following code after removing
RUN a2enmod rewrite headers
# Copy hello-cron file to the cron.d directory COPY hello-cron /etc/cron.d/hello-cron #just any name # Give execution rights on the cron job RUN chmod 0644 /etc/cron.d/hello-cron # Apply cron job RUN crontab /etc/cron.d/hello-cron # Create the log file to be able to run tail RUN touch /var/log/cron.log # Enable apache modules RUN a2enmod rewrite headers CMD cron && /usr/sbin/apache2ctl -D FOREGROUND
and cron.sh was changed to
* * * * * echo "hello world" >> /var/www/html/logs/crontab.log 2>&1
This worked for me. I need not have to add entry neither on docker-compose nor on docker file, but I guess entry also would work. I don’t know if this is the right way to do. If anyone would give me more advance idea, I would be happy to give it a try. Thanks