NextCloud Container PHP Memory Issue as Deployed via Kubernetes

Most common example:

Below is a raw text paste of an exercise in resolving the issue of PHP running out of memory – this normally happens with default Docker/Kubernetes containerized deployments

# Entering container bash shell command line interface
admin@controller01:~$ containerName=nextcloud-6cf9c65d85-45kll
admin@controller01:~$ kubectl exec --stdin --tty $containerName -- /bin/bash
Defaulting container name to nextcloud.
Use 'kubectl describe pod/nextcloud-6cf9c65d85-45kll -n default' to see all of the containers in this pod.

# Attempting to run a php command that has not been added to environmental variables
root@nextcloud-6cf9c65d85-45kll:/var/www/html# occ db:add-missing-indices
bash: occ: command not found

# Calling command from its known path
root@nextcloud-6cf9c65d85-45kll:/var/www/html# /var/www/html/occ db:add-missing-indices
Console has to be executed with the user that owns the file config/config.php
Current user id: 0
Owner id of config.php: 33
Try adding 'sudo -u #33' to the beginning of the command (without the single quotes)
If running with 'docker exec' try adding the option '-u 33' to the docker command (without the single quotes)

# Change into user id 33 (www-data)
root@nextcloud-6cf9c65d85-45kll:/var/www/html# chsh -s /bin/bash www-data
root@nextcloud-6cf9c65d85-45kll:/var/www/html# su - www-data

# Retry running the php command to experience the out-of-memory error
www-data@nextcloud-6cf9c65d85-45kll:~$ /var/www/html/occ db:add-missing-indices

Fatal error: Allowed memory size of 2097152 bytes exhausted (tried to allocate 438272 bytes) in /var/www/html/3rdparty/composer/autoload_real.php on line 37

# Workaround the memory error by invoking php with no memory limits
www-data@nextcloud-6cf9c65d85-45kll:~$ php -d memory_limit=-1 -f  /var/www/html/occ db:add-missing-indices
Check indices of the share table.
Check indices of the filecache table.
Adding additional size index to the filecache table, this can take some time...
Filecache table updated successfully.
Check indices of the twofactor_providers table.
Check indices of the login_flow_v2 table.
Check indices of the whats_new table.
Check indices of the cards table.
Check indices of the cards_properties table.
Check indices of the calendarobjects_props table.
Check indices of the schedulingobjects table.
Check indices of the oc_properties table.

Another example in calling cron.php as privileged user:

# Executing command within Docker container
command=php -d memory_limit=-1 -f /var/www/html/cron.php
docker exec nextcloud su - www-data -s /bin/bash -c '$command'

Generalized workarounds:

# Option 1:
docker exec nextcloud su - www-data -s /bin/bash -c 'PHP_MEMORY_LIMIT=-1 php -f /var/www/html/cron.php'

# Option 2:
docker exec nextcloud su --whitelist-environment=PHP_MEMORY_LIMIT - www-data -s /bin/bash -c 'PHP_MEMORY_LIMIT=-1 php -f /var/www/html/cron.php'

# Option 3:
Add PHP_MEMORY_LIMIT=2G to /etc/enviroment
Posted on Categories Linux

Leave a Reply

Your email address will not be published. Required fields are marked *