laradock 使用 php‐worker - daniel-qa/Laradock GitHub Wiki


php-worker 是指用于处理后台任务的 PHP 容器,例如 Laravel 队列任务(queue)

通常,php-worker 的定义类似于 workspace 的定义,但是它使用了一个不同的 Docker 镜像,其中包含了队列处理器(如 supervisord)。

一个简单的 docker-compose.yml 示例如下所示:

version: '3'

services:
  workspace:
    build:
      context: ./workspace
      dockerfile: Dockerfile
    container_name: project_workspace
    volumes:
      - ../:/var/www
    networks:
      - laravel
    # Other configurations for the workspace service go here

  php-worker:
    build:
      context: ./php-worker
      dockerfile: Dockerfile
    container_name: project_php-worker
    volumes:
      - ../:/var/www
    networks:
      - laravel
    # Other configurations for the php-worker service go here

networks:
  laravel:
    driver: bridge

php-worker 服务的 Dockerfile

確保在 Dockerfile 中,你使用了适当的基础镜像,并安装了 Laravel 需要的 PHP 扩展和队列处理器(如 supervisord)。示例如下:

# Use an appropriate PHP version image, for example:
FROM php:7.4-fpm

# Install required PHP extensions (add any others your Laravel app needs)
RUN docker-php-ext-install pdo pdo_mysql

# Install Supervisor for queue workers
RUN apt-get update && apt-get install -y supervisor

# Copy Supervisor configuration
COPY ./path/to/supervisord.conf /etc/supervisor/conf.d/supervisord.conf

# Other configurations and setup for your php-worker container go here

# Start Supervisor as the main process
CMD ["/usr/bin/supervisord", "-n", "-c", "/etc/supervisor/conf.d/supervisord.conf"]

設置 Supervisor 配置文件(supervisord.conf),其中包含 Laravel 队列 worker 的设置

这将确保在容器中正确启动队列处理器。示例如下:

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

注意:你需要根据自己的项目结构和需求进行适当的配置。

  • 令启动 workspace php-worker
docker-compose up -d workspace php-worker
  • 进入到 workspace 容器中,然后在 Laravel 项目目录中运行队列 worker:
docker-compose exec workspace bash
php artisan queue:work

现在,Laravel 的队列 worker 将在 php-worker 容器中运行,并处理队列中的后台任务。

这是一个基本的设置,可以帮助你在 laradock 中使用 php-worker 容器来处理 Laravel 队列任务。