Cron.php fails to complete. (Doctrine\DBAL\Exception: Failed to connect to the database: An exception occurred in the driver: SQLSTATE[HY000] [2002] Operation timed out in /var/www/html/lib/private/DB/Connection.php:233)

After my previous post, I was somehow able to get nextcloud:fpm up and running after almost a month of troubleshooting (yayy!!), but now I am facing a different challenge, now it is with cron.php. I first did the normal method of changing the background job engine from ajax to cron, hoping that it would be that easy; but i couldnt be more wrong. the cron job failed after ten minutes, and I got confused. I followed similar instructions to the official nextcloud:fpm tutorial on github and sure enough there was a separate container just for cron. I used the same exact configuration in my docker-compose.yaml.

This is my current working compose file, It is a little overengineered, but it is more reliable:

services:
  nginx:
    image: nginx:alpine
    container_name: nextcloud-nginx
    restart: unless-stopped
    volumes:
      - /mnt/SysHDD/Docker-Compose/nextcloud/nginx/:/etc/nginx/:rw
      - /home/server-inspiron/.acme.sh/:/mnt/certs/:ro
      - web:/var/www/html:z,ro
    ports:
      - 443:443
      - 80:80
      - 8096:81
      - 8920:82
    networks:
      nextcloud:
        ipv4_address: 172.21.128.4

  mariadb:
    image: mariadb
    container_name: nextcloud-mariadb
    restart: unless-stopped
    command: --transaction-isolation=READ-COMMITTED --binlog-format=ROW --innodb-file-per-table=1 --skip-innodb-read-only-compressed
    volumes:
      - /mnt/SysHDD/Databases/nextcloud-mariadb/:/var/lib/mysql/:rw
    env_file:
      - db.env
    networks:
      nextcloud:
        ipv4_address: 172.21.128.1

  redis:
    image: redis:alpine
    container_name: nextcloud-redis
    restart: always
    volumes:
      - /mnt/SysHDD/Databases/nextcloud-redis/:/etc/redis
    command: redis-server --requirepass password
    networks:
      nextcloud:
        ipv4_address: 172.21.128.2
  app:
    image: nextcloud:fpm-alpine
    restart: unless-stopped
    container_name: nextcloud-web
    user: 33:1001
    links:
      - mariadb
      - redis
    volumes:
      - web:/var/www/html
      - /mnt/SysHDD/:/mnt/SysHDD/:rw
      - /mnt/SSD/:/mnt/SSD/:rw
      - /mnt/HDD0/:/mnt/HDD0/:rw
      - /mnt/HDD1/:/mnt/HDD1/:rw
    environment:
      - MYSQL_HOST=172.21.128.1:3306
      - REDIS_HOST_PASSWORD=password
    env_file:
      - db.env
    depends_on:
      - mariadb
      - redis
    networks:
      nextcloud:
        ipv4_address: 172.21.128.3
  cron:
    image: nextcloud:fpm-alpine
    user: 33:1001
    restart: unless-stopped
    container_name: nextcloud-cron
    volumes:
      - web:/var/www/html:z
      - /mnt/SysHDD/:/mnt/SysHDD/:rw
      - /mnt/SSD/:/mnt/SSD/:rw
      - /mnt/HDD0/:/mnt/HDD0/:rw
      - /mnt/HDD1/:/mnt/HDD1/:rw
    entrypoint: /cron.sh
    environment:
      - MYSQL_HOST=172.21.128.1:3306
    env_file:
      - db.env
    depends_on:
      - mariadb
      - redis
volumes:
    nextcloud-mariadb:
    web:
networks:
  nextcloud:
    driver: bridge
    ipam:
      config:
        - subnet: 172.21.0.0/16
          gateway: 172.21.0.1

after i saw cron failed, I ran docker exec -it -u 0 nextcloud-cron /bin/sh so that I can see what happens when I run cron.php manually.

I use the command php --define apc-enable_cli=1 -f cron.php, and I wait.
normally a successful cron shouldve taken like 3 seconds, but here I was waiting for around ten minutes, after which the following error popped up, along with a stack trace:

Doctrine\DBAL\Exception: Failed to connect to the database: An exception occurred in the driver: SQLSTATE[HY000] [2002] Operation timed out in /var/www/html/lib/private/DB/Connection.php:233

