skip to Main Content

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

SymfonyComponentDebugExceptionFatalErrorException : 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.

4

Answers


  1. Chosen as BEST ANSWER

    Thanks for the suggestions. I have used php.ini based on the 2 files and after reloading nginx it worked as intended. Alternatively I also tried ini_set right before

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

    and it also did the job. Previously I ini_set in another file but after further reading I hace come to realized it just set for the current working file and not globally.


  2. 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
    
    Login or Signup to reply.
  3. 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
    
    Login or Signup to reply.
  4. It’s work for me:

    1. enter inside conteiner with php
    2. cd /usr/local/etc/php
    3. echo ‘memory_limit = 512M’ >> php.ini
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search