PHP memory size exhausted in docker with magento/bin setup:di:compile
When running php bin/magento setup:di:compile or php bin/magento deploy:mode:set production inside a php:7.1-fpm container, you can hit the dreaded "Allowed memory size" error immediately. The logs will show the compilation process starting, generating proxies and repositories, but then failing with a fatal error like Fatal error: Allowed memory size of 134217728 bytes exhausted.
The confusion usually stems from how PHP-FPM handles memory limits compared to CLI or Apache SGMH. The error message indicates the process tried to allocate roughly 36KB but was blocked because it had already consumed the limit set to 128MB (the default). You might try increasing the limit by prefixing the command with php -dmemory_limit=1G or even php -dmemory_limit=-1. While these commands work in a standard terminal, they do not resolve the issue inside a Docker container if the underlying PHP-FPM configuration is restrictive.
This discrepancy often appears when switching between images. If the same codebase runs fine on php:7.1-apache but fails on php:7.1-fpm, the root cause lies in how the FastCGI Process Manager (FPM) pools resources versus how CLI processes allocate them. The FPM pool configuration overrides runtime arguments passed via the shell, specifically for memory limits defined as php_admin_value.
The provided Dockerfile demonstrates a common setup where www.conf is configured to allow 2GB:
RUN echo "php_admin_value[memory_limit] = 2G" >> /usr/local/etc/php-fpm.d/www.conf
However, this directive only affects processes spawned by the FPM pool. The command php bin/magento setup:di:compile invokes PHP directly as a CLI process, not through the FPM pool. In many Dockerized environments using the official PHP images, the CLI entrypoint script checks and enforces the memory limit based on the global php.ini or specific CLI configurations, potentially ignoring the FPM-specific settings intended for web requests.
The critical distinction is that setup:di:compile is a heavy operation involving code generation for proxies and repositories. The error log shows it gets stuck at 0/7 or 1/7 items while consuming memory rapidly. When running under FPM, even direct CLI calls might inherit stricter constraints or behave differently regarding how the interpreter handles the memory_limit directive compared to Apache's SGMH module.
To reliably solve this, ensure that the PHP configuration applied globally is respected regardless of the entrypoint method. You can explicitly set the memory limit in the global php.ini inside the container:
echo "memory_limit = 2G" > /usr/local/etc/php/conf.d/docker-php-memory-limit.ini
Placing this in a dedicated .ini file ensures it takes precedence over defaults and applies to all PHP invocations, including CLI commands run via bin/magento. If you must use the FPM configuration, verify that your entrypoint script or Dockerfile explicitly overrides the limit for CLI processes, as simply modifying www.conf might not propagate to non-pool tasks.
Additionally, check if the image version has specific behaviors regarding memory limits in CLI mode versus web mode. Some official images enforce a hard cap on CLI operations to prevent runaway scripts from consuming host resources, whereas Apache setups are more lenient. Understanding these architectural differences prevents wasted time tweaking commands that don't actually change the execution context of the FPM container.