cwhite92 / blog-comments

0 stars 0 forks source link

17-frankenphp #6

Open utterances-bot opened 8 months ago

utterances-bot commented 8 months ago

FrankenPHP and Laravel Octane with Docker

Just random stuff.

https://chriswhite.is/coding/frankenphp-and-laravel-octane-with-docker

mikeziri commented 8 months ago

Do you, by any chance, have a container image on Docker Hub with this setup to try out? Nice job!

cwhite92 commented 8 months ago

Hey @mikeziri, unfortunately not, but it should be pretty trivial to build the container in your project. Fundamentally it's just two files: the Dockerfile and docker-compose.yml, both of which you can copy paste from this post. And then when you docker-compose up it should build the image for you. If you need to make additional changes to the image (like installing more PHP extensions), you can use docker-compose build web to rebuild the image. :)

luigirossidev commented 8 months ago

Thanks for this post! In this configuration, how i can set a laravel's queue dispatcher? Do you think it is enough to add the same container with the "php artisan queue:work" command?

AratKruglik commented 7 months ago

Great post, thanks! And how to confess HTTPS ports? What if I need HTTPS port different then 443?

AratKruglik commented 7 months ago

@luigirossidev I did it via supervisor daemon like that:

Dockerfile:

#FROM --platform=linux/amd64 dunglas/frankenphp
FROM dunglas/frankenphp

# add additional extensions here:
RUN install-php-extensions \
    pdo_mysql \
    gd \
    intl \
    imap \
    bcmath \
    redis \
    curl \
    exif \
    hash \
    iconv \
    json \
    mbstring \
    mysqli \
    mysqlnd \
    pcntl \
    pcre \
    xml \
    libxml \
    zlib \
    zip

RUN apt update && apt install -y supervisor

#RUN curl -sS https://getcomposer.org/installer -o composer-setup.php && \
#    php composer-setup.php --install-dir=/usr/local/bin --filename=composer && \
#    rm composer-setup.php

COPY . /app

COPY ./docker/frankenphp/supervisord.conf /etc/supervisor/conf.d/supervisord.conf

WORKDIR /app

EXPOSE 80 443

CMD ["supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]

and supervisor configs

[supervisord]
user=root
nodaemon=true
logfile=/dev/stdout
logfile_maxbytes=0
pidfile=/var/run/supervisord.pid

[program:octane]
command=php /app/artisan octane:frankenphp --host=127.0.0.1 --port=8000 --workers=8 --max-requests=10000
autostart=true
autorestart=true
priority=2
stdout_events_enabled=true
stderr_events_enabled=true
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0

[program:queue-runner]
command=php /app/artisan queue:work --tries=3 --timeout=90 --sleep=3 --daemon
autostart=true
autorestart=true
priority=3
stdout_events_enabled=true
stderr_events_enabled=true
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0

[program:websocket]
command=php /app/artisan websockets:serve
autostart=true
autorestart=true
priority=3
stdout_events_enabled=true
stderr_events_enabled=true
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
cwhite92 commented 7 months ago

Hey @luigirossidev, sorry for the late reply.

FrankenPHP is just meant to be a web server, not something that runs your queues. Just like you can't use nginx/php-fpm to run your queue, you can't use FrankenPHP either.

I'd recommend building a separate image entirely to run your queue, and have it use the official PHP CLI Docker images as a base, like FROM php:cli. Then your container's ENTRYPOINT can be php artisan queue:work.

@AratKruglik's solution with supervisor also works, but it combines both serving HTTP traffic and running queue jobs into one container. That might be fine for your use case, but usually you want to split them out for easier scaling. For example if your app gets little traffic but executes a lot of queue jobs, you can deploy more of your queue:work containers without also deploying more of your web server containers. Docker containers are ideally meant to do "one job", and combining jobs into one container, while possible, isn't usually recommended.

AratKruglik commented 7 months ago

@cwhite92 do you mean push some queue jobs to Redis for example and read them from separate queue containers? Make sense, thanks if you mean this stuff.

BTW do you have some Caddyfile example to use it with Octane and listen 80 and 443 ports and redirect traffic to 8000 Octane port? Because I guess I have trouble with it (

ramonmalcolm10 commented 7 months ago

Any example with a production build?

maxcelos commented 4 months ago

Does this approach brings any performance benefits compared to just run octane without docker?