Laravel Dockerization: Setting up Supervisor for Horizon

Laravel Dockerization: Setting up Supervisor for Horizon

Introduction

Having a solid Docker setup for a Laravel project is great, but many developers struggle when it comes to dealing with additional services not included in the environment by default. For instance, when working with jobs, manually running commands like php artisan queue:work or php artisan horizon inside the container every time can become tedious. Luckily, there is a way to automate this part by using supervisor.

Supervisor in a Nutshell

If you are unfamiliar with supervisor, it is a straightforward process manager for Linux-like systems. It allows you to automate the execution and monitoring of commands continuously. To achieve this for Laravel Horizon, you need to install supervisor in the server container, create a configuration file for the Horizon command, and then start supervisor.

Installing and Configuring Supervisor

Installing Supervisor in a Docker container is as simple as doing it in any Linux-like system. However, you'll need to incorporate these steps into your Dockerfile. Here's a step-by-step guide:

1. First, create a horizon.conf file and place it in /server/config/. This file will contain the configuration for Laravel Horizon.

2. Next, add the following installation lines to your Dockerfile and copy the configuration file to /etc/supervisor/conf.d/.

3. Finally, start supervisor within your docker-entrypoint.sh script.

That's it! Optionally, you can also include php artisan horizon:publish to ensure your Horizon assets stay up to date.

References