Configure the scheduled task crontab in the Docker container (DaoCloud + Docker + Laravel5)

Source: Internet
Author: User
Tags composer install
A wild full-stack engineer configures the scheduled task crontab (DaoCloud + Docker + Laravel5) in a Docker container and prefers to study various new technologies.

Follow Now

Recently, the project involves the function of a scheduled task. so I went to study how to use crontab over the past few days and successfully enabled the function on my computer according to relevant online tutorials.

Add crontab configuration in Laravel + crontab

1. execute commands

$ crontab -e

2. add the following content (path/to is the application path), that is, execute the following command every minute

* * * * * php /path/to/artisan schedule:run >> /dev/null 2>&1

3. start (the following command is the startup command under Ubuntu, which may be different from other systems)

$ /etc/init.d/cron start

After completing the preceding three steps, you can successfully enable Laravel's task scheduling function. The following is a verification:

Verify that the task scheduling is normal

./App \ Console \ Kernel

... Protected function schedule (Schedule $ schedule) {$ schedule-> call (function () {// The following code inserts a Tweet data DB: table ('Tweets ') -> insert (['content' => 'hi']);})-> everyMinute ();}...

In a few minutes, you can check whether the database has successfully inserted data ~

References
  • Laravel task scheduling
  • How to use crontab for scheduled tasks under docker [python]
  • Laravel scheduled task
DaoCloud + Docker + Laravel + crontab

After the above attempt is successful, these configurations are added to the Dockerfile, so that the container can automatically enable the crontab task scheduling at the start time, so that all of these tasks can be completed automatically ~

Procedure

1. create the following crontab configuration file in the project./_ linux/var/spool/cron/crontabs/root

* * * * * /usr/local/bin/php /app/artisan schedule:run >> /dev/null 2>&1

2. copy the configuration file to the Directory of the configuration specified by crontab in Dockerfile.

FROM php: 7.0.7-apacheMAINTAINER JianyingLi
 
  
# Install the cron command... RUN apt-get update & apt-get install-y cron vim... # configure crontab # Copy the configuration file/var/spool/cron/crontabs/ADD _ linux/var/spool/cron/crontabs/root # set the file owner and file association group to root: crontab: the associated group must be crontabRUN chown-R root: crontab/var/spool/cron/crontabs/root \ # Modify the object ACL, which must be 600, otherwise, do not recognize & chmod 600/var/spool/cron/crontabs/root # create the log file RUN touch/var/log/cron. log... # In entrypoint. RUN chmod 777 is added to the sh script to start apache and crontab. /entrypoint. shENTRYPOINT [". /entrypoint. sh "]
 

3. add the./entrypoint. sh script and start apache and crontab

Php: 7.0.7-the basic image of apache already contains a CMD ['apache2'-foreground '] command to start the apache service, but we need to start apache and crontab at the same time, therefore, this script file is added and related commands are added to it.

#! /Bin/bashset-x # save the environment variables to/etc/default/localerm-rf/etc/default/localeenv>/etc/default/locale # start crontab/etc/ init. d/cron start # start apacheapache2-foregroundexec "$ @"

The crontab execution mechanism makes it impossible to directly use the environment variables configured through the DaoCloud background, but our application configurations are configured through the environment variables, therefore, you need to use the env command to save these environment variables to/etc/default/locale. crontab will load the environment variables in this file at startup; otherwise, execute the php artisan schedule: the run command will not be able to obtain the relevant application configurations, resulting in failure to run the license we expected (for example, it is impossible to insert new data to the database)

Complete related files

The following three configuration files are used to build and run the Laravel5 application, which can meet most of the requirements.

 .├── _linux│   └── var│       └── spool│           └── cron│               └── crontabs│                   └── root├── Dockerfile└── entrypoint.sh

./Dockerfile

FROM php:7.0.7-apacheMAINTAINER JianyingLi 
 
  RUN apt-get update     \ && apt-get install -y \      libmcrypt-dev \      libz-dev      \      git           \      cron          \      vim           \ && docker-php-ext-install \      mcrypt    \      mbstring  \      pdo_mysql \      zip       \ && apt-get clean      \ && apt-get autoclean  \ && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composerADD _linux/var/spool/cron/crontabs/root /var/spool/cron/crontabs/rootRUN chown -R root:crontab /var/spool/cron/crontabs/root \ && chmod 600 /var/spool/cron/crontabs/rootRUN touch /var/log/cron.logRUN a2enmod rewriteWORKDIR /appCOPY ./composer.json /app/COPY ./composer.lock /app/RUN composer install --no-autoloader --no-scriptsCOPY . /appRUN rm -fr /var/www/html \ && ln -s /app/public /var/www/htmlRUN chown -R www-data:www-data /app \ && chmod -R 0777 /app/storage      \ && composer installRUN chmod 777 ./entrypoint.shENTRYPOINT ["./entrypoint.sh"]
 

./_ Linux/var/spool/cron/crontabs/root

* * * * * /usr/local/bin/php /app/artisan schedule:run >> /dev/null 2>&1

./Entrypoint. sh

#!/bin/bashset -xrm -rf /etc/default/localeenv >> /etc/default/locale/etc/init.d/cron startapache2-foregroundexec "$@"
Example (to complete the code, refer to the following project)
  • Phoenix-backend
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.