Stack trace:
#0 /var/www/html/3rdparty/doctrine/dbal/src/Connection.php(453): OC\DB\Connection->connect()
#1 /var/www/html/3rdparty/doctrine/dbal/src/Connection.php(411): Doctrine\DBAL\Connection->getDatabasePlatformVersion()
#2 /var/www/html/3rdparty/doctrine/dbal/src/Connection.php(318): Doctrine\DBAL\Connection->detectDatabasePlatform()
#3 /var/www/html/lib/private/DB/Connection.php(899): Doctrine\DBAL\Connection->getDatabasePlatform()
#4 /var/www/html/lib/private/DB/ConnectionAdapter.php(235): OC\DB\Connection->getDatabaseProvider()
#5 /var/www/html/lib/private/DB/QueryBuilder/QueryBuilder.php(96): OC\DB\ConnectionAdapter->getDatabaseProvider()
#6 /var/www/html/lib/private/AppConfig.php(1211): OC\DB\QueryBuilder\QueryBuilder->expr()
#7 /var/www/html/lib/private/AppConfig.php(237): OC\AppConfig->loadConfig(false)
#8 /var/www/html/lib/private/AppConfig.php(1351): OC\AppConfig->searchValues('enabled', false, 2)
#9 /var/www/html/lib/private/App/AppManager.php(126): OC\AppConfig->getValues(false, 'enabled')
#10 /var/www/html/lib/private/App/AppManager.php(147): OC\App\AppManager->getInstalledAppsValues()
#11 /var/www/html/lib/private/legacy/OC_App.php(191): OC\App\AppManager->getInstalledApps()
#12 /var/www/html/lib/private/AppFramework/Bootstrap/Coordinator.php(48): OC_App::getEnabledApps()
#13 /var/www/html/lib/base.php(657): OC\AppFramework\Bootstrap\Coordinator->runInitialRegistration()
#14 /var/www/html/lib/base.php(1132): OC::init()
#15 /var/www/html/cron.php(24): require_once('/var/www/html/l...')
#16 {main}

At first, I thought the container isn’t able to log into the database, so i changed the docker compose file to include the mysql credentials in the cron service as well, and then retried the same command. still the same issue. I am unable to decipher the honestly very cryptic error messages, so I am asking the community again to lend me a hand here. Thank you.

Your cron container does not appear to be associated with the dedicated network your created for nextcloud.

Thank you, I did what you instructed and cron.php worked, but only when I called it manually. After I left the shell environment I checked after around half an hour, but I saw that cron.php wasn’t called.

This is my crontab file, I made sure to edit it using crontab -u www-data -e

*/5 * * * * php --define apc-enable_cli=1 -f /var/www/html/cron.php

What can I do to fix this?

Edit: Found the Cron Logs, seems like it’s some permission issue, I am unable to figure out where it’s coming from, since for testing I gave 777 permissions to the web volume

