Enable SOAP for PHP in Docker Container
Enabling SOAP functionality in a Dockerized PHP environment often hits a wall immediately after you try to install the extension. The symptoms are distinct: apt-get install php-soap fails because the package manager sees only virtual packages or obsolete versions like php7.0-soap, while attempting to compile the extension via docker-php-ext-install soap throws a cryptic error about libxml2 not found.
This specific failure pattern, observed with PHP 7.2.x on Debian-based images, stems from how Alpine and Debian container variants handle dependencies differently. The error message regarding libxml2 is misleading because the C library itself is present inside the container (as evidenced by php_info showing version 2.9.4). The compiler cannot find it because the PHP build process relies on system headers that are not installed by default in minimal images like php:7.2-cli.
To resolve this, you must install the development headers for libxml2 before attempting to compile or install the SOAP extension. In a Debian-based container (which is standard for most non-Alpine PHP stacks), run the following command inside your container:
apt-get update && apt-get install -y libxml2-dev
Once the headers are installed, you can proceed with the compilation step using the official Docker build tools or manual compilation. If you are building a custom image based on php:7.2-cli, extend the Dockerfile as follows:
FROM php:7.2-cli
# Install dependencies required for extensions
RUN apt-get update && apt-get install -y \
libxml2-dev \
libcurl4-openssl-dev \
libzip-dev \
zip \
&& rm -rf /var/lib/apt/lists/*
# Install SOAP extension
RUN docker-php-ext-install soap
If you are running a command in an existing container, ensure libxml2-dev is installed first. Then, execute:
docker-php-ext-install soap
After installation, it is mandatory to restart your PHP-FPM service or refresh the OPcache if you are using PHP 7.1 and above, as the extension must be loaded into memory to take effect. You can verify the installation by creating a file named info.php with the content <?php phpinfo(); ?> and accessing it via a web server, then searching for the "SOAP" section in the output.
A common pitfall occurs when developers assume that because libxml2 is listed in the PHP runtime configuration (libxml2 Version => 2.9.4), the development headers are automatically available to the compiler. The build tools require the .h header files, which reside in a separate package (libxml2-dev). Without these headers, the configure script for the SOAP extension cannot locate the necessary XML parsing routines, leading to immediate compilation failure.
Another frequent issue involves Alpine Linux images (e.g., php:7.2-alpine). The command structure is entirely different there; you would use apk add --no-cache libxml-dev instead of apt-get. Mixing commands from Debian and Alpine ecosystems in the same mental model often leads to confusion. If you are on an Alpine base image, apt-get will not work at all, and attempting it will result in a "command not found" error or a failure to locate packages entirely.
If your application relies heavily on SOAP for legacy integrations, ensure that you do not accidentally disable the extension in your php.ini. Sometimes, installing the extension updates the configuration file location, but the active runtime might be pointing to an older php.ini inside /usr/local/etc/php/7.2/cli/conf.d. You may need to explicitly create a conf.d/soap.ini file with the line extension=soap.so if the automatic discovery does not pick it up.
For modern applications, consider whether SOAP is truly necessary or if XML-RPC or REST services would be a better architectural choice given the overhead of WSDL parsing and strict typing requirements inherent in SOAP. However, for maintaining legacy enterprise integrations where you cannot change the protocol, ensuring the correct dependency chain (headers -> compiler -> runtime) is the only viable path forward.