5

I am working on a PHP API and I would like to disable unused php Modules inside my PHP-FPM image, such as "sqlite3, pdo ..".

I am a docker beginner and I would like to know if is there anything similar to docker-php-ext-enable if not what is the best practice for disabling unused php modules.

Mohamed Salem Lamiri
  • 5,767
  • 6
  • 34
  • 47

4 Answers4

13

Finally I found the key point.

Inside docker php container, all the registered modules holds by a configuration file under the below path.

/usr/local/etc/php/conf.d/*.ini

bash into the container:

docker exec -it php_container_name bash

You can list all the enabled modules by php -m:

And cd into that folder, you can see the relating config files:

cd /usr/local/etc/php/conf.d/
ls

# output
docker-php-ext-mcrypt.ini  docker-php-ext-mysqli.ini    
docker-php-ext-opcache.ini  opcache-recommended.ini  
docker-php-ext-zip.ini

To disable some extension module, make a dir disabled, and move that .ini file inside it, for example:

mkdir disabled
mv docker-php-ext-opcache.ini disabled
mv opcache-recommended.ini

Finally, press Ctrl+D to exit the container, and then restart the container to make changes work.

docker restart php_container_name

You can get into the container and run php -m to see, the relating extension is gone.

Potherca
  • 13,207
  • 5
  • 76
  • 94
Alfred Huang
  • 17,654
  • 32
  • 118
  • 189
  • There's a typo `mkdir disalbled`, can't change it because edits need to be at least 10 edited characters. – Bas Peeters Nov 24 '17 at 14:20
  • 3
    i am using php:7.2.5-fpm and wanna disable pdo_sqlite that shipped with image by default. How to do that? – crz Apr 27 '18 at 07:22
  • @crz in that case (if it was compiled with sqlite), I don't think you can remove it. You should compile php without it – Pere Aug 25 '22 at 22:12
1

In my case i had to enable/disable xdebug extension. I appears that there's some 'magic' that works by (de-)suffixing extensions ini file with -disabled.

Once the suffix changes, php somehow magically loads/unloads module. You can check that by running php -m command. (maybe this behaviour is only alpine-linux related??)

I wrote additional docker-php-ext-disable-xdebug script by taking example from /usr/local/bin/docker-php-ext-enable-xdebug:

#!/bin/sh

if test -f '/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini'; then
        echo 'Disabling extension xdebug'
        mv '/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini' '/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini-disabled'
else
        echo 'The extension xdebug is not enabled'
fi
beslovas
  • 23
  • 5
0

Piggybacking off Alfred's answer but I made these today.

alias disDebugCust="docker exec -it xdebugContainer bash -c 'cd /usr/local/etc/php/conf.d/ && mkdir -p disabled && mv xdebug.ini disabled && /etc/init.d/apache2 reload'"
alias enDebugCust="docker exec -it xdebugContainer bash -c 'cd /usr/local/etc/php/conf.d/disabled && mv xdebug.ini /usr/local/etc/php/conf.d/ && /etc/init.d/apache2 reload'"

First we exec into the container. Then we go to the config folder. Then we make a new directory called disabled if one doesn't exist. Then we move the ini file. Finally we restart apache.

The second command just moves it back and restarts apache.

ShadyAmoeba
  • 527
  • 1
  • 4
  • 15
-5

Yes that's possible.

Taken from https://hub.docker.com/_/php/

For example, if you want to have a PHP-FPM image with iconv, mcrypt and gd extensions, you can inherit the base image that you like, and write your own Dockerfile like this:

FROM php:7.0-fpm
RUN apt-get update && apt-get install -y \
        libfreetype6-dev \
        libjpeg62-turbo-dev \
        libmcrypt-dev \
       libpng12-dev \
    && docker-php-ext-install -j$(nproc) iconv mcrypt \
    && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
    && docker-php-ext-install -j$(nproc) gd

Remember, you must install dependencies for your extensions manually. If an extension needs custom configure arguments, you can use the docker-php-ext-configure script like this example.

Camilo Silva
  • 8,283
  • 4
  • 41
  • 61