Adding a cronjob via dockerfile

How exactly do I add a cronjob (to an image)? For the previewgenerator for example (yes, upon rebuild there isn’t previewgenerator installed, but thats something for another topic).

I tried

FROM nextcloud:apache

RUN apt-get update && apt-get install -y \
    supervisor \
  && rm -rf /var/lib/apt/lists/* \
  && mkdir /var/log/supervisord /var/run/supervisord

COPY supervisord.conf /etc/supervisor/supervisord.conf

RUN echo '*/10 * * * * php occ preview:pre-generate' >> /var/spool/cron/crontabs/www-data

COPY my.config.php /var/www/html/config/my.config.php

ENV NEXTCLOUD_UPDATE=1

CMD ["/usr/bin/supervisord"]

as per cron example from the nextcloud docker repository, with the addition of the RUN echo ... line.
But reviewing the logs via
sudo docker logs nextcloud | grep -i cron
i get

[Sat Jul 27 18:52:40.319805 2019] [php7:error] [pid 31] [client 172.18.0.4:56010] PHP Fatal error:  Uncaught Doctrine\\DBAL\\DBALException: Failed to connect to the database: An exception occurred in driver: SQLSTATE[HY000] [2002] Connection refused in /var/www/html/lib/private/DB/Connection.php:64\nStack trace:\n#0 /var/www/html/3rdparty/doctrine/dbal/lib/Doctrine/DBAL/Connection.php(448): OC\\DB\\Connection->connect()\n#1 /var/www/html/3rdparty/doctrine/dbal/lib/Doctrine/DBAL/Connection.php(410): Doctrine\\DBAL\\Connection->getDatabasePlatformVersion()\n#2 /var/www/html/3rdparty/doctrine/dbal/lib/Doctrine/DBAL/Connection.php(354): Doctrine\\DBAL\\Connection->detectDatabasePlatform()\n#3 /var/www/html/3rdparty/doctrine/dbal/lib/Doctrine/DBAL/Connection.php(710): Doctrine\\DBAL\\Connection->getDatabasePlatform()\n#4 /var/www/html/lib/private/DB/Connection.php(151): Doctrine\\DBAL\\Connection->setTransactionIsolation(2)\n#5 /var/www/html/3rdparty/doctrine/dbal/lib/Doctrine/DBAL/DriverManager.php(181): OC\\DB\\Connection->__construct(Array, Object(Doctrine\\DBAL\\Driver\\PDOMySql\\Driver), Object(Doctrine\\DBAL\\Configuration), Object( in /var/www/html/lib/private/DB/Connection.php on line 64
[Sat Jul 27 18:54:57.730960 2019] [authz_core:error] [pid 5283] [client 172.18.0.4:56104] AH01630: client denied by server configuration: /var/www/html/config
[Sat Jul 27 18:55:14.669406 2019] [authz_core:error] [pid 978] [client 172.18.0.4:56180] AH01630: client denied by server configuration: /var/www/html/config
crond: USER www-data pid 5384 cmd php -f /var/www/html/cron.php
crond: USER www-data pid 5386 cmd php occ preview:pre-generate
crond: USER www-data pid 5388 cmd php occ preview:pre-generate
crond: USER www-data pid 5491 cmd php -f /var/www/html/cron.php
crond: USER www-data pid 5667 cmd php occ preview:pre-generate
crond: USER www-data pid 5877 cmd php -f /var/www/html/cron.php
crond: USER www-data pid 5878 cmd php occ preview:pre-generate
crond: USER www-data pid 5993 cmd php occ preview:pre-generate
[Sat Jul 27 19:47:05.818048 2019] [authz_core:error] [pid 5579] [client 172.18.0.4:58130] AH01630: client denied by server configuration: /var/www/html/config
crond: USER www-data pid 6091 cmd php -f /var/www/html/cron.php
crond: USER www-data pid 6151 cmd php occ preview:pre-generate
crond: USER www-data pid 6396 cmd php -f /var/www/html/cron.php
crond: USER www-data pid 6397 cmd php occ preview:pre-generate
crond: USER www-data pid 6576 cmd php occ preview:pre-generate
crond: USER www-data pid 6610 cmd php -f /var/www/html/cron.php

which looks like some auth error
client denied by server configuration: /var/www/html/config

And the php7 error on top i got loads of, which look like some db misconfig or somtething, but I’m probably gonna open another topic for that, as it does not seam related.

Hi 5schmidti,

you can a solution (works for me like a charm) in the admin doc section at the nextcloud doc

For example, i do this …

/etc/systemd/system/nextcloudcron.service

grafik

and /etc/systemd/system/nextcloudcron.timer

grafik

Hope this works …

Regards

I actually figured out, that my

was the solution, I just read the logs wrong.
With sudo docker logs --timestamps nextcloud | grep -i cron you get all the timestamps.

A little late to the party, but extending the nextcloud apache image like this will break some of its features. Typically the REMOTE_ADDR substitution cannot be disabled as the /entrypoint.sh script is not working correctly. The grant form is broken because of this (proxy setup incompatibilities).