Docker Community Forums

Share and learn in the Docker community.

Official PHP Image - How to add more extensions?

Hi,

i need to know how exactly i can add (for php8.1.0-fpm) the following (for docker rootless production system):
GD, intl, mysqli, pdo_mysql, zip, opcache

PS: I dont know if it’s important… i will use nginx, not apache.

Did you read “How to install more PHP extensions” on https://hub.docker.com/_/php?

@avbentem
Yes, but i’m not familiar with php. :pensive:
As example it’s so if i would tell you how to repair your car. :wink:

So what parts are giving you problems? I feel it’s a bit too much to ask people to explain everything, including things you may already know.

@avbentem

How to add this?
English isn’t my familiar language too! :blush:
So sorry, i can’t explain it more. :pensive:

Edit:
Hope you or anybody else can help.

@avbentem is right. We can help you if you can understand our help. Installing extensions can be tricky since you need to figure out what libraries that extension requires. This is why I created my own project where I installed many additional extensions for multiple versions of PHP. Unfortunately I don’t have enough time recently and I stopped being a PHP developer, so I don’t have PHP 8 yet.

You can use it as an example. It might help you to install the extensions for PHP 8.

You can find the common installation mehods here: docker-php/common/bin at master · itsziget/docker-php · GitHub

When an extension for a specific PHP versions requires a different method, I have an other bin folder for that version like this: docker-php/7.4/fpm/bin at master · itsziget/docker-php · GitHub

I have different branches for automated builds so if you don’t want to search for files in multiple directories, use the merged build branches from here: Branches · itsziget/docker-php · GitHub

I noticed that GitHub has a bug so I can’t select branches using the dropdown list but I can on the “branches” page.

Since each PHP version can have different base image, the required libraries can be different too. If it is the case, you need to try to install the extension, read the error messages and install the libraries mentioned by the error message. Sometimes it is not obvious, but if you want to work with PHP, especially in a container, you need to understand Linux a little more than otherwise. It was a long procesdure for me too.

1 Like

@rimelek
Thanks! :smiley:
I don’t understand all, but a part of it. :wink:

But, i don’t understand how to do it for GD. :pensive:
My (example) Dockerfile looks like (code from github link below):

FROM php:8.1.0-fpm
RUN apt-get install -y --no-install-recommends \
        libfreetype6-dev \
        libjpeg62-turbo-dev \
        libpng-dev \
    && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
                                   --with-png-dir=/usr/include/ --enable-gd-native-ttf \
    && docker-php-ext-install -j$(nproc) gd

Then i do:

docker build -t myphp .

Output:

Sending build context to Docker daemon 47MB
Step 1/2 : FROM php:8.1.0-fpm
—> ce4c0139f920
Step 2/2 : RUN apt-get install -y --no-install-recommends libfreetype6-dev libjpeg62-turbo-dev libpng-dev && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ --with-png-dir=/usr/include/ --enable-gd-native-ttf && docker-php-ext-install -j$(nproc) gd
—> Running in afee78ea2dfa
Reading package lists…
Building dependency tree…
Reading state information…
E: Unable to locate package libfreetype6-dev
E: Unable to locate package libjpeg62-turbo-dev
E: Unable to locate package libpng-dev
The command ‘/bin/sh -c apt-get install -y --no-install-recommends libfreetype6-dev libjpeg62-turbo-dev libpng-dev && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ --with-png-dir=/usr/include/ --enable-gd-native-ttf && docker-php-ext-install -j$(nproc) gd’ returned a non-zero code: 100

What’s wrong? :thinking:
I’m confused… :roll_eyes:

Next question, is this code on docker-php/re-php-ext-gd at master · itsziget/docker-php · GitHub for prodcution systems too or is there another package for GD in prodcution?

Edit:
Ahhh, ok, my fault.
I think that not the packages for php8.1.0-fpm. :blush:

You need to run apt-get update before apt-get install

1 Like

@rimelek
i’ve tried it

