Laravel Task Scheduler with Docker

Running a Laravel task scheduler with Docker can be a little different from the traditional methods.

Important concepts

  1. We will not use cron to run the scheduler
  2. By default schedule:work checks every minute, so we will use that to run the system process
  3. The actual time trigger itself is set within Laravel

More detail

We need to run the schedule:work command from Laravel. Although the docs say "Running the scheduler locally", this is what we want in production. It will run the scheduler in the foreground and execute it every minute. You can configure your Laravel app for the exact time that a command should run through a scheduled task.


Example Docker Compose File

version: '3'services:  php:    image: my/laravel-app    environment:      PHP_POOL_NAME: "my-app_php"  task:    image: my/laravel-app    # Switch to "webuser" before running `php artisan`    # Declare command in list manner for environment variable expansion    command: ["su", "webuser", "-c", "php artisan schedule:work"]    environment:      PHP_POOL_NAME: "my-app_task"

This is an example how we would set the actual execution time within Laravel itself:

Example Laravel `Kernel.php`

<?phpnamespace App\Console;use Illuminate\Console\Scheduling\Schedule;use Illuminate\Foundation\Console\Kernel as ConsoleKernel;class Kernel extends ConsoleKernel{    /**     * The Artisan commands provided by your application.     *     * @var array     */    protected $commands = [        //    ];    /**     * Define the application's command schedule.     *     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule     * @return void     */    protected function schedule(Schedule $schedule)    {        $schedule->command('process:invoices')->daily()->at('02:00')->timezone('America/Chicago');        $schedule->command('process:latefees')->daily()->at('04:00')->timezone('America/Chicago');    }    /**     * Register the commands for the application.     *     * @return void     */    protected function commands()    {        $this->load(__DIR__.'/Commands');        require base_path('routes/console.php');    }}