tudorr89's avatar

How do you guys wrap your laravel app around docker?

Hi guys!

I've recently stumbled upon frankenphp and I wanted to ask you guys how can this be run in a project having queues and scheduler commands? I'm currently running octane wrapped with https://github.com/exaco/laravel-octane-dockerfile and created a container for app/horizon/scheduler.

Any tips would be highly appreciated! Happy holidays!

0 likes
2 replies
LaryAI's avatar
Level 58

To run a Laravel application with FrankenPHP, queues, and scheduler commands within Docker, you can follow these steps:

  1. Create a Dockerfile: You'll need to create a Dockerfile that sets up your PHP environment with all the necessary extensions and FrankenPHP. Here's an example Dockerfile:
FROM php:8.1-fpm

# Install system dependencies
RUN apt-get update && apt-get install -y \
    libonig-dev \
    libxml2-dev \
    zip \
    unzip \
    git \
    curl

# Install PHP extensions
RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd

# Get latest Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

# Set working directory
WORKDIR /var/www

# Copy existing application directory contents
COPY . /var/www

# Install FrankenPHP
RUN composer require frankenphp/frankenphp

# Expose port 80
EXPOSE 80

# Start FrankenPHP
CMD ["vendor/bin/frankenphp", "start", "--host=0.0.0.0", "--port=80"]
  1. Docker Compose: Use Docker Compose to manage your application container along with other services like queues and schedulers. Here's an example docker-compose.yml:
version: '3.8'

services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: laravel_app
    restart: unless-stopped
    working_dir: /var/www
    volumes:
      - ./:/var/www
    networks:
      - laravel

  scheduler:
    image: php:8.1-cli
    container_name: laravel_scheduler
    depends_on:
      - app
    volumes:
      - ./:/var/www
    entrypoint: /var/www/docker/scheduler-entrypoint.sh
    networks:
      - laravel

  queue:
    image: php:8.1-cli
    container_name: laravel_queue
    depends_on:
      - app
    volumes:
      - ./:/var/www
    entrypoint: php /var/www/artisan queue:work
    networks:
      - laravel

networks:
  laravel:
    driver: bridge
  1. Scheduler Entrypoint: For the scheduler, you'll need an entrypoint script that runs the scheduler command. Create a file named scheduler-entrypoint.sh in a docker directory:
#!/bin/sh
cd /var/www
while true; do
  php artisan schedule:run --verbose --no-interaction &
  sleep 60
done

Make sure to give execute permissions to the script:

chmod +x docker/scheduler-entrypoint.sh
  1. Run Docker Compose: With the Dockerfile and docker-compose.yml in place, you can start your containers using Docker Compose:
docker-compose up -d --build

This will build your application image, start the application with FrankenPHP, and also start separate containers for the scheduler and queue worker. The scheduler service runs the scheduler every minute, and the queue service processes jobs using Laravel's queue worker.

Remember to adjust the PHP version, extensions, and other configurations to match your application's requirements.

Please or to participate in this conversation.