Mohamed Salem Lamiri
Mohamed Salem Lamiri

Reputation: 6077

Docker - How to disable PHP ext / modules

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.

Upvotes: 6

Views: 15994

Answers (4)

beslovas
beslovas

Reputation: 43

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

Upvotes: 3

Alfred Huang
Alfred Huang

Reputation: 18235

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.

Upvotes: 15

ShadyAmoeba
ShadyAmoeba

Reputation: 577

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.

Upvotes: 0

Camilo Silva
Camilo Silva

Reputation: 8711

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.

Upvotes: -5

Related Questions