FROM php:8.1.0-fpm
RUN apt-get update
RUN apt-get install -y --no-install-recommends \
libfreetype6-dev \
libjpeg62-turbo-dev \
libpng-dev \
&& docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-di    r=/usr/include/ \
--with-png-dir=/usr/include/ --enable-gd-native-    ttf \
&& docker-php-ext-install -j$(nproc) gd

and another way

FROM php:8.1.0-fpm
RUN apt-get update && apt-get install -y --no-install-recommends \
libfreetype6-dev \
libjpeg62-turbo-dev \
libpng-dev \
&& docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-di    r=/usr/include/ \
--with-png-dir=/usr/include/ --enable-gd-native-    ttf \
&& docker-php-ext-install -j$(nproc) gd

Output same as before. :thinking: :blush:

Are you sure? Because I don’t think so. Please share the output even if you think it is the same. I think I know what message you have but I would like to see it from you.

@rimelek

method 1:

docker build -t myphp .
Sending build context to Docker daemon 47MB
Step 1/2 : FROM php:8.1.0-fpm
8.1.0-fpm: Pulling from library/php
e5ae68f74026: Pull complete
99c3c1c4d556: Pull complete
2c23b6beb07a: Pull complete
9874148cff9a: Pull complete
f46ad8faa98a: Pull complete
0e2ccbe4df72: Pull complete
bfa3ffa6f5a5: Pull complete
bf58c731d92e: Pull complete
e705fc1fdd21: Pull complete
e31916d2306b: Pull complete
Digest: sha256:c884ad419ce99d3136cd02a56f7ca84009fa5a50637e96d20acdd7e34e44c899
Status: Downloaded newer image for php:8.1.0-fpm
—> ce4c0139f920
Step 2/2 : RUN apt-get update && apt-get install -y --no-install-recommends libfreetype6-dev libjpeg62-turbo-dev libpng-dev && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-di r=/usr/include/ --with-png-dir=/usr/include/ --enable-gd-native- ttf && docker-php-ext-install -j$(nproc) gd
—> Running in 882cf633ddf4
Get:1 Index of /debian-security bullseye-security InRelease [44.1 kB]
Get:2 Index of /debian bullseye InRelease [116 kB]
Get:3 Index of /debian bullseye-updates InRelease [39.4 kB]
Get:4 Index of /debian-security bullseye-security/main amd64 Packages [99.5 kB]
Get:5 Index of /debian bullseye/main amd64 Packages [8180 kB]
Get:6 Index of /debian bullseye-updates/main amd64 Packages [2592 B]
Fetched 8481 kB in 2s (5519 kB/s)
Reading package lists…
Reading package lists…
Building dependency tree…
Reading state information…
The following additional packages will be installed:
libbrotli-dev libfreetype-dev libfreetype6 libjpeg62-turbo libpng16-16
zlib1g-dev
Suggested packages:
freetype2-doc
Recommended packages:
libpng-tools
The following NEW packages will be installed:
libbrotli-dev libfreetype-dev libfreetype6 libfreetype6-dev libjpeg62-turbo
libjpeg62-turbo-dev libpng-dev libpng16-16 zlib1g-dev
0 upgraded, 9 newly installed, 0 to remove and 0 not upgraded.
Need to get 2571 kB of archives.
After this operation, 7490 kB of additional disk space will be used.
Get:1 Index of /debian bullseye/main amd64 libbrotli-dev amd64 1.0.9-2+b2 [288 kB]
Get:2 Index of /debian bullseye/main amd64 libpng16-16 amd64 1.6.37-3 [294 kB]
Get:3 Index of /debian bullseye/main amd64 libfreetype6 amd64 2.10.4+dfsg-1 [418 kB]
Get:4 Index of /debian bullseye/main amd64 zlib1g-dev amd64 1:1.2.11.dfsg-2 [190 kB]
Get:5 Index of /debian bullseye/main amd64 libpng-dev amd64 1.6.37-3 [298 kB]
Get:6 Index of /debian bullseye/main amd64 libfreetype-dev amd64 2.10.4+dfsg-1 [571 kB]
Get:7 Index of /debian bullseye/main amd64 libfreetype6-dev amd64 2.10.4+dfsg-1 [82.4 kB]
Get:8 Index of /debian bullseye/main amd64 libjpeg62-turbo amd64 1:2.0.6-4 [151 kB]
Get:9 Index of /debian bullseye/main amd64 libjpeg62-turbo-dev amd64 1:2.0.6-4 [278 kB]
debconf: delaying package configuration, since apt-utils is not installed
Fetched 2571 kB in 0s (45.3 MB/s)
Selecting previously unselected package libbrotli-dev:amd64.
(Reading database … 13156 files and directories currently installed.)
Preparing to unpack …/0-libbrotli-dev_1.0.9-2+b2_amd64.deb …
Unpacking libbrotli-dev:amd64 (1.0.9-2+b2) …
Selecting previously unselected package libpng16-16:amd64.
Preparing to unpack …/1-libpng16-16_1.6.37-3_amd64.deb …
Unpacking libpng16-16:amd64 (1.6.37-3) …
Selecting previously unselected package libfreetype6:amd64.
Preparing to unpack …/2-libfreetype6_2.10.4+dfsg-1_amd64.deb …
Unpacking libfreetype6:amd64 (2.10.4+dfsg-1) …
Selecting previously unselected package zlib1g-dev:amd64.
Preparing to unpack …/3-zlib1g-dev_1%3a1.2.11.dfsg-2_amd64.deb …
Unpacking zlib1g-dev:amd64 (1:1.2.11.dfsg-2) …
Selecting previously unselected package libpng-dev:amd64.
Preparing to unpack …/4-libpng-dev_1.6.37-3_amd64.deb …
Unpacking libpng-dev:amd64 (1.6.37-3) …
Selecting previously unselected package libfreetype-dev:amd64.
Preparing to unpack …/5-libfreetype-dev_2.10.4+dfsg-1_amd64.deb …
Unpacking libfreetype-dev:amd64 (2.10.4+dfsg-1) …
Selecting previously unselected package libfreetype6-dev:amd64.
Preparing to unpack …/6-libfreetype6-dev_2.10.4+dfsg-1_amd64.deb …
Unpacking libfreetype6-dev:amd64 (2.10.4+dfsg-1) …
Selecting previously unselected package libjpeg62-turbo:amd64.
Preparing to unpack …/7-libjpeg62-turbo_1%3a2.0.6-4_amd64.deb …
Unpacking libjpeg62-turbo:amd64 (1:2.0.6-4) …
Selecting previously unselected package libjpeg62-turbo-dev:amd64.
Preparing to unpack …/8-libjpeg62-turbo-dev_1%3a2.0.6-4_amd64.deb …
Unpacking libjpeg62-turbo-dev:amd64 (1:2.0.6-4) …
Setting up libjpeg62-turbo:amd64 (1:2.0.6-4) …
Setting up libjpeg62-turbo-dev:amd64 (1:2.0.6-4) …
Setting up libpng16-16:amd64 (1.6.37-3) …
Setting up zlib1g-dev:amd64 (1:1.2.11.dfsg-2) …
Setting up libbrotli-dev:amd64 (1.0.9-2+b2) …
Setting up libpng-dev:amd64 (1.6.37-3) …
Setting up libfreetype6:amd64 (2.10.4+dfsg-1) …
Setting up libfreetype-dev:amd64 (2.10.4+dfsg-1) …
Setting up libfreetype6-dev:amd64 (2.10.4+dfsg-1) …
Processing triggers for libc-bin (2.31-13+deb11u2) …
Configuring for:
PHP Api Version: 20210902
Zend Module Api No: 20210902
Zend Extension Api No: 420210902
configure: WARNING: you should use --build, --host, --target
configure: error: unrecognized options: --with-freetype-dir, --with-jpeg-di, --with-png-dir, --enable-gd-native-
The command ‘/bin/sh -c apt-get update && apt-get install -y --no-install-recommends libfreetype6-dev libjpeg62-turbo-dev libpng-dev && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-di r=/usr/include/ --with-png-dir=/usr/include/ --enable-gd-native- ttf && docker-php-ext-install -j$(nproc) gd’ returned a non-zero code: 1

