Nika Kurashvili
Nika Kurashvili

Reputation: 6474

How to use Laravel queue listen in Docker

When running on localhost, I use:

php artisan queue:listen database

which listens and acts as queue. This works fine until I want to Dockerize my Laravel app. I have already done this, so cannot run the command so that container would have queue listener.

I read that I can do the following:

docker exec -t (containername) php artisan queue:listen

I would like to find another way of doing this, because if container dies and restarts itself, this command won't get executed, and I can't just watch when container dies so that I can do it by hand. I want an automatic way. each container who starts has to have this command automatically running. I tried in Dockerfile, but doesn't have any effect.

Upvotes: 3

Views: 13954

Answers (7)

Create another container with the same image.

Here is my docker-composer.yml file

version: "3.7"

services:
    php:
        build:
            args:
                user: sammy
                uid: 1000
            context: .
            dockerfile: dockerfiles/php.dockerfile
        image: sarah-php
        container_name: sarah-php
        user: 'sammy'
        restart: unless-stopped
        working_dir: /var/www/
        volumes:
            - ./:/var/www/
        networks:
            - sarah
        depends_on:
            - redis
            - mysql

    queue:
        image: sarah-php
        container_name: sarah-queue
        restart: always
        working_dir: /var/www/
        command: php artisan queue:work
        depends_on:
            - php
        volumes:
            - ./:/var/www
        networks:
            - sarah

It should be noted that for the command to work correctly, you need to set work_dir first.

Upvotes: 3

You can use container inheritance in docker-compose. In my case "app" container implements php:7.4.10-fpm. If you run the "php artisan queue:work" command inside "app", then it will not work (I don’t know why, I think because the command hangs the terminal process), but if inherited, then the command is executed conditionally in a separate terminal and does not break anything

app:
 &base-app
 build:
  context: './docker/main/app'
  container_name: ${DOCKER_COMPOSE_PROJECT_NAME}_app

worker:
 <<: *base-app
 container_name: ${DOCKER_COMPOSE_PROJECT_NAME}_worker
 command: php artisan queue:work

Upvotes: 1

Luka Tarkhnishvili
Luka Tarkhnishvili

Reputation: 44

You should create supervisorctl command.

how to install and create supervisorctl command?

laravel docs about supervisor

my laravel-worker.conf file:

[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=docker exec -t <docker container id (you can find it with command: docker ps)> php artisan queue:work database --sleep=3 --tries=3 --max-time=3600
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
;user=root
numprocs=2
redirect_stderr=true
stdout_logfile=/home/itsoft/it-software/worker.log
stopwaitsecs=3600

Upvotes: -1

PHP Worm...
PHP Worm...

Reputation: 4224

Not sure this is a correct way. But I am doing it for a long time and it's working well for me.

Go to the project folder. Run php artisan queue:listen

Before running this command add the current user to the www-data group.

Upvotes: 1

Bira
Bira

Reputation: 5506

Create a new service as bellow and run it on the same image.

Here is my docker-composer.yml file, This will help to run the queues in AWS Fargate environments as well

version: "3.4"
services:
  www:
    build: 
        context: .
        dockerfile: ./docker/Dockerfile
    image: php-laravel:latest
    ports: 
        - "8008:80"
    volumes:
        - ./src:/var/www/html/
    networks:
        - default
  php-queue:
    restart: always
    image: php-laravel:latest
    command: php artisan queue:work
    volumes:
        - ./src:/var/www/html/
volumes:
    default:

Upvotes: 6

Ing. Luca Stucchi
Ing. Luca Stucchi

Reputation: 3277

If you need to have more than a single worker, you can set it up with supervisor, as explained in the Laravel Queue Page https://laravel.com/docs/5.7/queues#supervisor-configuration

Upvotes: -2

user10686911
user10686911

Reputation:

To execute a command after startup, add in your Dockerfile RUN cd /my/project/root && php artisan queue:listen

Also, you can use vessel to work with Docker, it's already preconfigured for Laravel so that you can simply do something like ./vessel artisan <cmd>

Upvotes: 1

Related Questions