Docker-php-ext-install mcrypt missing folder
When building a PHP container based on php:7.2-apache and attempting to compile extensions like mcrypt, you might encounter an error stating that /usr/src/php/ext/mcrypt does not exist. This error message is often misleading for modern developers because the root cause isn't actually a missing source directory within your Docker image, but rather a fundamental shift in how the official PHP images handle extension installation.
The command docker-php-ext-install relies on a specific build directory structure inside the base image (/usr/src/php). This directory contains the C source code for extensions that are not compiled into the binary by default. However, the mcrypt extension was deprecated and eventually removed from the PHP core repository years ago. In the context of the php:7.2 image family, the source code for mcrypt is no longer present in /usr/src/php/ext/, causing the build script to fail immediately with a "does not exist" error.
The official documentation and Stack Overflow threads often suggest installing libmcrypt-dev and running docker-php-ext-install. While this workflow works perfectly for extensions like mbstring or gd, it breaks down for mcrypt. The presence of the libmcrypt-dev package in your apt-get command does not magically recreate the missing PHP extension source code; it only provides the C library headers needed if the extension source were present.
To resolve this, you must acknowledge that mcrypt is no longer a viable option for new projects due to its cryptographic weaknesses (specifically reliance on outdated algorithms like MCRYPT_RIJNDAEL_128). If your application requires encryption functionality, switching to OpenSSL-based alternatives is the industry standard approach.
If you are maintaining legacy code that strictly depends on mcrypt, you can bypass the missing source issue by manually downloading the extension source or compiling against an older PHP version that still includes it in its default sources. However, a more pragmatic solution involves using the ext-mcrypt package from PECL if available for your specific OS version, though this is rarely recommended given the security implications.
A common workaround found in older tutorials involves cloning the extension source from GitHub directly into the build path, but this adds significant complexity to your Dockerfile and introduces dependency hell regarding libmcrypt versions. The most robust fix is simply to remove mcrypt from your RUN command.
If you must compile custom extensions that are present in the base image sources (like gd, mbstring, or zip), ensure you follow this pattern:
FROM php:7.2-apache
# Install required system dependencies for specific extensions
RUN apt-get update && apt-get install -y \
libfreetype6-dev \
libjpeg62-turbo-dev \
libpng-dev \
libmcrypt-dev # Only needed if you were actually compiling mcrypt, which is deprecated
# Compile and install extensions that exist in /usr/src/php/ext/
RUN docker-php-ext-install -j$(nproc) iconv gd mbstring zip
# Configure GD with specific image libraries if required
RUN docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
&& docker-php-ext-install -j$(nproc) gd
# Clean up to reduce image size
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
Note that libmcrypt-dev is included here only if you plan to compile a third-party replacement or if you are on a very old PHP version where it might still be valid. For standard php:7.2 images, removing the mcrypt argument from docker-php-ext-install will stop the build failure.
For Laravel applications specifically, relying on deprecated encryption methods like mcrypt is strongly discouraged. The LaravelCompany resources often recommend upgrading to modern cryptographic standards using Laravel's built-in EncryptedString or migrating to openssl_encrypt. If you are maintaining a legacy system that refuses to update, consider isolating that logic in a separate service container using an older PHP image tag (e.g., php:7.0-apache) solely for the duration of the encryption tasks, rather than trying to force it into a 7.2 environment where the source code simply doesn't exist.