method 2:

docker build -t myphp .
Sending build context to Docker daemon 47MB
Step 1/3 : FROM php:8.1.0-fpm
—> ce4c0139f920
Step 2/3 : RUN apt-get update
—> Running in 4745221a3cf2
Get:1 Index of /debian-security bullseye-security InRelease [44.1 kB]
Get:2 Index of /debian bullseye InRelease [116 kB]
Get:3 Index of /debian bullseye-updates InRelease [39.4 kB]
Get:4 Index of /debian-security bullseye-security/main amd64 Packages [99.5 kB]
Get:5 Index of /debian bullseye/main amd64 Packages [8180 kB]
Get:6 Index of /debian bullseye-updates/main amd64 Packages [2592 B]
Fetched 8481 kB in 1s (5833 kB/s)
Reading package lists…
Removing intermediate container 4745221a3cf2
—> 5cf90a036d29
Step 3/3 : RUN apt-get install -y --no-install-recommends libfreetype6-dev libjpeg62-turbo-dev libpng-dev && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ --with-png-dir=/usr/include/ --enable-gd-native-ttf && docker-php-ext-install -j$(nproc) gd
—> Running in 3e229e3e2e24
Reading package lists…
Building dependency tree…
Reading state information…
The following additional packages will be installed:
libbrotli-dev libfreetype-dev libfreetype6 libjpeg62-turbo libpng16-16
zlib1g-dev
Suggested packages:
freetype2-doc
Recommended packages:
libpng-tools
The following NEW packages will be installed:
libbrotli-dev libfreetype-dev libfreetype6 libfreetype6-dev libjpeg62-turbo
libjpeg62-turbo-dev libpng-dev libpng16-16 zlib1g-dev
0 upgraded, 9 newly installed, 0 to remove and 0 not upgraded.
Need to get 2571 kB of archives.
After this operation, 7490 kB of additional disk space will be used.
Get:1 Index of /debian bullseye/main amd64 libbrotli-dev amd64 1.0.9-2+b2 [288 kB]
Get:2 Index of /debian bullseye/main amd64 libpng16-16 amd64 1.6.37-3 [294 kB]
Get:3 Index of /debian bullseye/main amd64 libfreetype6 amd64 2.10.4+dfsg-1 [418 kB]
Get:4 Index of /debian bullseye/main amd64 zlib1g-dev amd64 1:1.2.11.dfsg-2 [190 kB]
Get:5 Index of /debian bullseye/main amd64 libpng-dev amd64 1.6.37-3 [298 kB]
Get:6 Index of /debian bullseye/main amd64 libfreetype-dev amd64 2.10.4+dfsg-1 [571 kB]
Get:7 Index of /debian bullseye/main amd64 libfreetype6-dev amd64 2.10.4+dfsg-1 [82.4 kB]
Get:8 Index of /debian bullseye/main amd64 libjpeg62-turbo amd64 1:2.0.6-4 [151 kB]
Get:9 Index of /debian bullseye/main amd64 libjpeg62-turbo-dev amd64 1:2.0.6-4 [278 kB]
debconf: delaying package configuration, since apt-utils is not installed
Fetched 2571 kB in 0s (56.8 MB/s)
Selecting previously unselected package libbrotli-dev:amd64.
(Reading database … 13156 files and directories currently installed.)
Preparing to unpack …/0-libbrotli-dev_1.0.9-2+b2_amd64.deb …
Unpacking libbrotli-dev:amd64 (1.0.9-2+b2) …
Selecting previously unselected package libpng16-16:amd64.
Preparing to unpack …/1-libpng16-16_1.6.37-3_amd64.deb …
Unpacking libpng16-16:amd64 (1.6.37-3) …
Selecting previously unselected package libfreetype6:amd64.
Preparing to unpack …/2-libfreetype6_2.10.4+dfsg-1_amd64.deb …
Unpacking libfreetype6:amd64 (2.10.4+dfsg-1) …
Selecting previously unselected package zlib1g-dev:amd64.
Preparing to unpack …/3-zlib1g-dev_1%3a1.2.11.dfsg-2_amd64.deb …
Unpacking zlib1g-dev:amd64 (1:1.2.11.dfsg-2) …
Selecting previously unselected package libpng-dev:amd64.
Preparing to unpack …/4-libpng-dev_1.6.37-3_amd64.deb …
Unpacking libpng-dev:amd64 (1.6.37-3) …
Selecting previously unselected package libfreetype-dev:amd64.
Preparing to unpack …/5-libfreetype-dev_2.10.4+dfsg-1_amd64.deb …
Unpacking libfreetype-dev:amd64 (2.10.4+dfsg-1) …
Selecting previously unselected package libfreetype6-dev:amd64.
Preparing to unpack …/6-libfreetype6-dev_2.10.4+dfsg-1_amd64.deb …
Unpacking libfreetype6-dev:amd64 (2.10.4+dfsg-1) …
Selecting previously unselected package libjpeg62-turbo:amd64.
Preparing to unpack …/7-libjpeg62-turbo_1%3a2.0.6-4_amd64.deb …
Unpacking libjpeg62-turbo:amd64 (1:2.0.6-4) …
Selecting previously unselected package libjpeg62-turbo-dev:amd64.
Preparing to unpack …/8-libjpeg62-turbo-dev_1%3a2.0.6-4_amd64.deb …
Unpacking libjpeg62-turbo-dev:amd64 (1:2.0.6-4) …
Setting up libjpeg62-turbo:amd64 (1:2.0.6-4) …
Setting up libjpeg62-turbo-dev:amd64 (1:2.0.6-4) …
Setting up libpng16-16:amd64 (1.6.37-3) …
Setting up zlib1g-dev:amd64 (1:1.2.11.dfsg-2) …
Setting up libbrotli-dev:amd64 (1.0.9-2+b2) …
Setting up libpng-dev:amd64 (1.6.37-3) …
Setting up libfreetype6:amd64 (2.10.4+dfsg-1) …
Setting up libfreetype-dev:amd64 (2.10.4+dfsg-1) …
Setting up libfreetype6-dev:amd64 (2.10.4+dfsg-1) …
Processing triggers for libc-bin (2.31-13+deb11u2) …
Configuring for:
PHP Api Version: 20210902
Zend Module Api No: 20210902
Zend Extension Api No: 420210902
configure: WARNING: you should use --build, --host, --target
configure: error: unrecognized options: --with-freetype-dir, --with-jpeg-di, --with-png-dir, --enable-gd-native-
The command ‘/bin/sh -c apt-get install -y --no-install-recommends libfreetype6-dev libjpeg62-turbo-dev libpng-dev && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ --with-png-dir=/usr/include/ --enable-gd-native-ttf && docker-php-ext-install -j$(nproc) gd’ returned a non-zero code: 1

