PHP memory size exhausted in docker with magento/bin setup:di:compile

PHP memory size exhausted in docker with magento/bin setup:di:compile

September 16, 2026 · Illustrated author avatarBy Stefan Bogdanescu
PHP memory size exhausted in docker with magento/bin setup:di:compile

Solving PHP Memory Exhaustion Errors in Magento Docker Containers

As a senior developer, I often encounter various challenges when working with e-commerce platforms like Magento on Docker containers. One common issue is the PHP memory size exhausted error that occurs during critical operations such as php bin/magento setup:di:compile. Let me guide you through this problem step-by-step.

Understanding the Memory Exhaustion Issue

The error typically appears in your log file:

[2019-02-04 12:15:26] main.ERROR: /usr/local/bin/php -f /var/www/html/m230/bin/magento setup:di:compile 2>&1
Compilation was started.
...
Proxies code generation... 1/7 [====>-----------------------]  14% 1 sec 76.0 MiB
Repositories code generation... 1/7 [====>-----------------------]  14% 1 sec 76.0 MiB
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 36864 bytes) in /var/www/html/m230/setup/src/Magento/Setup/Module/Di/Code/Reader/FileClassScanner.php on line 81
Check <https://getcomposer.org/doc/articles/troubleshooting.md#memory-limit-errors> for more info on how to handle out of memory errors. [] []

The Root Cause

This error occurs because the PHP process running the Magento command has exceeded its allocated memory limit. By default, Docker containers have limited resources, including memory.

Increasing Memory Limit in Your Docker Image

To resolve this issue, you should increase the PHP memory limit within your Docker container. You can do this by passing the -dmemory_limit flag when running the command:

php -dmemory_limit=1G bin/magento setup:di:compile

Or even set it to unlimited with:

php -dmemory_limit=-1 bin/magento setup:di:compile

However, these methods are temporary and do not persist across container restarts. To make the change permanent, update your PHP configuration file.

Setting Memory Limit in Your Dockerfile

In your Dockerfile, add the following line under the php:7.1-fpm base image to set the memory limit:

RUN echo "memory_limit = 2G" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini

Or if you prefer to use the www.conf file for FPM configuration, add this line instead:

RUN echo "php_admin_value[memory_limit] = 2G" >> /usr/local/etc/php-fpm.d/www.conf

Remember to restart your container after making these changes. The increased memory limit should now be available for your Magento processes.

Troubleshooting Tips

If the error persists, consider the following:

  1. Check Your Docker Image: Ensure you’re using a compatible image like php:7.1-fpm. If you’re switching between images like php:7.1-apache and php:7.1-fpm, there might be differences in how resources are allocated.

  2. Database Considerations: The memory issue could also stem from an insufficient database server. Ensure your MySQL or MariaDB server has enough RAM to handle Magento’s resource demands.

  3. PHP Configuration: Double-check your PHP configuration for any conflicting settings that might prevent the memory limit change from taking effect.

Conclusion

When encountering PHP memory size exhausted errors while working with Magento on Docker containers, remember to:

  • Increase the memory limit using the -dmemory_limit flag when running commands.
  • Persistently set the memory limit in your PHP configuration within the container’s context (either directly or via the FPM configuration file).
  • Regularly monitor and optimize your container resources based on your application’s needs.

By following these best practices, you should be able to resolve most memory-related issues in Magento Docker environments. If the problem persists, consider reaching out to a developer expert at LaravelCompany for further assistance with your specific setup.

Happy coding!

Extensions Deployment Errors CLI

Our network