Using Docker Compose's YAML Features to Manage Laravel Workers Efficiently

Table of contents

Docker Compose provides an excellent mechanism for defining and running multi-container Docker applications. When it comes to orchestrating multiple services, especially in scenarios like running Laravel workers, things can get repetitive. However, Docker Compose combined with YAML’s powerful features can simplify this process.

In this post, we’ll explore how to manage multiple Laravel queue workers using Docker Compose while keeping our configuration DRY (Don’t Repeat Yourself).

Introducing YAML Anchors and Aliases

As discussed in my previous post YAML, the language Docker Compose files are written in, has a feature called “Anchors and Aliases”. This allows us to define a block of configuration and reuse it in multiple places.


x-shared-configuration: &shared-config
  image: my-laravel-image
    - APP_ENV=production

In this snippet, &shared-config is an anchor pointing to the x-shared-configuration block.

Applying Shared Configuration in Docker Compose

Consider a typical Laravel setup where we want one container to run the application and several others to process different queues. We can avoid repeating the common configuration for each service by using our anchor:

    <<: *shared-config
      - "8000:80"

    <<: *shared-config
    command: ["php", "artisan", "queue:work", "--queue=email"]

    <<: *shared-config
    command: ["php", "artisan", "queue:work", "--queue=notifications"]

Here, the <<: *shared-config syntax means we’re injecting everything from our x-shared-configuration block into the service.


Maintainability: If you need to update a shared setting, such as changing the image version, you can do it in one place, and it will propagate to all services using the shared configuration. Readability: The Docker Compose file becomes less cluttered and easier to understand. Scalability: It’s easy to add new workers or services without duplicating a lot of content.


Docker Compose and YAML’s advanced features can make managing complex setups, like multiple Laravel workers, a breeze. By leveraging these capabilities, we can create efficient, maintainable, and clean configurations that scale with our needs.

© 2024 Virendra Giri