How to add extensions to php docker image ?

docker-php

The default php images like php:7.0-fpm may not contain all required php extensions like mysqli, pdo, mbstring, which are the most common requirements for a website based on PHP or a framework like Drupal, Wordpress or Laravel. In such cases, the extensions need to be manually added to the image. This can be done as follows -

  1. Execute shell inside the php container -
    docker exec -it 631d75a07e77 /bin/bash
  2. Verify missing extensions using php -i | grep -i mysql
  3. Install the extensions -
    docker-php-ext-install mysql
    docker-php-ext-install mysqli
    docker-php-ext-install mbstring
    docker-php-ext-install pdo_mysql
  4. Verify after installation - new extensions will be visible
    php -i | grep -i mysql
  5. Keeping the container running, execute this command from outside the container -
    docker commit 631d75a07e77 something/php:7.0-fpm-custom

This will save the changes in a new image named phpfpm2

Once done, we can use this image in docker-compose.yml or Dockerfile. One could additionally push the image to their personal repository to keep it safe & use that in Dockerfile/docker-compose.yml files!
docker push something/php:7.0-fpm-custom