How to use Laravel queue listen in Docker

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

How to Run Laravel Queue Listeners Automatically in Docker: A Robust Solution

As developers move from local development environments to containerized deployments, a common hurdle emerges: ensuring long-running background processes, like a Laravel queue listener, persist reliably within the ephemeral nature of Docker containers. When running locally, commands like php artisan queue:listen database work perfectly because the terminal session is always active. However, when you containerize your application, this persistence breaks down, as the process stops or is lost when the container restarts or crashes.

This post will dive deep into why simply running the command inside a Dockerfile doesn't suffice and provide robust, automatic methods for ensuring your Laravel queue listener runs continuously within your Docker environment.

The Persistence Problem in Docker

The issue you are facing stems from how Docker manages processes. When you execute a command directly (like docker run my-app), Docker starts the container based on that command. If that command finishes executing, or if the main process exits, the container stops, and there is no inherent mechanism for Docker to automatically restart it with the listener running in the foreground.

Your attempts using docker exec are manual—they execute a command inside an already running container. To achieve true automation, we need to integrate the queue listening process directly into the container’s lifecycle management, ideally via the ENTRYPOINT or CMD.

Solution 1: Using Entrypoint Scripts for Persistent Services

The most idiomatic and robust way to handle persistent services in Docker is by defining a custom ENTRYPOINT script. This script acts as the primary execution point for the container, ensuring that whatever command it executes is kept running in the foreground until explicitly stopped.

Instead of just executing the artisan command, we need a wrapper process that keeps the listener alive indefinitely. While Laravel itself doesn't offer a built-in daemon mode for queue listening that runs infinitely by default, we can leverage standard shell practices to achieve this persistence.

Here is how you structure your Dockerfile and entrypoint:

Step 1: Create the Entrypoint Script

Create a script (e.g., docker-entrypoint.sh) that will handle starting the queue listener and keeping the container alive.

#!/bin/sh
# docker-entrypoint.sh

# Ensure we run the main command in the foreground
exec "$@"

Step 2: Update Your Dockerfile

In your Dockerfile, copy this script and set it as the entrypoint, ensuring PHP-FPM or your web server is still available for other tasks if needed (though for a pure queue worker, we focus on the listener).

FROM php:8.2-fpm

# Copy the custom entrypoint script into the container
COPY docker-entrypoint.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/docker-entrypoint.sh

# Set the entrypoint to our script
ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"]

# Define the default command (this will be passed to the entrypoint script)
CMD ["php", "artisan", "queue:listen", "database"]

Step 3: Building and Running the Container

When you run your container, the ENTRYPOINT script takes over. Because we used exec "$@" in our script, the shell command passed to it (php artisan queue:listen database) runs as the main process of the container. This ensures that Docker monitors this foreground process, and if it stops, the container exits (which is often desired for worker processes), but crucially, the process starts automatically upon container launch and remains active until explicitly shut down.

This approach aligns perfectly with best practices for service management in Docker, ensuring that your application's background workers are managed reliably, which is essential when managing complex systems like those built on Laravel frameworks from laravelcompany.com.

Conclusion: Automation Through Process Management

Relying on manual docker exec commands for persistent tasks is not scalable or reliable. By implementing a custom entrypoint script, you shift the responsibility of process management into the container definition itself. This method guarantees that whenever a new instance of your Laravel application starts in Docker, the queue listener automatically initializes and runs in the foreground, providing the persistence required for robust background job processing. Embrace these principles to build resilient, automated microservices on Docker.