1

I have a container with a nginx, mailhog, redis and PHP image. All these images are on the same network. I run Laravel on the PHP image. I want to make use of the Job queue that laravel has, but I am struggling to run the queue in the PHP image. I've looked at all the examples but it seems my lack of understanding of docker is causing me to not ask the right question

Below is my docker-compose.yml

version: '3'

networks:   
    devnet:
        external: true
    
services: 

    # lightweight web-server:
    nginx: 
        image: nginx:stable-alpine
        container_name: lar-nginx
        ports:
            - 8080:80
            - 4040:443
        volumes: 
            - ./:/var/www
            - ./run/nginx:/etc/nginx/conf.d
            - ./local/certs:/etc/nginx/certs
        depends_on:
            - php
        networks:
            - devnet
    
    # server-side scripting engine
    php:
        build:
            context: .
            dockerfile: Dockerfile
        container_name: lar-php
        volumes:
            - ./:/var/www
        ports:
            - "9000:9000"
        networks:
            - devnet

    # caching server:
    redis:
        image: redis:latest
        container_name: lar-redis
        ports:
            - "6379:6379"
        networks:
            - devnet        
    
    # development email catch-all server & client:
    mailhog:
        image: mailhog/mailhog:latest
        container_name: lar-mailhog
        ports:
            # imap port for send mail
            - "1025:1025"
            # www mailhog ui
            - "8025:8025"
        networks:
            - devnet            

Dockerfile


FROM php:7.4-fpm

RUN apt-get update
RUN apt-get -y install curl gnupg cron
# RUN curl -sL https://deb.nodesource.com/setup_12.x  | bash -
# RUN apt-get -y install nodejs
# RUN npm install

# Install other required PHP extensions and unix utils:
RUN apt-get update && apt-get install -y libmcrypt-dev \
    mariadb-client libmagickwand-dev libonig-dev \
    libzip-dev libcurl4-openssl-dev redis-server \ 
    zlib1g-dev wget git \
    --no-install-recommends \
    # && pecl install imagick 
    # && docker-php-ext-enable imagick 
    && docker-php-ext-install pdo_mysql \ 
    && docker-php-ext-install mbstring \
    && docker-php-ext-install zip \
    && docker-php-ext-install xml \
    && docker-php-ext-install curl \
    && docker-php-ext-install gd \
&& docker-php-ext-install soap

# Configure PHP internal vars:
ENV PHP_MEMORY_LIMIT=256M

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

# Install php apcu pecl package:
RUN pecl install apcu && docker-php-ext-enable apcu

# Install php redis pecl package:
RUN pecl install redis && docker-php-ext-enable redis
# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

# Install extensions
RUN docker-php-ext-install pdo_mysql zip exif pcntl

# Permissions for Laravel
RUN chown -R www-data:www-data /var/www
RUN chmod -R 777 /var/www

COPY entrypoint.bash /usr/sbin
RUN chmod a+x /usr/sbin/entrypoint.bash
ENTRYPOINT /usr/sbin/entrypoint.bash

entrypoint.bash

#!/bin/bash
# turn on bash's job control
set -m
# Start the "main" PHP process and put it in the background
php-fpm &
# Start the helper crond process
crond
# now we bring the primary process back into the foreground
fg %1

In normal server(lamp) environment its pretty simple to work with cronjobs and queue but I dont know how to start up the queue. php artisan queue:work in the php image returs There are no commands defined in the "queue:" namespace. Did you mean this? queue

Running it in tinker \Queue::pushON('new', new App\Jobs\PublishingClass(array('foo'=>1,'foobar'=>783,'foobarfoo'=>33))); show the job gets processed but I need to do it with a process running in the background

petrivoges
  • 79
  • 1
  • 10

2 Answers2

1

The most simple way is to call with the use of Tinker

It's Laravel command using for debugging, use it by running below command from from project root

php artisan tinker

To dispatch job on a specific queue from tinker

\Queue::pushON('rms', new App\Jobs\UpdateRMS());

first parameter - Queue name

second parameter - job name

Dispatch multiple jobs at once to a specific queue

\Queue::bulk([new App\Jobs\UpdateRMS(), new App\Jobs\UpdateRMS()], null, 'rms');
  • I dont want to use tinker. That is not a reflection of our live environment – petrivoges May 18 '22 at 16:00
  • Ahhh... I misunderstood your question ... As I remember in production I've used a supervisor for queues, did you try this one? Take a look at this post: https://stackoverflow.com/a/44501632/13678053 – Roman Slisarenko May 18 '22 at 16:49
  • I also want to preferably stay away from installing supervisor. I want this to be as lean as possible without any dependencies – petrivoges May 18 '22 at 19:36
  • Unfortunately, I can't suggest a better option ... Don't be afraid to use a supervisor, I understand that this is an additional dependency, but I don't see anything wrong with it and it's in line with best practices, in my thought. – Roman Slisarenko May 19 '22 at 18:49
0

You can use this docker image, you don't need to configure the schedule, it's already implemented, with differents php expansions like redis, Rdkafka. Follow this link: https://hub.docker.com/r/jkaninda/laravel-php-fpm

https://github.com/jkaninda/laravel-php-fpm

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 22 '22 at 05:23