Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save guiwuff/2486cfabb58aabd90e2de7c2db6aad20 to your computer and use it in GitHub Desktop.
Save guiwuff/2486cfabb58aabd90e2de7c2db6aad20 to your computer and use it in GitHub Desktop.
Custom php-fpm Docker Image from a Container

Create Custom Docker Images for php-fpm with additional Modules

Make sure you have docker up and running.

Base Image php:5.6.30-fpm

Pull the official php-fpm 5.6 from docker registry, it will pull all of the neccessary files from docker and ready to be used by a container.

Image reference in the docker hub : https://hub.docker.com/_/php/, the latest one would be php version 7, in this build I want to specifically use php 5.6.x since that is the version used in the production server currently.

$ docker pull php:5.6.30-fpm
5.6.30-fpm: Pulling from library/php
cd0a524342ef: Pull complete
14b8a88a0af0: Pull complete
d78c922dd678: Pull complete
ed16e0ed90d8: Pull complete
1f2895c191af: Pull complete
d575de0a5bec: Pull complete
5b71dee130cf: Pull complete
ca32272d000c: Pull complete
4019e7ecc801: Pull complete
Digest: sha256:338ae29e81b521a866a2fef1d5688f69416c5270b692e76f7af0c15fd39f2270
Status: Downloaded newer image for php:5.6.30-fpm

Confirm the pulled image

$ docker images | grep php
php                     5.6.30-fpm          e682025e0e96        6 days ago          362 MB
php                     7-fpm               ce56632b08e5        5 weeks ago         378 MB

Create and start the container with the recently pulled image and login to the container to activate and install additional extensions. This container will serve php only through fpm in port 9000.

$ docker create --name php-fpm-custom php:5.6.30-fpm
69cf5747e4e90551d89d621372ba506339b0c11849754473d6ad873c4bb9174e
$ docker start php-fpm-custom
php-fpm-custom

Confirm the container is up and running

$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS               NAMES
69cf5747e4e9        php:5.6.30-fpm      "docker-php-entryp..."   About a minute ago   Up 24 seconds       9000/tcp            php-fpm-custom

Install Additional PHP Extensions

Login to docker with bash and install the additional extensions. Use docker helper sript docker-php-ext-install to install mysqli, gd, iconv and mcrypt.

$ docker exec -it php-fpm-custom bash
root@69cf5747e4e9:/var/www/html# docker-php-ext-install mysqli
+ cd mysqli
+ phpize
Configuring for:
PHP Api Version:         20131106
Zend Module Api No:      20131226
Zend Extension Api No:   220131226
+ ./configure
...
...
See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------

Build complete.
Don't forget to run 'make test'.

Installing shared extensions:     /usr/local/lib/php/extensions/no-debug-non-zts-20131226/
Installing header files:           /usr/local/include/php/
find . -name \*.gcno -o -name \*.gcda | xargs rm -f
find . -name \*.lo -o -name \*.o | xargs rm -f
find . -name \*.la -o -name \*.a | xargs rm -f
find . -name \*.so | xargs rm -f
find . -name .libs -a -type d|xargs rm -rf
rm -f libphp.la       modules/* libs/*

root@69cf5747e4e9:/var/www/html# 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


Get:1 http://deb.debian.org jessie-updates InRelease [145 kB]
Ign http://deb.debian.org jessie InRelease
Get:2 http://deb.debian.org jessie Release.gpg [2373 B]
Get:3 http://deb.debian.org jessie-updates/main amd64 Packages [17.6 kB]
Get:4 http://deb.debian.org jessie Release [148 kB]
...
...
See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------

Build complete.
Don't forget to run 'make test'.

Installing shared extensions:     /usr/local/lib/php/extensions/no-debug-non-zts-20131226/
Installing header files:           /usr/local/include/php/
find . -name \*.gcno -o -name \*.gcda | xargs rm -f
find . -name \*.lo -o -name \*.o | xargs rm -f
find . -name \*.la -o -name \*.a | xargs rm -f
find . -name \*.so | xargs rm -f
find . -name .libs -a -type d|xargs rm -rf
rm -f libphp.la       modules/* libs/*

Create Images based on the container

The setup inside the existing container php-fpm-custom can be used for another time for example using docker compose in a project, for that we need to create a new image consisting the modification using docker commit

$ docker commit php-fpm-custom php:5.6.30-fpm-custom
sha256:06a0c31709e21123a19e4e34a2c7fe0c675e5ba3d94fd63b06d78381c9b3408a

Docker commit create new images under php with tag 5.6.30-fpm-custom

$ docker images | grep php
php                     5.6.30-fpm-custom   06a0c31709e2        3 minutes ago       384 MB
php                     5.6.30-fpm          e682025e0e96        6 days ago          362 MB
php                     7-fpm               ce56632b08e5        5 weeks ago         378 MB
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment