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
I would do something like this using supervisord:
Dockerfile:
supervisord.conf:
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
This solution also works on Podman Compose.