PS: Thanks for your help! :wink:

Edit:
Because of (i know, it’s not 8.1.0) In php:7.4.1-apache, Error: unrecognized options: --with-png-dir, --with-jpeg-dir, --with-freetype-dir · Issue #931 · docker-library/php · GitHub i’ve tried another method without

-dir

and get

configure: error: unrecognized options: --with-png, --enable-gd-native-ttf
The command ‘/bin/sh -c apt-get install -y --no-install-recommends libfreetype6-dev libjpeg62-turbo-dev libpng-dev && docker-php-ext-configure gd --with-freetype=/usr/include/ --with-jpeg=/usr/include/ --with-png=/usr/include/ --enable-gd-native-ttf && docker-php-ext-install -j$(nproc) gd’ returned a non-zero code: 1

I’ve tried it also without

--with-png

because of In php:7.4.1-apache, Error: unrecognized options: --with-png-dir, --with-jpeg-dir, --with-freetype-dir · Issue #931 · docker-library/php · GitHub
but it don’t work!

configure: error: unrecognized options: --enable-gd-native-ttf
The command ‘/bin/sh -c apt-get install -y --no-install-recommends libfreetype6-dev libjpeg62-turbo-dev libpng-dev && docker-php-ext-configure gd --with-freetype=/usr/include/ --with-jpeg=/usr/include/ --enable-gd-native-ttf && docker-php-ext-install -j$(nproc) gd’ returned a non-zero code: 1

