To run a Laravel application with FrankenPHP, queues, and scheduler commands within Docker, you can follow these steps:
- 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"]
-
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
-
Scheduler Entrypoint: For the scheduler, you'll need an entrypoint script that runs the scheduler command. Create a file named
scheduler-entrypoint.shin adockerdirectory:
#!/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
-
Run Docker Compose: With the
Dockerfileanddocker-compose.ymlin 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.