Can't Update php.ini File in Docker Container
You've confirmed that your custom php.ini file sits correctly in /usr/local/etc/php/ inside the container and that PHP is indeed reading it during initialization, yet your runtime settings like memory_limit and max_execution_time remain unchanged. This discrepancy usually points to a specific architectural pattern used in Dockerized PHP environments: OPcache caching configuration data at the binary or process level, rather than a simple file-system race condition.
The root cause here is almost certainly that you are running an older version of PHP (specifically 7.0-fpm based on your Dockerfile). In these versions, OPcache often caches the php.ini directives when the FPM master process starts. If you copy a new php.ini while the container is already running, or if the directive changes require a restart that isn't triggering correctly, the in-memory configuration remains stale.
Looking at your Dockerfile, you are copying the config file directly into the image:
COPY config/php.ini /usr/local/etc/php/
Because this happens during the RUN or COPY instruction execution, the resulting image layer is immutable once built. If you modify config/php.ini on your host machine and run docker-compose up, Docker reuses the existing layers unless you rebuild the image. However, even if you do rebuild the image, the FPM master process inside the container might have already loaded the configuration into memory before your changes took effect in the running instance.
The Correct Approach: Rebuild and Restart
To apply a custom php.ini in a Dockerized PHP setup, you must ensure two things happen in strict sequence:
- Rebuild the Image: This ensures the new file is baked into the filesystem layer.
- Restart the Containers: This forces the PHP-FPM master process to read the new configuration from disk during initialization.
Your current docker-compose.yml defines a standalone php service that builds an image and runs as FPM. The command "true" suggests you aren't running any specific application inside that container, which is fine for testing config, but it implies the container might be starting without proper health checks or entrypoint handling.
Here is the workflow to resolve the issue:
- Stop all services:
docker-compose down - Remove the old image (optional but recommended to ensure a clean slate):
docker rmi $(docker images -q mymagento2docker_php_1) - Verify your
php.inichanges: Ensurememory_limit = 2Gandmax_execution_time = 800are at the top of the file or commented out in a way that doesn't conflict with defaults, though for FPM they usually just need to be present. - Rebuild and start:
docker-compose build php --no-cache docker-compose up -d
Once the containers are back up, verify the change inside the running container:
docker exec -it mymagento2docker_php_1 bash
php -i | grep memory_limit
# Output should now show 2G
Why Your Previous Attempts Failed
You attempted to restart Nginx via various methods (/etc/init.d/nginx restart, nginx -s reload). This failed because Nginx is irrelevant to PHP-FPM configuration. Nginx acts as a reverse proxy; it forwards requests to the PHP-FPM socket or port. Changing php.ini settings affects the PHP runtime, not the Nginx server block. Even if you successfully reloaded Nginx, the PHP-FPM master process (running in your container) would still be holding the old configuration in memory.
Furthermore, your app service uses image: php:7.0-fpm with a volume mount and command: "true". This creates a container that runs the base FPM image but does nothing else. If you intended to run Magento 2 inside this specific container (based on the volume mount ./magento2:/var/www/html), note that Magento 2 typically expects a specific entrypoint script or needs to be aware of the web server configuration. However, strictly regarding the php.ini issue, the stale cache in the PHP-FPM master process is the culprit.
Alternative: Using Environment Variables
If rebuilding images and restarting containers feels too heavy for your workflow, consider using environment variables to override specific settings for PHP-FPM. This is a common pattern in modern PHP deployments (including Laravel and Symfony ecosystems).
You can define these directly in docker-compose.yml:
php:
build: ./php/
environment:
- MEMORY_LIMIT=2G
- MAX_EXECUTION_TIME=800
Note: This works reliably for many directives, but not all. For critical performance tuning or complex configurations, modifying the php.ini file and rebuilding remains the most robust solution.
If you are deploying a production Magento 2 instance, ensure your PHP version is updated to at least 7.4 or 8.x, as PHP 7.0 is End of Life and lacks many modern security patches and performance improvements available in newer Docker images.
