skip to Main Content

I want to run my docker compose and not have to manually execute php artisan queue:work to start my worker. I created 2 service o the first one is my php fpm and the second one is my queue serice which has the same image and the same volume.

  app:
    build:
      context: php
    container_name: laravel_app
    volumes:
      - ./laravel:/var/www/html
    networks:
      - laravel

  queue:
    build:
      context: php
    volumes:
      - ./laravel:/var/www/html
    command: ['/bin/sh', '-c', 'php artisan queue:work']
    depends_on:
      - redis
      - app

The service is up but don’t receive any job even if i exec manually inside the container artisan queue:work i have "INFO Processing jobs from the [default] queue." but i don’t receive any job. However, when i exec manually the same command inside the app container it works and they have the same volume.

The problem is the connection with redis when the queue service exec queue:work i have this error message in my laravel.log:

    [2024-09-04 19:40:48] local.ERROR: php_network_getaddresses: getaddrinfo for redis failed: Name or service not known {"exception":"[object] (RedisException(code: 0): php_network_getaddresses: getaddrinfo for red
is failed: Name or service not known at /var/www/html/vendor/laravel/framework/src/Illuminate/Redis/Connectors/PhpRedisConnector.php:159)

my redis config is in my .env file present in laravel folder: REDIS_HOST=redis REDIS_PASSWORD=null REDIS_PORT=6379

2

Answers


  1. I would do something like this using supervisord:

    Dockerfile:

    FROM php:8.2-fpm
    
    # do watherever you need to do to setup for laravel
    # such as copying files or installing extensions
    
    
    
    # Install Supervisor
    RUN apt-get update && apt-get install -y supervisor
    
    COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
    
    CMD ["/usr/bin/supervisord"]
    
    

    supervisord.conf:

    
    
    [program:laravel-worker]
    process_name=%(program_name)s_%(process_num)02d
    command=php /var/www/html/artisan queue:work --sleep=3 --tries=3 --timeout=90
    autostart=true
    autorestart=true
    numprocs=1
    redirect_stderr=true
    stdout_logfile=/var/www/html/storage/logs/worker.log
    
    
    Login or Signup to reply.
  2. Here is a two-container Docker Compose solution using a dedicated worker container to work the queue which uses YAML anchors and aliases to avoid markup redundancy. The :z option tells Docker that the volume content will be shared between containers. May request changes if not using Octane:

    compose.yaml

    x-base: &base
      # Alternatively your image
      build:
        context: .
      # Shared volume between main container and worker container
      volumes:
        - .:/app:z
    
    services:
      worker:
        <<: *base
        name: worker
        entrypoint: php artisan queue:work
    
      main:
        <<: *base
        name: main
        entrypoint: php artisan octane:frankenphp
        ports:
          - 8000:8000
    

    This solution also works on Podman Compose.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search