Skip to content

Add Docker Swarm support for replicas and task optimization #155

@jhorlima

Description

@jhorlima

Hello!

First of all, I'd like to congratulate you on the excellent work on this repository. The setup for running Laravel Octane with Docker is very well-organized and comprehensive.

I'm opening this issue to suggest the possibility of adding support for Docker Swarm. The idea would be to enable using docker stack deploy to bring up the services, which would allow for simple, native management of application replicas using Docker, thereby improving scalability and resilience.

Benefits of Replicas for Workers:

A key area that would greatly benefit from Swarm is the ability to scale Laravel workers (horizon or the default worker). With replica configuration, we could easily increase or decrease the number of running workers based on job processing demand, efficiently distributing the load across the cluster nodes.

Dedicated Service for Migrations:

For a deployment in a multi-replica environment to work correctly, running php artisan migrate requires special attention. Executing the migration within the main app service would cause conflicts, as each replica would attempt to run the command simultaneously.

An effective solution would be to create an isolated service dedicated solely to running migrations. This service would have no replicas and would run only once during deployment to ensure the database is up-to-date before the app and worker services start.

Here is an example of how this could be structured in the docker-compose.yml file:

services:
  # Service to run migrations, with no replicas
  migrate:
    image: <your-app-image>
    command: php artisan migrate --force
    # Deploy configuration to ensure it runs only once
    deploy:
      replicas: 1
      restart_policy:
        condition: on-failure

  # Application service with multiple replicas
  app:
    image: <your-app-image>
    # ... other configurations
    deploy:
      replicas: 3 # Example with 3 replicas
      update_config:
        parallelism: 1
        delay: 10s
      restart_policy:
        condition: on-failure
    depends_on:
      - migrate # Ensures migration runs first

  # Workers also with replicas
  worker:
    image: <your-app-image>
    # ... other configurations
    deploy:
      replicas: 5 # Example with 5 workers

Would you be open to this suggestion? I believe these additions would make the repository even more powerful for large-scale production environments. If there is interest, I would be happy to help review a Pull Request with these changes.

Thank you for your consideration!

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions