How to Increase PHP Memory Limit in Docker Container: Fixing “Allowed Memory Size Exhausted” Error


2 views

When encountering memory limit errors in Docker, the first step is locating your PHP configuration files. Here are several approaches:


# Method 1: Check loaded configuration files
docker exec -it your_container_name php -i | grep "Loaded Configuration File"

# Method 2: Find all possible php.ini locations
docker exec -it your_container_name find / -name "php.ini" 2>/dev/null

Once you've located your php.ini file, you have several options to increase the memory limit:

Option 1: Direct File Modification


# First copy the php.ini file to your host for editing
docker cp your_container_name:/path/to/php.ini ./php.ini

# Edit the memory_limit value (typically around line 400)
memory_limit = 512M  # or higher value like 1024M

# Copy the modified file back
docker cp ./php.ini your_container_name:/path/to/php.ini

Option 2: Using Docker Compose

For a more maintainable solution, modify your docker-compose.yml:


version: '3'
services:
  your_service:
    image: your_php_image
    environment:
      - PHP_MEMORY_LIMIT=512M
    # Alternative approach for custom php.ini
    volumes:
      - ./custom-php.ini:/usr/local/etc/php/conf.d/custom.ini

Option 3: Runtime Configuration

For temporary changes or debugging:


# Directly in PHP script
ini_set('memory_limit', '512M');

# Via command line when running scripts
docker exec your_container_name php -d memory_limit=512M your_script.php

After making changes, verify the new memory limit:


docker exec -it your_container_name php -i | grep memory_limit

If changes don't take effect:

  • Check for multiple php.ini files in different directories
  • Look for .user.ini files that might override settings
  • Verify container restart after configuration changes
  • Check for PHP-FPM pool configuration that might impose lower limits

Instead of arbitrarily increasing memory limits:

  • Profile your application to identify memory leaks
  • Implement proper chunking for large data processing
  • Consider upgrading to newer PHP versions with better memory management
  • Set appropriate limits in both PHP and Docker container configuration

When working with PHP in Docker, the configuration file location varies depending on your image. Here's how to find it:


# Execute into your running container
docker exec -it your_container_name bash

# Search for php.ini locations
php --ini | grep "Loaded Configuration File"

# Common locations in popular images:
# - Official PHP images: /usr/local/etc/php/php.ini
# - Alpine-based images: /etc/php8/php.ini
# - Ubuntu-based images: /etc/php/8.x/cli/php.ini

For temporary testing or quick fixes, you can override the memory limit without editing php.ini:


# Runtime override (in your PHP script)
ini_set('memory_limit', '512M');

# Docker run command override
docker run -e PHP_INI_MEMORY_LIMIT=512M your_image_name

# In docker-compose.yml
services:
  app:
    environment:
      - PHP_INI_MEMORY_LIMIT=512M

For permanent changes, consider these approaches:


# Option 1: Custom Dockerfile
FROM php:8.2-apache
RUN echo "memory_limit = 512M" > /usr/local/etc/php/conf.d/memory-limit.ini

# Option 2: Mount custom config file
# docker-compose.yml
services:
  app:
    volumes:
      - ./custom-php.ini:/usr/local/etc/php/conf.d/custom.ini

If changes don't take effect:

  • Verify the active php.ini: php --ini
  • Check for multiple PHP installations
  • Restart your container after changes
  • Verify permissions on custom config files

While increasing memory limit solves immediate issues, consider:


# Alternative optimization techniques
# Batch processing for large datasets
$batchSize = 1000;
foreach (array_chunk($largeArray, $batchSize) as $batch) {
    processBatch($batch);
}

# Using generators for memory efficiency
function generateLines($file) {
    while (!feof($file)) {
        yield fgets($file);
    }
}