tobybatch / kimai2

Docker containers for the kimai2 web application including docker-compose and kubernetes/helm deployment.
MIT License
183 stars 97 forks source link

[DOCS] allow memory_limit to be set at runtime #426

Closed clyso-dr closed 1 year ago

clyso-dr commented 1 year ago

Describe the bug The environment variable does not work.

To Reproduce

www-data@kimai-kimai2-68c4b57fd-lgl9n:/opt/kimai/var/plugins$ env | grep memory
memory_limit=256
www-data@kimai-kimai2-68c4b57fd-lgl9n:/opt/kimai/var/plugins$ ../../bin/console kimai:reload

Reloading configurations ...
============================

 [OK] All 40 YAML files contain valid syntax.

 [OK] All 514 XLIFF files contain valid syntax.

 Rebuilding your cache, please be patient ...

 // Clearing the cache for the dev environment with debug true

PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 53248 bytes) in /opt/kimai/vendor/twig/twig/src/Cache/FilesystemCache.php on line 42
[2022-11-07 10:12:19] php.CRITICAL: Fatal Error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 53248 bytes) {"exception":"[object] (Symfony\\Component\\ErrorHandler\\Error\\OutOfMemoryError(code: 0): Error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 53248 bytes) at /opt/kimai/vendor/twig/twig/src/Cache/FilesystemCache.php:42)"} []
10:12:19 CRITICAL  [php] Fatal Error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 53248 bytes) ["exception" => Symfony\Component\ErrorHandler\Error\OutOfMemoryError^ { …}]

In FilesystemCache.php line 42:

  Error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 53248 bytes)

kimai:reload [-h|--help] [-q|--quiet] [-v|vv|vvv|--verbose] [-V|--version] [--ansi] [--no-ansi] [-n|--no-interaction] [-e|--env ENV] [--no-debug] [--] <command>

Desktop (please complete the following information):

Command used to run the container

kevinpapst commented 1 year ago

Try php -D memory_limit=1G ../../bin/console kimai:reload

clyso-dr commented 1 year ago

yes, with -d instead of -D its working

www-data@kimai-kimai2-68c4b57fd-lgl9n:/opt/kimai/var/plugins$ php -d memory_limit=1G ../../bin/console kimai:reload

Reloading configurations ...
============================

 [OK] All 40 YAML files contain valid syntax.

 [OK] All 514 XLIFF files contain valid syntax.

 Rebuilding your cache, please be patient ...

 // Clearing the cache for the dev environment with debug true

 [OK] Cache for the "dev" environment (debug=true) was successfully cleared.

 // Warming up the cache for the dev environment with debug true

 [OK] Cache for the "dev" environment (debug=true) was successfully warmed.

 [OK] Kimai config was reloaded
kevinpapst commented 1 year ago

added docs for the next one #427

clyso-dr commented 1 year ago

But I get the memory error also on the start up of the container not only by the reload.

kevinpapst commented 1 year ago

https://tobybatch.github.io/kimai2/runtime-args.html#runtime-arguments

If 512M is not sufficient, something is wrong.

clyso-dr commented 1 year ago

Its 256M:

www-data@kimai-kimai2-68c4b57fd-lgl9n:/opt/kimai/var/plugins$ env | grep memory
memory_limit=256

And if I do this with 256M its working, with 128 not:

www-data@kimai-kimai2-68c4b57fd-lgl9n:/opt/kimai/var/plugins$ php -d memory_limit=256M ../../bin/console kimai:reload

Reloading configurations ...
============================

 [OK] All 40 YAML files contain valid syntax.

 [OK] All 514 XLIFF files contain valid syntax.

 Rebuilding your cache, please be patient ...

 // Clearing the cache for the dev environment with debug true

 [OK] Cache for the "dev" environment (debug=true) was successfully cleared.

 // Warming up the cache for the dev environment with debug true

 [OK] Cache for the "dev" environment (debug=true) was successfully warmed.

 [OK] Kimai config was reloaded

With 128M:

www-data@kimai-kimai2-68c4b57fd-lgl9n:/opt/kimai/var/plugins$ php -d memory_limit=128M ../../bin/console kimai:reload

Reloading configurations ...
============================

 [OK] All 40 YAML files contain valid syntax.

 [OK] All 514 XLIFF files contain valid syntax.

 Rebuilding your cache, please be patient ...

 // Clearing the cache for the dev environment with debug true

PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 32768 bytes) in /opt/kimai/var/cache/de_/twig/1e/1e5e0c3831f3cffe97cf0bbb29bf4ede.php on line 87
[2022-11-07 12:33:35] php.CRITICAL: Fatal Error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 32768 bytes) {"exception":"[object] (Symfony\\Component\\ErrorHandler\\Error\\OutOfMemoryError(code: 0): Error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 32768 bytes) at /opt/kimai/var/cache/de_/twig/1e/1e5e0c3831f3cffe97cf0bbb29bf4ede.php:87)"} []
12:33:35 CRITICAL  [php] Fatal Error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 32768 bytes) ["exception" => Symfony\Component\ErrorHandler\Error\OutOfMemoryError^ { …}]

In 1e5e0c3831f3cffe97cf0bbb29bf4ede.php line 87:

  Error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 32768 bytes)

kimai:reload [-h|--help] [-q|--quiet] [-v|vv|vvv|--verbose] [-V|--version] [--ansi] [--no-ansi] [-n|--no-interaction] [-e|--env ENV] [--no-debug] [--] <command>

So as I said by opening the ticket, the feature is not working...

tobybatch commented 1 year ago

@clyso-dr which kimai image are you using?

clyso-dr commented 1 year ago

container version: apache-latest (1.28.1)

tobybatch commented 1 year ago

I don't use helm to start containers but it seems to work on the CLI. Could you confirm that if your run the same command you see the same outcome.

tobias@tobias:~$ docker run --rm -ti -e memory_limit=512 --entrypoint /bin/bash kimai/kimai2:apache-latest
Unable to find image 'kimai/kimai2:apache-latest' locally
apache-latest: Pulling from kimai/kimai2
4b7b4a8876e2: Already exists 
... snipped ...
804217f9c0ce: Pull complete 
Digest: sha256:c40f2b5edc14579de187db1e7fb82bdbf07e1ae0f6d1e08bdc281348b4033f1f
Status: Downloaded newer image for kimai/kimai2:apache-latest
www-data@6d4c41c76ea1:~/html$ env | grep memory_limit
memory_limit=512
www-data@6d4c41c76ea1:~/html$ 
clyso-dr commented 1 year ago

That we do not miss understand us, the variable is there and I added the feature to adjust it via the helm chart (https://github.com/robjuz/helm-charts/pull/26/files) so this is working, I just added 256M instead of 512M but the php of the container is still using 128M, expect I set manually "php -d....". So variable is there but without function in my perspective.

clyso-dr commented 1 year ago

Bildschirmfoto 2022-11-07 um 15 28 04

www-data@kimai-kimai2-68c4b57fd-lgl9n:/opt/kimai/var/plugins$ env | grep memory
memory_limit=256
tobybatch commented 1 year ago

So I'd guess that this is an issue with how the helm chart is starting the container. When it starts using the native docker daemon it correctly sets the env variable and the docker memory limit.

I don't use helm to deploy images so don't really know but it looks like that sets the virtual machine memory limit for php, the container also tries to set it's limit: https://github.com/tobybatch/kimai2/blob/main/assets/startup.sh#L39

Can you try passing the equivalent of -e memory_limit=512

tobybatch commented 1 year ago

How do you pass in other environment settings, like DATABASE_URL? Could it me that as memory_limit mirrors the php setting name it is being masked during tear up?

We could try a dev build that used KIMAI_MEMORY_LIMIT instead of memory_limit

clyso-dr commented 1 year ago

DATABASE_URL is a secret and will be also set as environment variable in the container as everything else. (https://github.com/robjuz/helm-charts/blob/master/charts/kimai2/templates/secret.yaml)

$ env | grep DATA
DATABASE_URL=mysql://USER:PASSWORD@kimai-mariadb/kimai

On the helm chart its always values.yaml -> configmap or secret -> environment variable

tobybatch commented 1 year ago

So your secrets look something like

{{- if not .Values.existingSecret -}}
apiVersion: v1
kind: Secret
metadata:
  name: {{ include "kimai2.name" . }}-secret
  labels:
    {{- include "kimai2.labels" . | nindent 4 }}
type: Opaque
stringData:
  ADMINPASS: {{ .Values.kimaiAdminPassword }}
  APP_SECRET: {{ .Values.kimaiAppSecret }}
  DATABASE_URL: {{ include "kimai2.databaseUrl" . }}
  MAILER_URL: {{ .Values.kimaiMailerUrl }}
  memory_limit: 512
{{- end -}}

Is that right?

clyso-dr commented 1 year ago

No because it must not be secure so it will just set as environment variabel ... but I think I found the problem:

/startup.sh: line 42: /usr/local/etc/php/php.ini: Permission denied

Because the container is running as www-data and the file permission is root:

www-data@kimai-kimai2-5b8b976f-5b7ff:/$ ls -la /usr/local/etc/php/php.ini              
-rw-r--r-- 1 root root 72909 Oct 30 11:40 /usr/local/etc/php/php.ini
clyso-dr commented 1 year ago

Maybe as idea - Example 1 php.ini Environment Variables: https://www.php.net/manual/en/configuration.file.php#example-36

tobybatch commented 1 year ago

Yes, you can totally override the php ini files my mounting your own. In fact that will be the wuickest way to fix this issue. I'm going to need to re-think how the container starts before I can both; override system config files, and run as a delegated user. Ironically this used to the how we advised containers to be set up.

leuchtrakete666 commented 1 year ago

i solved this problem by copying the content of the container php.ini to a php.ini located in my home directory of the docker compose. than i mounted this php.ini to /usr/local/etc/php/php.ini inside the container. i changed the memory limit to 512M.

that do the trick. it works with docker-compose and also on the truenas scale truechart.

tobybatch commented 1 year ago

@leuchtrakete666 Yes, thanks. That would work with any file you wanted to override.

schucly commented 1 year ago

Maybe as idea - Example 1 php.ini Environment Variables: https://www.php.net/manual/en/configuration.file.php#example-36

We changed it to use the above solution. Works good for us. The memory_limit has to be changed to 256M, because the environment variable will be read as is.

stale[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

tobybatch commented 1 year ago

This does work in the current build:

Proof of function

Create a phpinfo.php file

<?php
phpinfo();

Start an image with memory override

  environment:
    - ADMINMAIL=admin@kimai.local
    - memory_limit=512
    ...

Copy in the phpinfo.php file

docker cp phpinfo.php CONTAINERNAME:/opt/kimai/publid/phpinfo.php

Start the container and hit http://localhost:8001/phpinfo.php

Selection_097