memory_limit does not change inside a PHP Docker container
Allowed memory size of 134217728 bytes exhaustedShort 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.
Fix it fast
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;"2Match what you see, then jump to its fix
- 1
php --ini lists your file nowhere under Scan for additional .ini filesThe ini file is not in a directory PHP scansFix - 2
php -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
php --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
ini_set('memory_limit', ...) in code changes nothing under FPMAn FPM pool setting locks the valueFix
- 1
What does the error look like?
Allowed memory size of 134217728 bytes exhaustedmemory_limit is still 128M after editing php.ini in the containerphp -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 = 512M3Verify
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
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.ini3Verify
Request the web check again and see the new value.
curl -s http://localhost:8080/check.phpConfirmed by php.net
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.ini3Verify
Read the limit again after the rebuild.
docker compose exec app php -r "echo ini_get('memory_limit'), PHP_EOL;"Confirmed by php.net
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] = 512M3Verify
Restart the FPM container and repeat the web check.
docker compose restart app
curl -s http://localhost:8080/check.phpConfirmed by php.net
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- Dispatch ProA real-time logistics platform: Flutter apps, Spring Boot services and a Kafka event pipeline on AWS.
- Spotless LaundryAn on-demand laundry app: Flutter on a Laravel back end, with Firebase for live updates.
Related fixes
Back end
PHP Fatal error: Allowed memory size of 134217728 bytes exhausted
PHP Fatal error: Allowed memory size of 268435456 bytes exhausted
The script used more memory than memory_limit allows.
4 causes, each with a fixBack 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 fixBack end
docker build stops at RUN composer install with exit code 1
Allowed memory size of ... bytes exhausted
Exit code 1 only says the RUN step failed.
4 causes, each with a fixBack end
php artisan serve starts but the app is not reachable, or the .env is ignored
Server stops right after start with a non-zero exit code when --port is given and the port is busy
php artisan serve listens on 127.0.0.1 port 8000 by default.
4 causes, each with a fixBack end
Guzzle or Http::get fails with cURL error 6 in Laravel
Illuminate\Http\Client\ConnectionException: cURL error 6: Could not resolve host
cURL error 6 means the machine running PHP could not turn the host name in the URL into an IP address.
3 causes, each with a fixBack end
Laravel cannot find the database host: getaddrinfo failed
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo for mysql failed
The message means PHP could not turn DB_HOST into an IP address.
3 causes, each with a fix