Docker setup & cron


I’m running my nextclud instance via docker (compose file adopted from here). There is already a container running /, which starts a busybox with cron, which starts cron.php every 5 minutes.

Now my question is: how to do i add additional tasks? (i.e. ./occ fulltextsearch:index)

I do not want to rebuild the nextcloud image just for adding a cron task. And i do not want to use the host for managing tasks in the container (host systemd oder host cron)

any way to manage this?


What’s your host system?

the host is running ubutu LTS

Maybe you should. They have a Dockerfile for doing this, and with docker-compose, the process is basically automatic.

i just run them as cron jobs on the host. put them in small skript /usr/local/bin/ and put this roots crontab.

any reason for this?

did you try to copy the to your host, add your tasks and run the container like this:

    image: nextcloud:apache
    restart: always
      - nextcloud:/var/www/html
      - /path/to/your/
    entrypoint: /
      - db
      - redis

Hey guys,

as far as i can tell the crontab entry is done here. So as you can see, inside the container the file /var/spool/cron/crontabs/www-data is used and it usually contains one line like */5 * * * * php -f /var/www/html/cron.php for executing the cron.php every 5 minutes. If you want to do any additional cron tasks you could mount a custom crontab file and add any desired lines. A snippet of the corresponding docker-compose file could look like this:

    image: nextcloud:apache
    restart: always
      - nextcloud:/var/www/html
      - ./mycronfile:/var/spool/cron/crontabs/www-data
    entrypoint: /
      - db
      - redis

while the content of mycronfile could be something like

*/5 * * * * php -f /var/www/html/cron.php
0   0 * * 0 php -f /var/www/html/occ fulltextsearch:index

which will additionally index your NC instance once a week on sunday night at 0:00.


could you please show an example on how to add a task to this script?
I could not find any examples on how to use this It seems to be a normal bash script but then how do you add a task e.g. for preview generator (occ preview:pre-generate)? I do not understand the “exec busybox crond” line already in that file, why do you need exec?
Thanks in advance!

use this script as cronjob. do not try to enable cron inside the contaier. inside any container.

so you just add docker exec --user www-data nextcloud php occ preview:pre-generate assuming that your container name is nextcloud and www-data the web server user.

just saying.

this is a way to enable crond inside a container.

exec executes everything you use as the first argument. e.g. a shell skript has no +x bit. exec that-skript is your friend. and busybox crond just starts the cron daemon inside the container.

nevertheless after i wrote the comment above i found this article in german it says: don’t do it with busybox. not a good idea. the authors advice: use the host cron + docker exec

1 Like

Thanks @Reiner_Nippes. I will then use system cron. I use this docker-compose config to run my nextcloud and since there is a docker service for cron I thought I should use that but then that’s not recommended.

I just migrated my nextcloud to a docker setup on a NAS as host system. First I used the second container solution but now switched to your proposal of using a script in the host’s cron service.

Yet I have two questions that maybe you or s.o. else have answers to:

  1. Do these commands
docker exec --user www-data nextcloud php occ files:scan --all
docker exec --user www-data nextcloud php occ files:scan-app-data
docker exec --user www-data nextcloud php occ preview:pre-generate
exit 0

represent all relevant jobs which are usually done by cron.php?

  1. Does this solution have any advantage against for example calling cron.php as a webjob like e.g. (de) proposes?

Oh, and a bonus question if I may:

  1. It is a tiny setup for max 5 users mainly using the cloud during daytime. So I’d like to let my NAS sleep during nighttime. A 24/7 every-15-minutes cronjob would prevent this, right? Is there any risk if I pause the cronjob during night hours ?

yesterday I was fixing my cron job, cos my deployment is running in docker-compose statement, so yes, as you said, I created a cronjob into the host to call a simple:

docker exec -t -u www-data nextcloud-app php -f /var/www/html/cron.php

in fact, from my beginner understanding from all about containers, It should be better run it on this mode. We should leaves each container running its own service. :grinning: