swoole / docker-swoole

🏄 Official Docker Image of Swoole
https://hub.docker.com/r/phpswoole/swoole
Apache License 2.0
534 stars 113 forks source link

> 300MB RAM usage on server start (Docker/Octane) #38

Closed foremtehan closed 1 year ago

foremtehan commented 1 year ago

With this dockerfile:

FROM phpswoole/swoole:php8.1

WORKDIR /var/www/html

COPY --from=composer /usr/bin/composer /usr/bin/composer

RUN composer create-project laravel/laravel .

RUN mv ./.env.example .env

RUN php artisan key:generate

RUN composer require laravel/octane

RUN php artisan octane:install --server=swoole

EXPOSE 9000

ENTRYPOINT ["/bin/bash", "-c", "php /var/www/html/artisan octane:start --server=swoole  --host=0.0.0.0 --port=9000 --max-requests=1500"]

docker build -t octane .

docker run -d -p 9000:9000 --name=dump octane

then docker stats and you will see:

image

As you can see when the server started with laravel octane it allocated 300 mb of ram, What cause this ? Or is it normal ?

liulyn commented 1 year ago

邮件已收到 谢谢!

deminy commented 1 year ago

I started with a simple web server from example 01-basic. Here is what I saw:

docker-swoole deminy$ ./bin/example.sh start 01
docker-swoole deminy$ docker stats --no-stream
CONTAINER ID   NAME             CPU %     MEM USAGE / LIMIT    MEM %     NET I/O      BLOCK I/O     PIDS
cc7b39ac51ff   01-basic-app-1   0.02%     23.7MiB / 7.773GiB   0.30%     3kB / 714B   0B / 12.3kB   3

As shown, the Docker container used 23.7MiB memory. Of course, it should take more memory to run Octane applications.

I adjusted your Dockerfile a little, mainly to consolidate RUN commands and remove Composer caches (please ignore other changes like working directory and port number):

FROM phpswoole/swoole:php8.1

RUN \
    set -ex && \
    rm -rf * && \
    composer create-project laravel/laravel . && \
    mv ./.env.example .env && \
    ./artisan key:generate && \
    composer require laravel/octane && \
    composer clearcache && \
    ./artisan octane:install --server=swoole

ENTRYPOINT ["/bin/bash", "-c", "./artisan octane:start --server=swoole --host=0.0.0.0 --port=9501 --max-requests=1500"]

Here are the stats after making one HTTP call to the Octane server:

CONTAINER ID   NAME      CPU %     MEM USAGE / LIMIT     MEM %     NET I/O           BLOCK I/O     PIDS
1487ba5bffa4   octane    0.18%     181.8MiB / 7.773GiB   2.28%     3.64kB / 19.5kB   0B / 36.9kB   15

As shown, the Docker container used 181.8MiB memory.

deminy commented 1 year ago

I did another test using your Dockerfile, and it used 182.4MiB memory, almost the same as the one from my previous comment.

There are other optimizations to make the container (slightly) faster, e.g., exclude dev packages from Composer, use the Alpine image instead, etc. However, I doubt there could be many improvements.