PHP 内存大小在 docker 和 magento/bin setup:di:compile 中耗尽

PHP memory size exhausted in docker with magento/bin setup:di:compile

我在 运行 php bin/magento setup:di:compilephp bin/magento deploy:mode:set production 之后 php:7.1-fpm 图像容器中的日志文件中收到此错误。

[2019-02-04 12:15:26] main.ERROR: /usr/local/bin/php -f /var/www/html/m230/bin/magento setup:di:compile 2>&1 Compilation was started. %message% 0/7 [>---------------------------] 0% < 1 sec 72.0 MiB%message% 0/7 [>---------------------------] 0% < 1 sec 72.0 MiBProxies code generation... 0/7 [>---------------------------] 0% < 1 sec 72.0 MiB Proxies code generation... 1/7 [====>-----------------------] 14% 1 sec 76.0 MiB Repositories code generation... 1/7 [====>-----------------------] 14% 1 sec 76.0 MiB Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 36864 bytes) in /var/www/html/m230/setup/src/Magento/Setup/Module/Di/Code/Reader/FileClassScanner.php on line 81 Check https://getcomposer.org/doc/articles/troubleshooting.md#memory-limit-errors for more info on how to handle out of memory errors. [] []

我试图通过 运行ning php -dmemory_limit=1G bin/magento setup:di:compile 甚至 php -dmemory_limit=-1 bin/magento setup:di:compile

来增加内存限制

如果我使用php:7.1-apache图像就不会出现这个错误(我使用相同的源代码和数据库,只是改变图像)

我的笔记本电脑 (运行ning arch-linux) 和台式机 (运行ning ubuntu) 都会出现此错误。我知道他们足够强大 运行 这个命令。

我使用的 Dockerfile:

FROM php:7.1-fpm

# Install necessary libraries for Magento2
RUN apt-get -y update \
    && apt-get install -y \
        libmcrypt-dev \
        libxslt-dev \
        zlib1g-dev \
        libpng-dev \
        libjpeg-dev \
        libfreetype6-dev \
        libjpeg62-turbo-dev
RUN docker-php-ext-install -j$(nproc) iconv
RUN docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/
RUN docker-php-ext-install pdo_mysql mcrypt xsl intl zip bcmath -j$(nproc) gd soap

# Install xdebug
RUN pecl install xdebug
RUN docker-php-ext-enable xdebug
RUN echo "xdebug.remote_enable=on\n\
xdebug.remote_autostart=off\n\
xdebug.remote_host=10.5.0.1\n\
xdebug.remote_port=9000\n\
xdebug.remote_handler=dbgp" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini

# Set memory_limit
RUN echo "php_admin_value[memory_limit] = 2G" >> /usr/local/etc/php-fpm.d/www.conf

# Install cron
#RUN apt-get install -y cron

# Remove apt cache
RUN rm -rf /var/lib/apt/lists/*

# Create non-root user
ARG USER_NAME
ARG UID
RUN useradd -m -U ${USER_NAME} -u ${UID} -p1 -s /bin/bash -G root -o

# Edit PS1 in basrc
RUN echo "PS1='${debian_chroot:+($debian_chroot)}\w$ '" >> /home/${USER_NAME}/.bashrc

# Change www-data user to ${USER_NAME}
RUN sed -i -e "s/www-data/${USER_NAME}/" /usr/local/etc/php-fpm.d/www.conf

在docker-compose.yml

php:
    container_name: php-server
    build: ./php/
    volumes:
      - ./..:/var/www
      - ./php/ini:/usr/local/etc/php // note that this line maps docker's /usr/local/etc/php

在 ini/conf.d/ 下的 php Dockerfile 目录中创建一个名为 memory_limit.ini 的文件 所以你的目录会在.../php/ini/conf.d/memory_limit.ini里面memory_limit.ini

memory_limit = -1

您必须在 php docker 容器中更改 PHP cli 的内存限制。

Docker 方式

在您的 docker 容器 bash ~$ php -r "echo ini_get('memory_limit').PHP_EOL;" 上通过 运行 此命令确认您的内存不足。它应该输出 128M。要增加,请检查容器中是否存在 /usr/local/etc/php/conf.d/ 路径。如果您的 php 存储库是从官方 Docker 中心构建和维护的,我相信应该如此。 如果存在:


~$ cd /usr/local/etc/php/conf.d/
#create and or update a file called docker-php-memlimit.ini (comment)
#in /usr/local/etc/php/conf.d/(comment)
~$ echo 'memory_limit = -1' >> /usr/local/etc/php/conf.d/docker-php-memlimit.ini
#from the line below we setting memory to unlimited by using -1 (comment)
#Or you can set amount allocated by changing 'memory_limit = 512M' (comment)

# then check the memory with the following code (comment)
~$ php -r "echo ini_get('memory_limit').PHP_EOL;"

为了避免 运行 bash 每次需要安装它,请将文件添加到 docker 文件中,如下所示:


#dockerfile
RUN cd /usr/local/etc/php/conf.d/ && \
  echo 'memory_limit = -1' >> /usr/local/etc/php/conf.d/docker-php-memlimit.ini

如果 docker 构建无法将目录更改为 /usr/local/etc/php/conf.d/,它将中断。您可以通过其他方式从您的存储库中浏览。

参考文献:https://github.com/nextcloud/docker/issues/447