Get your queue worker and scheduler running, and add a convenient service to easily run Artisan commands
Hopefully, you’ve read my first post and now have a locally running docker setup. Now, let’s setup our queue worker, scheduler and a convenience service to run artisan commands.
Let’s start with an artisan container, whose only purpose will be to make running artisan commands less of a pain for you. Below you can see an excerpt of the
docker-compose.yml, with the relevant pieces to add.
services: ... artisan: container_name: myapp-artisan build: . entrypoint: php artisan volumes: - .:/var/www/html
That’s it. This will use the same image as is used for your main application, but instead run a command that will be
php artisan [your command]. You can now use this as follows:
docker-compose run --rm artisan migrate
This will run
php artisan migrate within your container.
To run the scheduler, we’re gonna run
cron in our image. First, let’s create a crontab file:
echo "* * * * * php /var/www/html/artisan schedule:run >> /dev/null 2>&1" > crontab
This will run
php artisan schedule:run every minute to run all your scheduled commands.
services: ... scheduler: container_name: myapp-scheduler build: . command: runsv /etc/service/cron volumes: - .:/var/www/html - ./crontab:/etc/crontab
Last but not least, we’ll start a queue worker:
services: ... queue-worker: container_name: myapp-queue build: . command: php artisan queue:work volumes: - .:/var/www/html
If you run Laravel Horizon to manage your queues instead, you can equally well change the
command part to be
php artisan horizon instead.
And that’s it! With those 3 services setup, we can now run our entire Laravel setup locally, in Docker, without installing any additional dependencies on our own machines!