β οΈ This post links to an external website. β οΈ
Currently, the de-facto standard to dockerizing PHP applications is to use the Docker PHP official image. This is maintained by Docker itself and community members and has compiled PHP with no extensions. To install extensions, we have to do this in our Dockerfile:
FROM php:8.2-cliRUN docker-php-ext-install pdo pdo_mysql curlThe helper script
docker-php-ext-installdoes unpack the PHP source code and installs the given extensions. For some extensions likegd, we need also libraries installed likelibpngand so on. So we have to install manually before the library with dev dependencies and remove it afterwards (we need the development headers to compile the extension).Luckily, this problem has been solved already by the Community with the docker-php-extension-installer. So instead we install manually
aptorapkpackages, we just use this script, and it takes care of that. The usage looks like this:FROM php:8.2-cliADD --chmod=0755 https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/RUN install-php-extensions pdo pdo_mysql gd curlThis takes 20s on my machine (I have a high-end gaming machine, not comparable to real CI) and produces a Docker image which is 557MB.
β― docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEphp latest 683c45c057f0 38 seconds ago 557MBHuh, thatβs much. Usually, the solution which many people are considering is then to use the Alpine variant. The Alpine variant is normally much smaller, but uses a different libc so musl. There already some known PHP issues to Alpine variant images, also there are known performance issues with allocation, see FrankenPHP issue.
But anyway, letβs do the same again on alpine to just compare the size. So we added additionally an
-alpineflag to the base image.FROM php:8.2-cli-alpineADD --chmod=0755 https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/RUN install-php-extensions pdo pdo_mysql gd curland the image is after compiling
β― docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEphp latest 8651ba41dbd1 37 seconds ago 114MBfrom 557MB to just 114MB, thatβs really good. The next pain point of the official docker image is building a multi arch image.
continue reading on shyim.me
If this post was enjoyable or useful for you, please share it! If you have comments, questions, or feedback, you can email my personal email. To get new posts, subscribe use the RSS feed.