Skip to content

Instantly share code, notes, and snippets.

@Baldinof
Last active February 13, 2024 22:52
Show Gist options
  • Save Baldinof/8af17f09c7a57aa468e1b6c66d4272a3 to your computer and use it in GitHub Desktop.
Save Baldinof/8af17f09c7a57aa468e1b6c66d4272a3 to your computer and use it in GitHub Desktop.
Single PHP FPM container with Caddy
/vendor
/docker
/Dockerfile
0.0.0.0:{$PORT}
root /usr/src/app/public
fastcgi / /var/run/php-fpm.sock php
rewrite {
to {path} /index.php?{query}
}
supervisor {
command php-fpm
restart_policy always
redirect_stdout stdout
redirect_stderr stderr
}
errors stdout
FROM php:7.4-fpm-alpine
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
COPY --from=wizbii/caddy /caddy /usr/local/bin/caddy
RUN apk add --no-cache autoconf openssl-dev g++ make pcre-dev icu-dev zlib-dev libzip-dev && \
docker-php-ext-install bcmath intl opcache zip sockets && \
apk del --purge autoconf g++ make && \
mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"
WORKDIR /usr/src/app
COPY composer.json composer.lock ./
RUN composer install --no-dev --optimize-autoloader --no-scripts --no-plugins --prefer-dist --no-progress --no-interaction
COPY . .
ENV APP_ENV=prod
ENV PORT=80
RUN caddy -validate -conf=docker/Caddyfile && \
cp docker/php-fpm.conf /usr/local/etc/php-fpm.d/zz-docker.conf && \
php bin/console cache:warmup && \
chmod -R 777 var/cache var/log
EXPOSE 80
CMD ["caddy", "--conf", "docker/Caddyfile", "--log", "stdout"]
[global]
daemonize = no
[www]
listen = /var/run/php-fpm.sock
@janklan
Copy link

janklan commented Jun 20, 2023

Question: what magic step causes php-fpm to start here? Isn't the CMD section overriding php:7.4-fpm-alpine behaviour? I'm asking because in my case only Caddy started, not PHP. I used supervisord to start php-fpm for me. I wonder if there is a better way.

@luizvaz
Copy link

luizvaz commented Jun 30, 2023

Question: what magic step causes php-fpm to start here? Isn't the CMD section overriding php:7.4-fpm-alpine behaviour? I'm asking because in my case only Caddy started, not PHP. I used supervisord to start php-fpm for me. I wonder if there is a better way.

The command php bin/console cache:warmup at the RUN keyword
it is running using an UNIX socket file /var/run/php-fpm.sock

@Baldinof
Copy link
Author

This is caddy that is starting php-fpm, using it's supervisor plugin.

If you are using Caddy V2, I recommend you to have a look at this gist: https://gist.github.com/Baldinof/9adf039d9edabc0001dea3abafb6764f

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment