Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

memory_limit does not change inside a PHP Docker container

ErrorAllowed memory size of 134217728 bytes exhausted

Short answer

Your setting is being read from the wrong place or overridden. Ask PHP which ini files it loaded, using the same SAPI that fails: CLI or FPM. Then put the setting in a file in the conf.d directory, name it so it sorts last, and check for an FPM pool value that overrides it.

  • Applies to Docker, php official image 8.3 (cli and fpm), PHP 8.3
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Inside the container, ask the CLI which ini files it loads and what limit it sees. Do the same through the web SAPI with phpinfo(), because the two can differ.

    docker compose exec app php --ini
    docker compose exec app php -r "echo ini_get('memory_limit'), PHP_EOL;"
  2. 2Match what you see, then jump to its fix

    1. 1php --ini lists your file nowhere under Scan for additional .ini filesThe ini file is not in a directory PHP scansFix
    2. 2php -i shows the new limit, the browser request still hits the old oneYour check ran in the CLI, but the failing request runs in FPMFix
    3. 3php --ini shows your file and another file in conf.d, and the value is not yoursAnother ini file sorts after yours and sets memory_limit againFix
    4. 4ini_set('memory_limit', ...) in code changes nothing under FPMAn FPM pool setting locks the valueFix

What does the error look like?

  • Allowed memory size of 134217728 bytes exhausted
  • memory_limit is still 128M after editing php.ini in the container
  • php -i shows a new value but the web request still fails

Cause 1 of 4

The ini file is not in a directory PHP scans

1Check

The official image ships php.ini-development and php.ini-production but no active php.ini. Custom settings belong in the conf.d directory under PHP_INI_DIR.

docker compose exec app php --ini
docker compose exec app sh -c 'ls $PHP_INI_DIR $PHP_INI_DIR/conf.d'

2Fix

Copy a small ini file into conf.d in the Dockerfile, then rebuild the image.

# Dockerfile
COPY docker/php/memory.ini $PHP_INI_DIR/conf.d/zz-memory.ini

; docker/php/memory.ini
memory_limit = 512M

3Verify

Rebuild, then read the limit again.

docker compose build app && docker compose up -d app
docker compose exec app php -r "echo ini_get('memory_limit'), PHP_EOL;"

Confirmed by hub.docker.com

Did this fix it?

Cause 2 of 4

Your check ran in the CLI, but the failing request runs in FPM

1Check

PHP looks for php-SAPI.ini first and reads additional .ini files per setup, so the CLI and the web SAPI can load different configuration. Ask the web side directly.

# temporary route or file served by the web container
<?php echo ini_get('memory_limit'), ' ', php_ini_loaded_file();

2Fix

Set the value where the failing SAPI reads it, and keep the same conf.d file in the FPM container image. Remove the temporary file after the check.

# use the same image or the same COPY line for the php-fpm service
COPY docker/php/memory.ini $PHP_INI_DIR/conf.d/zz-memory.ini

3Verify

Request the web check again and see the new value.

curl -s http://localhost:8080/check.php

Confirmed by php.net

Did this fix it?

Cause 3 of 4

Another ini file sorts after yours and sets memory_limit again

1Check

Files in the scan directory are read in alphabetical order, so a file that sorts later can reset the value.

docker compose exec app sh -c 'grep -rn memory_limit $PHP_INI_DIR/conf.d'

2Fix

Rename your file so it sorts last, or remove the duplicate line.

COPY docker/php/memory.ini $PHP_INI_DIR/conf.d/zz-memory.ini

3Verify

Read the limit again after the rebuild.

docker compose exec app php -r "echo ini_get('memory_limit'), PHP_EOL;"

Confirmed by php.net

Did this fix it?

Cause 4 of 4

An FPM pool setting locks the value

1Check

A php_admin_value[memory_limit] line in a pool file cannot be overridden by ini_set(). Pool files for the image live in php-fpm.d.

docker compose exec app sh -c 'grep -rn "memory_limit" /usr/local/etc/php-fpm.d/'

2Fix

Change or remove the line in your own .conf file in php-fpm.d. Do not edit zz-docker.conf, the image documentation says it should not be overridden.

; /usr/local/etc/php-fpm.d/memory.conf
php_admin_value[memory_limit] = 512M

3Verify

Restart the FPM container and repeat the web check.

docker compose restart app
curl -s http://localhost:8080/check.php

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Keep ini files in the repository and COPY them into conf.d, so every environment gets the same limit.
  • Check with php --ini and with a web request, not only with one of them.
  • Raise memory_limit only after measuring the peak; -1 turns the limit off.
  • Use one image for the CLI jobs and the FPM service where you can.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

composer require laravel/horizon fails: ext-pcntl is missing

laravel/horizon v1.1.0 requires ext-pcntl * -> the requested PHP extension pcntl is missing from your system

Horizon needs the pcntl extension, and Composer refuses to install it where PHP lacks that extension.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.