Cron job on docker (macos host)

I have my nextcloud server (28.0.4) installed as a docker container on a mac mini (14.4.1). Everytime I run the cron command manually in the terminal it works with no issues.

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

However, when I append it to sudo crontab -e to create an automatic 5 min cron job, as:

*/5 * * * * docker exec -t -u www-data nc php -f /var/www/html/cron.php --define apc.enable_cli=1

Nothing happens!

Any suggetions would be very appreciated, thanks.

I think you better ask Apple Support why this doesn’t work and how to troubleshoot Mac cron jobs.

Native Docker way would be to install side-car container running cron jobs:

  cron:
    image: nextcloud:apache
    restart: always
    volumes:
      - nextcloud:/var/www/html:z
    entrypoint: /cron.sh
    depends_on:
      - db
      - redis

see Docker examples

You might be interested on Nextcloud docker-compose setup with notify_push (2024) showing almost complete setup.

Thanks for your time to answer my issue. I did use the proposed solution adding cron side-car in the nextcloud stack but it is not working for me and I couldn’t tell what is the issue. Here is my docker compose yml

version: '3'

services:
  nc:
    image: nextcloud
    container_name: nc
    restart: unless-stopped
    networks: 
      - tunnel
    depends_on:
      - ncdb
      - redis
    ports:
      - 8080:80
    volumes:
      - /Users/docker/nc/app:/var/www/html
      - /Users/docker/nc/custom_apps:/var/www/html/custom_apps
      - /Users/docker/nc/config:/var/www/html/config
      - /Users/docker/nc/data:/var/www/html/data
    environment:
      - MYSQL_DATABASE=nextcloud
      - MYSQL_USER=nextcloud
      - MYSQL_PASSWORD=
      - MYSQL_HOST=ncdb
      - REDIS_HOST=redis
      - REDIS_HOST_PASSWORD=

  ncdb:
    image: mariadb
    container_name: ncdb
    restart: unless-stopped
    command: --transaction-isolation=READ-COMMITTED --binlog-format=ROW
    networks: 
      - tunnel
    volumes:
      - /Users/docker/nc/nextclouddb:/var/lib/mysql
    environment:
      - TZ=Europe/Paris
      - MYSQL_RANDOM_ROOT_PASSWORD=true
      - MYSQL_PASSWORD=
      - MYSQL_DATABASE=nextcloud
      - MYSQL_USER=nextcloud
      
  redis:
    image: redis:alpine
    container_name: redis
    restart: unless-stopped
    volumes:
      - /Users/docker/nc/redis:/data  
    networks: 
      - tunnel
    command: redis-server --requirepass

  cron:
    image: nextcloud
    restart: always
    volumes:
      - /Users/docker/nc/:/var/www/html:z
    entrypoint: /cron.sh
    depends_on:
     - ncdb
     - redis
      
networks:
  tunnel: #this is the name of the network that we created in the Cloudflared Container
    external: true

you provide different volume mounts… likely this is problem

you should use exactly same mounts for you app and cron containers.

1 Like

You are right I fixed the mounts and now it works. Thanks!

1 Like