Video Lesson: Increase the PHP memory limit in php.ini for Docker

Increase the PHP memory limit in php.ini for Docker

Learn how to customize PHP configuration in Docker by overriding the default php.ini settings for a project.


Lesson Content

To increase PHP’s memory limit in docker-magento, first check the current value:

bin/cli php -i | grep memory_limit

The output shows the current PHP memory limit, which is 4 gigabytes in this example.

The PHP image includes a php.ini file that defines PHP’s configuration, including memory_limit. We override that file with a project-specific copy so that our changes remain separate from the files provided by docker-magento.

Create the configuration directory from the project root:

mkdir -p images/php/conf

Open the php.ini file from the PHP image version used by your project. You can find it in the docker-magento repository under images -> php -> your PHP version. Copy the complete contents of that file into a new project file at images/php/conf/php.ini.

Change the memory_limit value to 8G:

memory_limit = 8G

You can make other PHP configuration changes in this file as well, but changing the memory limit is enough for this example.

Next, mount the project file into the PHP container. Open docker-compose.dev.yml and find the existing host bind mounts. The phpfpm service receives its volumes through YAML anchors from the app service. Because YAML anchors cannot be combined with additional values in this configuration, add the new mount to the app service’s volumes instead.

Add a cached bind mount for the local configuration file:

- ./images/php/conf/php.ini:/usr/local/etc/php/php.ini:cached

The destination comes from the PHP Dockerfile, which copies its default configuration into /usr/local/etc/php. Mounting our file at /usr/local/etc/php/php.ini replaces the original configuration when the container starts. The app service is the Nginx container, but it does not matter that the file is mounted there as well because Nginx does not use this PHP configuration file.

Restart the Docker containers so the new mount takes effect:

bin/restart

Check the memory limit again:

bin/cli php -i | grep memory_limit

The output now shows the updated value of 8G.

Maggie

Hey, I'm Maggie! 👋

Ask me anything.

Want to chat with Maggie?

Log in for AI tutor access.

Comments

Join the discussion!

Notes

Keep track of what you learn!