dunglas / frankenphp

🧟 The modern PHP app server
https://frankenphp.dev
MIT License
6.84k stars 234 forks source link

Issues with Installing PHP Extensions in Custom Docker Image #1107

Closed dionysios-ioannidis closed 1 week ago

dionysios-ioannidis commented 1 week ago

Hi,

I would like to build a custom Docker image for FrankenPHP and followed the instructions provided in the FrankenPHP documentation.

I attempted to install both the PHP extensions and the Caddy module for Cloudflare DNS. While the addition of the Caddy module was successful, I encountered issues with the PHP extensions. When visiting my WordPress site and checking the site health status, I receive the following warnings and error messages:

Here is my Dockerfile (on Debian 12):

FROM dunglas/frankenphp:latest-builder AS builder

# Add additional extensions here:
RUN install-php-extensions \
    pdo_mysql \
    gd \
    intl \
    zip \
    opcache \
    mysqli \
    curl \
    soap \
    xml \
    xmlreader \
    xmlwriter \
    xsl \
    imagick

# Copy xcaddy in the builder image
COPY --from=caddy:builder /usr/bin/xcaddy /usr/bin/xcaddy

# CGO must be enabled to build FrankenPHP
ENV CGO_ENABLED=1 XCADDY_SETCAP=1 XCADDY_GO_BUILD_FLAGS="-ldflags '-w -s'"
RUN xcaddy build \
    --output /usr/local/bin/frankenphp \
    --with github.com/dunglas/frankenphp=./ \
    --with github.com/dunglas/frankenphp/caddy=./caddy/ \
    --with github.com/dunglas/caddy-cbrotli \
    --with github.com/dunglas/mercure/caddy \
    --with github.com/dunglas/vulcain/caddy \
    --with github.com/caddy-dns/cloudflare

FROM dunglas/frankenphp AS runner

# Replace the official binary by the one containing your custom modules
COPY --from=builder /usr/local/bin/frankenphp /usr/local/bin/frankenphp

Steps to Build and Use the FrankenPHP Static File:

# Build:
docker build -t static-frankenphp -f Dockerfile .

# Extract the binary:
docker cp $(docker create --name static-frankenphp-tmp static-frankenphp):/usr/local/bin/frankenphp ~/src/frankenphp

# Check version
./frankenphp --version

# Replace
sudo rm /usr/local/bin/frankenphp
sudo mv frankenphp /usr/local/bin
sudo systemctl restart frankenphp

# Clean up the container:
docker rm static-frankenphp-tmp

Could you please help me troubleshoot why the PHP extensions are not being recognized in my WordPress installation? Thank you!

AlliBalliBaba commented 1 week ago

I think you have to do run the install-php-extensions part in the runner image, otherwise the libraries from the extensions will be missing in the final image.

...
FROM dunglas/frankenphp AS runner

# Replace the official binary by the one containing your custom modules
COPY --from=builder /usr/local/bin/frankenphp /usr/local/bin/frankenphp

# Now install PHP extensions
RUN install-php-extensions ...
dionysios-ioannidis commented 1 week ago

I followed your suggestion and explored other methods, but the PHP extensions are still missing from the FrankenPHP binary. I'm building the Docker image inside an Incus container. Could this be causing the issue? I'm running out of ideas, and since I'm not a Docker expert, perhaps compiling FrankenPHP from source might be an alternative approach, as explained in the documentation: https://frankenphp.dev/docs/compile/.

withinboredom commented 1 week ago

Looking at your Docker file and command, this isn't doing what you think it is doing. You are copying a FrankenPHP that is not statically compiled to your system. You need to statically compile FrankenPHP: https://frankenphp.dev/docs/embed/

Instead, you are using a dynamically compiled version of FrankenPHP which is using your system's installed version of PHP. If this is a non-zts build of PHP on your system, you will suffer severe performance issues and use your installed extensions. If it is a zts build of PHP and the same version from the container (same minor version with no special patches) you should be able to use it just fine this way. In fact, this is what you would end up with if you compile FrankenPHP from scratch.

dionysios-ioannidis commented 1 week ago

Thanks to everyone who offered their help.

I decided to take a different approach and build a FrankenPHP binary without using Docker. Following the instructions at https://frankenphp.dev/docs/compile/, I downloaded and built PHP from source, installed the required dependencies on Debian (including Go and xcaddy), and successfully created a binary that meets my WordPress needs.

Since I've achieved my goal, I believe this issue is no longer necessary.