Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my Docker Symfony project consuming so much memory?

Description

I'm deploying my symfony project in prod. It works fine when I simply run docker-compose up. However I'm getting an issue with my deploy script and try accessing the web page in my navigator.

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 20480 bytes) in /var/www/redaph/vendor/monolog/monolog/src/Monolog/Handler/StreamHandler.php on line 107

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 65536 bytes) in /var/www/redaph/vendor/composer/ClassLoader.php on line 444

What is weird is that, in my dockerfile I specify that I want my PHP_MEMORY_LIMIT to be at 256M. When I enter my container I see the following:

root@125de315edca:/var/www/redaph# php -i | grep memory_limit
memory_limit => 128M => 128M

Question

Why is my Docker Symfony project consuming so much memory?

If this is normal then: How do I correctly increase the PHP_MEMORY_LIMIT in my dockerfile?

deploy_prod.sh

#!/usr/bin/env bash
PROJECT=symfony
docker-compose up -d
docker exec redaph_symfony_1 php bin/console d:s:u --force
docker exec redaph_symfony_1 php bin/console c:c

Dockerfile:

FROM php:7.2-apache

ENV \
    APACHE_ADMIN_EMAIL=webmaster@localhost \
    PHP_TIME_ZONE=Europe/London \
    PHP_MEMORY_LIMIT=256M \
    PHP_UPLOAD_MAX_FILESIZE=32M \
    PHP_POST_MAX_SIZE=32M

ARG WORK_DIR

WORKDIR $WORK_DIR

COPY composer.lock $WORK_DIR
COPY composer.json $WORK_DIR

ENV COMPOSER_ALLOW_SUPERUSER 1

RUN apt-get update \
    && apt-get install -y -f apt-transport-https \
        libicu-dev \
        libfreetype6-dev \
        libjpeg62-turbo-dev \
        libpng-dev \
        libpq-dev \
        acl \
        cron \
        git \
        zip \
    && pecl install mongodb \
    && docker-php-ext-enable mongodb \
    && docker-php-ext-install \
        exif \
        gd \
        intl \
        opcache \
        pdo_mysql \
        pdo_pgsql \
        zip \
    && curl -sS https://getcomposer.org/installer | php \
    && mv composer.phar /usr/local/bin/composer \
    && composer install --no-dev --prefer-dist --optimize-autoloader --no-scripts \
    && chown -R www-data:www-data $WORK_DIR \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/* \
    && a2enmod rewrite \
    && service cron start
like image 674
kemicofa ghost Avatar asked Jun 20 '18 17:06

kemicofa ghost


2 Answers

It seems more likely the problem is the code / configuration of your app. Docker may not need to be touched.

Depending on the handler you have configured, look into limiting monolog's buffer size (configuration code here -- search for buffer_size).

With Composer, run composer update locally in dev. When deploying, run composer install. update takes more memory than install in order to to resolve the dependency graph, assuming install has a composer.lock to work with.

It looks like that is what is being done based on the dockerfile you posted, but maybe something else is happening/interfering with that somewhere along the way. I would verify composer.lock contains what you think it does, just in case.

You can also try the flag memory_limit=-1 when you run composer:

php -d memory_limit=-1 composer install rest-of-args-here

If that's not enough, provide the version of Symfony you are using. Post your composer.json and Symfony configs related to monolog.

like image 95
jstur Avatar answered Sep 22 '22 02:09

jstur


It would help knowing what's on those lines where the errors are happening.

Regarding how to change your limit: MY "right way", there's basically 2 I can think of:

  • place a RUN in dockerfile with a one-liner edit, this should work https://serverfault.com/questions/96372/sed-php-ini-memory-limit.

  • keep a php.ini file on local which will be copied to the container. To make docker copy it, under your volume key in docker-compose file do a line

    • /ini/location/on/local:/ini/location/container

    I find this one messy for your case, i'd only do it if the file needed lots/continuous editing, which isn't the case.

There's another option but it would require you to do it every time you build which isn't really useful:

  • docker exec -it whatever bash

From there you just edit the file, assuming you've got the right tools (sed, vim, whatever you want).

like image 25
Sebi Ilie Avatar answered Sep 21 '22 02:09

Sebi Ilie