stratboy's avatar

How to install supervisor in Docker

So I'm using Docker Desktop to run Laravel. I want to use queues both for Scout with Mailisearch and for other tasks, so I'd like to also install Supervisor in Docker and not just use php artisan queue:work, because I'd like to better understand how it would work in production, and also maybe to have all ready for deploy (but I don't know, I never deployed a Laravel app, it's my first one).

I'm not sure on how to do it. Should I use a Dockerfile (and by the way, why Laravel does not have one?)? Am I going to screw up all my app (which it's already about half developed) if I use a Dockerfile?

I've already found this guide but not sure if it's the best approach with Laravel/Sail:

https://ismatbabir.medium.com/use-supervisor-in-docker-container-9f61819bac4c

0 likes
15 replies
Foks's avatar

I don't think you would screw up your app if you were going to use Dockerfile, however I'd recommend using Docker compose - which Laravel Sail provides.

But you should be able to run the queue worker directly from the docker compose file.

1 like
vincent15000's avatar

@Foks @stratboy Sail should not be used in production.

Supervisor is already installed via Sail.

If you need to install supervisor in Docker for production use, it's very easy.

// in the docker-compose.yml file
supervisor:
  build:
    context: .
    dockerfile: supervisor.dockerfile
  container_name: yggdra-supervisor
  volumes:
    - ./src:/var/www/html
  depends_on:
    php:
      condition: service_started
    rabbitmq:
      condition: service_healthy
    mariadb:
      condition: service_started
    vendor:
      condition: service_started
  restart: always
// and the supervisor.dockerfile
FROM php:8.3-cli

RUN apt-get update && apt-get install -y supervisor

RUN docker-php-ext-install pdo pdo_mysql

RUN mkdir -p /var/log/supervisor

COPY supervisor/supervisord.conf /etc/supervisor/conf.d/supervisord.conf
COPY supervisor/conf.d/rabbitmq-consume.conf /etc/supervisor/conf.d/rabbitmq-consume.conf

CMD ["/usr/bin/supervisord"]

And sure you need to have a global configuration file for supervisor and one configuration file for each functionality you need to execute via supervisor.

1 like
Foks's avatar

@vincent15000 I'm aware that Sail shouldn't be used in production. OP never mentioned using Laravel Sail in prod either.

1 like
vincent15000's avatar

@Foks Sure but he needs to install supervisor on docker using sail, so I thought it was useful to remember that sail shouldn't be used in production.

stratboy's avatar

@vincent15000 Thank you very much. Being a newbe (even if 'playing' with Laravel since several years now, on my spare time, but now going to really learn it) I never deployed a Laravel app developed with Sail. I deployed something very basic only just to try. So I'm not sure of what you're exacty saying above:

  • First, "Supervisor is already installed via Sail": but then, how can I assign a config to run artisan queue:work, where do I put it, how do I tell the program to use it. If I inspect a little the sail container, I can see 2 configs:

ENV SUPERVISOR_PHP_COMMAND=/usr/bin/php -d variables_order=EGPCS /var/www/html/artisan serve --host=0.0.0.0 --port=80 ENV SUPERVISOR_PHP_USER=sail

  • "Sail should not be used in production.": do you mean that all my current docker configuration is totally useless and I will not be able to use it for production? Should I abandon Sail and try to dockerize my app from scratch to work to something professional and ready to be moved in production?

  • Production docker-compose.yml: do I have to copy it as is, or should I customize some params (I guess yes, but what are they)? And, so if I use a docker-compose I don't need an Dockerfile? And vice-versa? For what I studied till now, I thought a Dockerfile is to run commands, and a docker-compose is to orchestrate/configurate more than one container. But again, I'm kinda newbe. Ok, I must further study Docker (a lot :P).

  • But in my current docker-compose a Dockerfile is mentioned, but only on the sail service (but there isn't any Dockerfile in my root dir):

services:
    laravel.test:
        build:
            context: ./vendor/laravel/sail/runtimes/8.3
            dockerfile: Dockerfile

Excuse me for all these questions...

stratboy's avatar

@vincent15000 I tried to do what's in the first article and it work. Just to be sure, when the author say:

Enabling Supervisor control To enable supervisorctl and the web UI, we need to modify the default supervisord.conf file that ships with Laravel Sail. Add the following lines to your supervisord.conf:

Does he mean ./vendor/laravel/sail/runtimes/8.3/supervisord.conf in the app root?

I modded that one and it worked after a rebuild, but not sure its the right one. And I'm wandering: will it be ovewritten when updating Laravel?

1 like
stratboy's avatar
stratboy
OP
Best Answer
Level 5

I'll answer by myself here: I've found out that in fact, if you want to customize the Sail installation, you can publish its config files:

sail artisan sail:publish

This command will create a docker dir in the project root and update accordingly the context in docker-compose.yaml. Then you can simply modify the config files, including Dockerfile, supervisord.conf and php.ini. Then just run sail build --no-cache.

1 like
vincent15000's avatar

@stratboy When you have your mouse over a comment, you see a button appearing at the bottom of the comment to assign it the best answer.

LaryAI's avatar
Level 58

To install and configure Supervisor in a Docker container for a Laravel application, you can follow these steps. This approach involves creating a custom Dockerfile and configuring Supervisor to manage your Laravel queue workers.

Step 1: Create a Dockerfile

First, create a Dockerfile in the root of your Laravel project. This Dockerfile will extend the official PHP image, install Supervisor, and set up the necessary configuration.

# Use the official PHP image as the base image
FROM php:8.0-fpm

# Install system dependencies
RUN apt-get update && apt-get install -y \
    supervisor \
    && docker-php-ext-install pdo pdo_mysql

# Install Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

# Set the working directory
WORKDIR /var/www

# Copy the existing application directory contents
COPY . /var/www

# Install application dependencies
RUN composer install --no-dev --optimize-autoloader

# Copy the Supervisor configuration file
COPY ./docker/supervisord.conf /etc/supervisor/conf.d/supervisord.conf

# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD ["php-fpm"]

Step 2: Create Supervisor Configuration

Create a directory named docker in the root of your Laravel project and add a supervisord.conf file inside it. This file will contain the Supervisor configuration for managing the Laravel queue workers.

[supervisord]
nodaemon=true

[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/artisan queue:work --sleep=3 --tries=3 --timeout=90
autostart=true
autorestart=true
numprocs=1
redirect_stderr=true
stdout_logfile=/var/www/storage/logs/worker.log

Step 3: Update Docker Compose Configuration

If you are using Docker Compose, update your docker-compose.yml file to build the custom Docker image and run Supervisor.

Step 4: Build and Run the Docker Containers

Finally, build and run your Docker containers using Docker Compose.

docker-compose up --build

This setup will build a custom Docker image for your Laravel application, install Supervisor, and configure it to manage your Laravel queue workers. The supervisord.conf file specifies that Supervisor should run the php artisan queue:work command to process the queue jobs.

By following these steps, you should be able to run Supervisor in a Docker container for your Laravel application without disrupting your existing development environment.

Please or to participate in this conversation.