How to install external PHP Modules in Docker

Over the time, while you are trying to work with docker, you will like what you see, you will like the idea that you are building one linux distro which can do one thing only, and thats for me a passion ..

And everyday you will have something new to discover, am not going to talk a lot but I’ll put the past and the new way that I have found which helped me to install external PHP module (xDebug) without any problem.

Before, when I wanted to include xDebug in any PHP Docker image I build, I’ll download the source code, compile it and then try to move the module file to the PHP extension directory ( or use pecl to install it ), which was simple when you are using Ubuntu, but this will also lead to have a big image 500+MB in size and so your Dockerfile will be something like this:

Please note that in this docker file I also install other internal module, like : mcrypt, gd, mbstring .. etc.

its simple and really can do the trick for me, and I’ll have xDebug installed without any problem.

But while I was trying to learn Docker, I came across a nice small footprint linux distro called alpine, and I found out that many people/projects use it, and they use it a lot to create a small images for what they need, so I tried to get familiar with it, I modified my Dockerfile to make sure it will work with alpine and it become like this

And I was happy when the build finished and the size of the image was about 300MB I mean I saved 200MB , but when I looked at the logs I noticed that xDebug was not installed correctly, and it always fail to load. So after a big and long journey to find the solution, I found out that I can use a command called docker-php-source to achieve the best result and the usage of it is simple so we just extract the source, install the module, enable the module and then delete the source, like the following :

So my final Dockerfile has become like this :

and the image is now working like a charm with xdebug and redis installed and enabled.

The only sad thing is that I was not able to make the size smaller, because mcrypt need the development libraries which should be always be within the image, without them I can get a smaller image 100MB.

Finally you can find my image on Docker Hub at this address :

PS: if you find any grammatical or lingual issue please let me know, as English is not my mother language, thanks.


  • Thomas says:

    Thanks a lot, this article is very useful for me. Yesterday, I’m trying to install ‘libfreetype6-dev’ in alpine, cause I used to install it on php:5.6-fpm, whitch is based on ubuntu. Then I realize ‘libfreetype6-dev’ is a debian package, and I don’t know what to do in alpine. I was work hard on install an apt-get to alpine, and it’s a stupid way. Then I have read this article, and find out that ‘freetype-dev’ can do the job. Thanks again, and may I ask how do you know the ‘libfreetype6-dev’ is ‘freetype-dev’ on alpine? Is there any site I can search for it?

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: