Create dockerfile config supervisor for laravel

  • November 23, 2024
  • 73

Tạo dockerfile để chạy dự án laravel và thiết lập supersior trong docker container để giám sát queue và schedule trong laravel. Đầu tiên chúng ta sẽ tạo file Dockerfile


FROM php:8.1-fpm

RUN apt-get update

RUN apt-get install -y libzip-dev zip unzip libpng-dev libjpeg-dev zlib1g-dev procps vim supervisor

# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# Install php ext
RUN docker-php-ext-configure gd  --with-jpeg
RUN docker-php-ext-install pdo pdo_mysql zip exif gd

# Copy config supervisor COPY supervisord.conf /etc/supervisor/ # Copy and set permission for file sh
COPY docker-entrypoint.sh /usr/local/bin/ RUN chmod +x /usr/local/bin/docker-entrypoint.sh RUN usermod -u 1000 www-data WORKDIR /var/www/html COPY . /var/www/html RUN rm -f composer.lock RUN rm -rf ./storage/logs/laravel.* RUN chmod -R 777 storage RUN composer install
# Open port 9000 on container EXPOSE 9000

# Run file sh when container run ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"]

Tạo file config supervisor supervisord.conf


[supervisord]
nodaemon=true

[program:laravel_queue]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/html/artisan queue:work --timeout=300
autostart=true
autorestart=true
user=www-data
numprocs=4
redirect_stderr=true
stdout_logfile=/var/www/html/storage/logs/app-queue.log
stdout_logfile_maxbytes=0

[program:laravel_schedule]
process_name=%(program_name)s
command=php /var/www/html/artisan schedule:run
autostart=true
autorestart=true
user=www-data
numprocs=1
startsecs=0
startretries=3
redirect_stderr=true
stdout_logfile=/var/www/html/storage/logs/app-schedule.log
stdout_logfile_maxbytes=0

Tạo file docker-entrypoint.sh để chạy suppervisor sau khi container được chạy


#!/bin/bash

php-fpm &
/usr/bin/supervisord -c /etc/supervisor/supervisord.conf

Với các file cấu hình trên chúng ta sẽ build được một container sử dụng linux, cài đặt php 8.1 fpm, sử dụng supervisor để giám sát queue và schedule trong laravel.