Featured image of post Using laravel-s in Docker (Including Queue Usage)

Using laravel-s in Docker (Including Queue Usage)

A summary of previous experiences with laravel-s

Environment Configuration

FROM phpswoole/swoole:php7.4-alpine

# A script for installing PHP extensions
COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/local/bin/

RUN install-php-extensions pcntl redis pdo_mysql

WORKDIR /var/www
COPY . .
RUN chmod -R 0777 storage && \
    chmod -R 0777 bootstrap/cache && \
    composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/ && \
    composer install --optimize-autoloader --no-dev && \
    php artisan config:cache && \
    php artisan route:cache && \
    php artisan view:cache && \
    php artisan laravels publish --no-interaction

CMD ["php", "bin/laravels", "start", "--env=product"]

Deployment

## Build the image
docker build . -t demo-image
## Run container
docker run -d -p 80:5200 --name=demo-service demo-image

Development

Mount local directory for development

## Run container (/mnt/d/laravels-demo should be your project path)
docker run -d -it -p 80:5200 -v /mnt/d/laravels-demo:/var/dev  --name=demo-service -w /var/dev demo-image /bin/sh

Auto-reload on code changes

## Enter container
docker exec -it demo-service /bin/sh
# Install inotify
install-php-extensions inotify
## Modify .env
LARAVELS_INOTIFY_RELOAD=true
## Start with auto-reload
php bin/laravels start

Model annotations and code navigation

## Enter container
docker exec -it demo-service /bin/sh
## Install dependency
composer install barryvdh/laravel-ide-helper
## Generate model annotations
php artisan ide-helper:models --dir="app/Models" --write  --reset

Debugging

Using dump

## Enter container
docker exec -it demo-service /bin/sh
## Install dependency
composer install beyondcode/laravel-dump-server
## Capture dump outputs
php artisan dump-server

Using Swoole Tracker 3.1

  • Installation guide: https://wenda.swoole.com/detail/107688
  • Place .so file in container and enable extension in php.ini
  • Add trackerHookMalloc function and run: php -r "trackerAnalyzeLeak();"

Queue (Using Supervisor)

Configuration

  • Create queue.Dockerfile:
FROM phpswoole/swoole:php7.4-alpine

RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories && cat /etc/apk/repositories

COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/local/bin/
RUN install-php-extensions pcntl redis pdo_mysql zip

WORKDIR /var/www
COPY . .
RUN chmod -R 0777 storage && \
    chmod -R 0777 bootstrap/cache && \
    composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/ && \
    composer install --optimize-autoloader --no-dev

## Install Supervisor
RUN apk add supervisor

## Supervisor configuration
RUN mkdir /etc/supervisor.d/ &&  echo $'\n\
[program:laravel-worker] \n\
process_name=%(program_name)s_%(process_num)02d \n\
command=/usr/local/bin/php /var/www/artisan queue:work --sleep=3 --tries=3 \n\
numprocs=12 \n\
autostart=true \n\
autorestart=true \n\
redirect_stderr=true \n\
stdout_logfile_maxbytes=10MB \n\
stdout_logfile=/var/www/storage/logs/queue.log \n'\
> /etc/supervisor.d/laravel-worker.ini

CMD ["/usr/bin/supervisord", "--nodaemon", "-c", "/etc/supervisord.conf"]
  • Build queue image: docker build . -f queue.Dockerfile -t laravel-worker
  • Run container: docker run -d --name=laravel-worker-service laravel-worker

Queue Management

  • Graceful restart:
php artisan queue:restart
  • Code update methods:
    1. Copy code to container and restart queue
    2. Use volume mapping
    3. Rebuild container image

Tips

  • New queue jobs don’t require restart

Important Notes

  • Set listen_ip to 0.0.0.0 in config/laravels.php
  • Enable handle_static for static assets
  • Check register_providers and cleaners configurations
  • Maintain proper file permissions for storage and cache directories