Home > Net >  increase memory_limit doesn't work in PHP Container in Docker
increase memory_limit doesn't work in PHP Container in Docker

Time:11-16

I'm using a container in Docker to host my Laravel app and this container is connected to another container using Nginx to host it. I'm trying to import a SQL file into my Laravel app with a seeder that's like this

$path = public_path('sql/2022_11_16_import_table.sql');
        $sql = file_get_contents($path);
        DB::unprepared($sql);

However, it displayed the error

Symfony\Component\Debug\Exception\FatalErrorException : Allowed memory size of 134217728 bytes exhausted (tried to allocate 186885432 bytes) at /var/www/database/seeds/SqlFileSeeder.php:16

I have figured this is due to my memory_limit in php.ini is being 128M so I went ahead and changed it by changing the php.ini-development and php.ini-production file inside the PHP container and then restart the whole container and the Nginx container. However, when I tried

php -ini 

again the memory_limit is still 128M=>128M despite both the php.ini file has been changed. I have also tried ini_set('memory_limit','200M'); but it still seems to have no effect.

CodePudding user response:

You can add a command at the end of your docker file to copy the php.ini-production or php.ini-development to /usr/local/etc/php/php.ini.

Something like this:

RUN cp /usr/local/etc/php/php.ini-production /usr/local/etc/php/php.ini && \
        sed -i -e "s/^ *memory_limit.*/memory_limit = 4G/g" /usr/local/etc/php/php.ini

CodePudding user response:

php.ini-production file will not read. PHP initialization work with php.ini files, which are end .ini extension. You can add volume in Docker for main php.ini files like that:

volumes:
   - 'configs/php.ini:/usr/local/etc/php/php.ini'

Content of php.ini

 memory_limit = 200M

If you will do it manually after docker compose up, then you will need to run some commands:

Enter to Docker container

docker compose -it my_container_name bash

Restart PHP-fpm and Nginx services

service php-fpm restart

service nginx reload
  • Related