That’s why i’m asking the question how to add this extensions!

Whitout the line (i don’t know for what this line is, see below)

–enable-gd-native-ttf

Successfully built 920d8525b910
Successfully tagged myphp:latest

docker run -it myphp bash
root@e8ff29346a2b:/var/www/html# php -m
[PHP Modules]
Core
ctype
curl
date
dom
fileinfo
filter
ftp
gd
hash
iconv
json
libxml
mbstring
mysqlnd
openssl
pcre
PDO
pdo_sqlite
Phar
posix
readline
Reflection
session
SimpleXML
sodium
SPL
sqlite3
standard
tokenizer
xml
xmlreader
xmlwriter
zlib

–enable-gd-native-ttf` configure argument has been removed. This was not used since PHP 5.5.0.
source: PHP: Other changes - Manual

So, next extensions ( intl, mysqli, pdo_mysql, zip, opcache) are waiting. :roll_eyes:

Yes, your two quoated outpus are the same but both of them different from the original output you shared. This is exactly the output I thought you have. It means the new PHP 8 does not have those options that PHP 7 had.

The solution is what you did, removing the options one by one until the build succeeds. And this is how you should do with the other extensions too.

@rimelek
If i extend my Dockerfile with

&& docker-php-ext-configure intl && docker-php-ext-install -j$(nproc) intl

i get

configure: error: Package requirements (icu-uc >= 50.1 icu-io icu-i18n) were not met:
No package ‘icu-uc’ found
No package ‘icu-io’ found
No package ‘icu-i18n’ found
Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.
Alternatively, you may set the environment variables ICU_CFLAGS
and ICU_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.

So please, like you and all other see, i need help! :pensive: :blush:

I don’t have all the answers so I should also do the reseacrh to build PHP 8 with these extensions. You need to try, interpret the error messages and search for the error messages like No package 'icu-uc'. Install the required library and try again. Then you will probably find another error you need to solve.

Luckily for you I want to add PHP 8 to my project so I will try to build it later but I can’t promise so I suggest you continue trying.

@rimelek
Ok, hope you’ll get it done soon. :sunglasses:

Do you know how i can check that all extensions are working? :blush:

Like for opcache

php -i | grep 'opcache\.enable '

pcache.enable => On => On

You can check the rest of the extensions the same way you did with opcache. Of course an extension to be loaded does not always mean it works properly. So if you want to make sure, you have to test it.

@rimelek

php -i | grep 'mysqli\.enable '

and other extensions showing me nothing (no output). :thinking:

The method is the same. The string you need to search for can be different. Search only for the name of the extension. The fact that you don’t need to enable an extension does not mean it does not work.

1 Like

I have just added experimental support for PHP 8.0 FPM and PHP 8.1 FPM.

XMLRPC was moved to PECL so I had to change the build process a little to support custom PECL channels.

intl however was good as it was. You just missed to install libicu-dev: docker-php/re-php-ext-intl at build/8.0/fpm · itsziget/docker-php · GitHub

1 Like

@rimelek
Wow :flushed: there so many commands in there… can you tell me why they are needed? :thinking:
If i compare my Dockerfile with all files from you… mine has only a few lines. :blush: