Docker: how to increase php memory "the docker way"

root's picture

I got this error today while trying to upgrade some php application:

Fatal error encountered: /var/www/html/libs/Zend/Db/Statement/Pdo.php(292): Allowed memory size of 134217728 bytes exhausted (tried to allocate 20480 bytes)

UPS :) Quick! Go check your memory limits... pull your hair out, run around the house senseless! WE ARE DOOMED!
Well, the fix is easy. Read on (or watch the video).

1. Confirm the current php memory limit:

# php -r "echo ini_get('memory_limit').PHP_EOL;"

2. Create a new configuration file or add in an existing one, in /usr/local/etc/php/conf.d/:

# echo 'memory_limit = -1' >> /usr/local/etc/php/conf.d/docker-php-ram-limit.ini

3. Check if memory limit was changed:

# php -r "echo ini_get('memory_limit').PHP_EOL;"

THIS MODIFICATION IS INSTANT - no need to restart anything. Cute, right? :)
However, do read what follows.

Demo

To avoid running this every time you recreate/restart the container (on restart this is erased too), you can create a custom image by adding what follows to the dockerfile:

RUN cd /usr/local/etc/php/conf.d/ && \
  echo 'memory_limit = -1' >> /usr/local/etc/php/conf.d/docker-php-ram-limit.ini

Thou shalt not steal!

If you want to use this information on your own website, please remember: by doing copy/paste entirely it is always stealing and you should be ashamed of yourself! Have at least the decency to create your own text and comments and run the commands on your own servers and provide your output, not what I did!

Or at least link back to this website.