Custom php.ini to the docker library php using docker-compose config

An approach that adds additional ini files to be parsed by php by just adding two lines to your docker-compose.yml.

It’s a convenient practice for small projects without ad-hoc tailoring to use prebuilt images from docker hub. Sometimes we need to change some php.ini settings for a php container, for example, increasing upload_max_filesize parameter. The official docker hub library php approach for changing php.ini is to copy new php.ini and build a new image. But when there’s no need to build a new image, building a new image just to replace the file is an overkill. There’s also an approach to mount the whole dir with the php.ini, which is also not always suitable, e.g. when you have some prebuilt image based on the php, having a bunch of add-on files in conf.d subdirectory.

Here’s an approach that adds one more directory to parse for php, having all your other settings untouched. Just add the following two lines to the corresponding sections in the docker-compose.yml:

     - ./php-ini:/usr/local/etc/php/custom.d
: "/usr/local/etc/php/custom.d:/usr/local/etc/php/conf.d"

Add your new php.ini to the php-ini folder in the compose root folder. The directory will be scanned for the ini files after the original ini file is parsed and before conf.d files are parsed. So, you can override initial file setting or add new ones to the files that are under your VCS.

Here we exploit the fact that PHP_INI_SCAN_DIR can parse a list of directories, separated by a semicolon. The `/usr/local/etc/php/conf.d` is from the original image and fits the dockerhub library php default setting. Try checking your base image corresponding setting by running `docker-compose run –rm php php –ini | grep additional` and replacing the setting value after semicolon with setting from your container. Also, if you need something to be changed after the conf.d, just add another directory to the end, also separated by a semicolon.

That’s it, folks! Easy? 🙂