/var/log # cat cron.log
crond: crond (busybox 1.36.1) started, log level 8
crond: crond (busybox 1.36.1) started, log level 8
crond: USER www-data pid 118 cmd php --define apc-enable_cli
=1 -f /var/www/html/cron.php
{"reqId":"TEzmOsvdMdbXTlJ01l4U","level":3,"time":"2024-11-02
T04:40:00+00:00","remoteAddr":"","user":"--","app":"PHP","me
thod":"","url":"--","message":"fopen(/var/www/html/config/co
nfig.php): Failed to open stream: Permission denied at /var/
www/html/lib/private/Config.php#190","userAgent":"--","versi
on":"","data":{"app":"PHP"}}
crond: USER www-data pid 119 cmd php --define apc-enable_cli
=1 -f /var/www/html/cron.php
{"reqId":"2KlH1ELFCCnctGHFjOzt","level":3,"time":"2024-11-02
T04:40:00+00:00","remoteAddr":"","user":"--","app":"PHP","me
thod":"","url":"--","message":"fopen(/var/www/html/config/co
nfig.php): Failed to open stream: Permission denied at /var/
www/html/lib/private/Config.php#190","userAgent":"--","versi
on":"","data":{"app":"PHP"}}

There is no need to modify the crontab within the container. This is managed by the image.

If you open a shell with the same access into the cron container, can you access /var/www/html/config/config.php?

docker compose exec -u33:1001 cron bash
cat /var/www/html/config/config.php

FYI: You can access the cron logs by just checking the container logs docker compose logs cron.

No, I rechecked the permissions given to the file which somehow changed to rw-r---- even though i gave 777 permissions to the entire folder. I then fixed it by giving 777 permission to the config.php file.

I ran the cron again and kept an eye on the logs, then this error popped up:

crond: USER www-data pid 251 cmd php --define apc-enable_cli=1 -f /var/www/html/cron.php
{"reqId":"XnYsBiZ3yqsWoD6zpVDe","level":3,"time":"2024-11-02T23:35:00+00:00","remoteAddr":"","user":"--","app":"PHP","method":"","url":"--","message":"fopen(/var/www/html/data/data_dir_writability_test_6726b72445a15.tmp): Failed to open stream: Permission denied at /var/www/html/lib/private/legacy/OC_Util.php#516","userAgent":"--","version":"30.0.1.2","data":{"app":"PHP"}}
{"reqId":"XnYsBiZ3yqsWoD6zpVDe","level":3,"time":"2024-11-02T23:35:00+00:00","remoteAddr":"","user":"--","app":"PHP","method":"","url":"--","message":"fopen(/var/www/html/data/nextcloud.log): Failed to open stream: Permission denied at /var/www/html/lib/private/Log/File.php#53","userAgent":"--","version":"30.0.1.2","data":{"app":"PHP"}}
crond: USER www-data pid 252 cmd php --define apc-enable_cli=1 -f /var/www/html/cron.php
{"reqId":"09xstOaGp5QBcuTWDxCK","level":3,"time":"2024-11-02T23:35:00+00:00","remoteAddr":"","user":"--","app":"PHP","method":"","url":"--","message":"fopen(/var/www/html/data/data_dir_writability_test_6726b72484ac6.tmp): Failed to open stream: Permission denied at /var/www/html/lib/private/legacy/OC_Util.php#516","userAgent":"--","version":"30.0.1.2","data":{"app":"PHP"}}
{"reqId":"09xstOaGp5QBcuTWDxCK","level":3,"time":"2024-11-02T23:35:00+00:00","remoteAddr":"","user":"--","app":"PHP","method":"","url":"--","message":"fopen(/var/www/html/data/nextcloud.log): Failed to open stream: Permission denied at /var/www/html/lib/private/Log/File.php#53","userAgent":"--","version":"30.0.1.2","data":{"app":"PHP"}}

I can see this means there are quite a few files which have the wrong permissions. I can set the entire folder to rwxrwxrwx too but i worry that the same issue would arise if nextcloud decides to make new temp files, or new config files when I use it. How do i eliminate this issue from its roots?

also, i am surprised that file permissions matter when using cron and not running the cron.php file manually. how is it different and why did it work when i ran it manually and didnt when i used it with cron?

You appear to be using the Alpine images. Is there a reason you’re doing the following?

    user: 33:1001

I am using the alpine images just to save a bit on disk space.

for the uid:gid config in docker-compose, I am testing out directory permissions for specific users. On my home server, the uid 33 belongs to the user “www-data”, and the gid 1001 belongs to the group “system”, which contains the users “server-inspiron” (the name of my server), “www-data” and “root”.

the container works with the user “www-data”, the cron.php requires “www-data” as executing user, and the group contains the www-data user. the root volume of the nextcloud installation is chown’d to “www-data:system”. since the crontab is set with user “www-data”, it should execute cron.php as the “www-data” user as well.

what else am i missing that its still unable to access the files?

The www-data UID is 82 in Alpine-based containers.

the root volume of the nextcloud installation is chown’d to “www-data:system”. since the crontab is set with user “www-data”, it should execute cron.php as the “www-data” user as well.

www-data on your underlying host is not the same as www-data in the container. The main volume in your Nextcloud installation is web and is Docker managed. Unless you migrated volumes from a non-Alpine image or something, you should not have to modify these permissions.

The motivation of my prior question (“Is there a reason you’re doing the following?”) is that I’m trying to figure out why you’re using user / --user at all.

Is there something you’re trying to accomplish? Are you intentionally trying to use an alternative user and group?

That makes so much sense, I’ll try it out and see if it works

Well, cron didn’t execute when I didnt specify the user, so I was seeing if specifying a user would help. clearly it hasnt.

Well, the user is built-in, but I made a custom group so that I can also follow the principle of least privilege and only give folder access to the applications that use it.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.