skip to Main Content

Maybe memory overflow isn’t the correct description of what happening, but the point is: PHPUnit eat all of the memory and then dies the container.

Here is my phpunit.dockerfile:

FROM php:8.1-fpm-alpine

WORKDIR /var/www/html

RUN apk add --no-cache --repository http://dl-cdn.alpinelinux.org/alpine/edge/community/ --allow-untrusted gnu-libiconv
ENV LD_PRELOAD /usr/lib/preloadable_libiconv.so php

ENV PHP_MEMORY_LIMIT=1G
ENV PHP_UPLOAD_MAX_FILESIZE: 512M
ENV PHP_POST_MAX_SIZE: 512M

RUN docker-php-ext-install pdo

RUN apk add --no-cache libpng libpng-dev && docker-php-ext-install gd && apk del libpng-dev

RUN apk update 
    && apk upgrade 
    && apk add --no-cache 
        freetype 
        libpng 
        libjpeg-turbo 
        freetype-dev 
        libpng-dev 
        jpeg-dev 
        libwebp-dev 
        libjpeg 
        libjpeg-turbo-dev

RUN docker-php-ext-configure gd 
        --with-freetype=/usr/lib/ 
        --with-jpeg=/usr/lib/ 
        --with-webp=/usr

RUN NUMPROC=$(grep -c ^processor /proc/cpuinfo 2>/dev/null || 1) 
    && docker-php-ext-install -j${NUMPROC} gd

RUN apk add --no-cache sqlite-libs
RUN apk add --no-cache icu sqlite git openssh zip
RUN apk add --no-cache --virtual .build-deps icu-dev libxml2-dev sqlite-dev curl-dev
RUN docker-php-ext-install 
        bcmath 
        curl 
        ctype 
        intl 
        pdo 
        pdo_sqlite 
        xml
RUN apk del .build-deps

RUN docker-php-ext-enable pdo_sqlite

# Add xdebug
RUN apk add --no-cache --virtual .build-deps $PHPIZE_DEPS
RUN apk add --update linux-headers
RUN pecl install xdebug-3.1.5
RUN docker-php-ext-enable xdebug
RUN apk del -f .build-deps

# Configure Xdebug
RUN echo "xdebug.start_with_request=yes" >> /usr/local/etc/php/conf.d/xdebug.ini 
    && echo "xdebug.mode=coverage" >> /usr/local/etc/php/conf.d/xdebug.ini 
    && echo "xdebug.log=/var/www/html/xdebug/xdebug.log" >> /usr/local/etc/php/conf.d/xdebug.ini 
    && echo "xdebug.discover_client_host=1" >> /usr/local/etc/php/conf.d/xdebug.ini

RUN echo memory_limit = -1 >> /usr/local/etc/php/conf.d/docker-php-memlimit.ini;

I use docker-compose run --rm phpunit --coverage-html to start PHPUnit.

When I run PHPUnit without --coverage-html option, it runs smoothly. But with this option it start tests and new dots showing slowly and slowly and then stops showing new dots. But the processor usage grow up to 100%, memory usage also growing up to 100%, then swap is runs out, and then the container dies.

Is there any option to tell PHPUnit the maximum usable memory size or something other trick to avoid this issue?

2

Answers


  1. You can try to increase your memory limit manually via editing the line ENV PHP_MEMORY_LIMIT=1G. If you application is the reason for the high memory consumption, you should consider to fix memory leaks.

    Login or Signup to reply.
  2. I’m not sure about setting memory limit for PHPUnit, but, if you want to limit the memory/CPU usage of your docker, you can set in in docker-compose file similar to this.

    Just curious: Is the memory and CPU spike happening in non-docker environment as well?

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search