Extending Official PHP Images with php-redis
When building a custom PHP-FPM container, you often start with the official image from Docker Hub rather than compiling PHP from source. This approach leverages pre-configured security patches, dependency management, and optimized build scripts that are already baked into the base image. However, once you extend an image like php:5.6-fpm, you frequently need to add specific extensions such as redis to enable caching or session storage capabilities.
The official documentation demonstrates this pattern clearly using a Dockerfile that installs development libraries and then invokes the docker-php-ext-install command. For standard C extensions, the workflow is straightforward: update your package list, install the required header files (the -dev packages), configure any necessary compile-time flags, and finally run the installation script.
To apply this same methodology to the redis extension, you must first understand that php-redis is not a C extension in the traditional sense; it is a PHP module written in C but distributed as a source package within the Debian/Ubuntu repositories used by these images. Consequently, the installation process mirrors the logic shown in the official examples: install the development headers, locate the source code, and compile.
Here is how you would modify a standard php:5.6-fpm Dockerfile to include Redis. You do not simply run an apt-get install command for the extension itself within the image; instead, you install the build dependencies and use the provided installer script.
FROM php:5.6-fpm
# Install system dependencies needed to compile the redis module
RUN apt-get update && apt-get install -y \
libzip-dev \
zip \
&& docker-php-ext-install zip
# Now, add Redis support
# Note: The 'redis' package on Debian/Ubuntu usually contains both the PECL source and build tools
RUN apt-get update && apt-get install -y php5-redis \
&& rm -rf /var/lib/apt/lists/*
Waitâthere is a critical distinction here that often trips up developers. In many modern PHP versions, you might expect to use docker-php-ext-install redis. However, with older images like the php:5.6 tag referenced in the source material, the package structure differs slightly. The official Debian/Ubuntu repositories often provide php5-redis as a binary package that includes the compiled module and necessary headers.
If you are working with PHP 7 or later using the pecl approach, the command changes. With PECL (PHP Extension Community Library), which is the standard for redis starting from PHP 5.3 on Debian/Ubuntu repositories, the correct sequence involves installing the development package and then running the PECL installer.
FROM php:7.4-fpm
# Install dependencies for redis extension
RUN apt-get update && apt-get install -y \
libpcre3-dev \
&& pecl install redis
# Add the installed module to PHP's load path
RUN docker-php-ext-enable redis
The key takeaway is that you must determine whether the image version provides phpX-redis as a binary or expects you to use pecl. In the case of the older php:5.6 image, the documentation implies installing the module via PECL if it's not pre-installed. The command becomes:
RUN pecl install redis && docker-php-ext-enable redis
This approach is cleaner than trying to manually manage source downloads and compilation scripts because the php:5.6 Dockerfile already contains the necessary infrastructure to handle PECL installations, provided you have installed the corresponding -dev packages or simply rely on the repository's binary if available for that specific tag.
Remember to clean up the apt cache (rm -rf /var/lib/apt/lists/*) immediately after installation to keep your final image size small, as every megabyte counts in production environments. Also, ensure you restart php-fpm after enabling the extension so it loads into memory.
For projects utilizing Laravel or similar frameworks where caching is critical, ensuring the redis extension is present in your Docker build context is mandatory. If you encounter issues loading the module at runtime, check that the extension is listed in your php.ini, which docker-php-ext-enable handles automatically for most cases. Always verify the installation by running php -m | grep redis inside the container before committing to production builds.
