为什么我的Docker Symfony项目要消耗这么多内存?

时间:2022-01-16 15:37:50

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.

我正在prod中部署symfony项目,当我简单地运行docker- composer时,它可以正常工作。但是,我的部署脚本有问题,请尝试在导航器中访问web页面。

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

致命错误:允许内存大小为134217728字节(试图分配20480字节),在/var/www/redaph/vendor/monolog/monolog/src/Monolog/Handler/StreamHandler中。php在第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

致命错误:允许内存大小为134217728字节(试图分配65536个字节)在/var/www/redaph/vendor/composer/ClassLoader中。php在第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:

奇怪的是,在dockerfile中,我指定要将PHP_MEMORY_LIMIT设置为256M。当我进入我的容器,我看到如下:

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?

为什么我的Docker Symfony项目要消耗这么多内存?

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

如果这是正常的,那么:如何正确地增加dockerfile中的PHP_MEMORY_LIMIT ?

deploy_prod.sh

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:

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

2 个解决方案

#1


0  

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

看起来更可能的问题是你的应用程序的代码/配置。Docker可能不需要被触碰。

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

根据您配置的处理程序,查看限制monolog的缓冲区大小(这里的配置代码——搜索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.

使用Composer,在dev中本地运行Composer更新。部署时,运行Composer安装。为了解决依赖关系图,更新需要比安装更多的内存,假设安装有一个编写器。锁。

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.

看起来这是基于你发布的dockerfile所做的事情,但是可能在这个过程的某个地方发生了其他的事情。我将核实作曲家。锁包含你认为它的功能,以防万一。

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

您还可以在运行composer时尝试一下flag memory_limit=-1:

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

php -d memory_limit=-1编写器安装rest-of-args

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

如果这还不够,提供您正在使用的Symfony版本。把你的作曲家。json和Symfony configs与独白相关。

#2


0  

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:

关于如何改变你的限制:我的“正确的方式”,基本上有2个我可以想到的:

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

    在dockerfile中使用一行编辑器进行运行,这应该可以工作为https://serverfault.com/questions/96372/atedphp -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

    保持一个php。本地的ini文件,将被复制到容器中。要使docker复制它,请在docker- composer文件中的卷键下做一行

    • /ini/location/on/local:/ini/location/container
    • ini /位置/ /本地:/ ini /位置/容器

    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
  • docker执行程序——无论如何

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

从那里你只要编辑文件,假设你有正确的工具(sed, vim,任何你想要的)。

#1


0  

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

看起来更可能的问题是你的应用程序的代码/配置。Docker可能不需要被触碰。

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

根据您配置的处理程序,查看限制monolog的缓冲区大小(这里的配置代码——搜索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.

使用Composer,在dev中本地运行Composer更新。部署时,运行Composer安装。为了解决依赖关系图,更新需要比安装更多的内存,假设安装有一个编写器。锁。

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.

看起来这是基于你发布的dockerfile所做的事情,但是可能在这个过程的某个地方发生了其他的事情。我将核实作曲家。锁包含你认为它的功能,以防万一。

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

您还可以在运行composer时尝试一下flag memory_limit=-1:

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

php -d memory_limit=-1编写器安装rest-of-args

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

如果这还不够,提供您正在使用的Symfony版本。把你的作曲家。json和Symfony configs与独白相关。

#2


0  

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:

关于如何改变你的限制:我的“正确的方式”,基本上有2个我可以想到的:

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

    在dockerfile中使用一行编辑器进行运行,这应该可以工作为https://serverfault.com/questions/96372/atedphp -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

    保持一个php。本地的ini文件,将被复制到容器中。要使docker复制它,请在docker- composer文件中的卷键下做一行

    • /ini/location/on/local:/ini/location/container
    • ini /位置/ /本地:/ ini /位置/容器

    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
  • docker执行程序——无论如何

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

从那里你只要编辑文件,假设你有正确的工具(sed, vim,任何你想要的)。