Timebased pihole blocking - with docker

I'm using the docker pihole/pihole image, and I want to setup timebased pihole blocking. Basically I have two shellscripts with something like this:

pihole -b youtube.com fortnite.com
pihole -b -d youtube.com fortnite.com

I want to set up cronjobs for these shellscripts - but I'm not sure how to do it in a docker environment in the container so that it will persist in the future.

You'd have to mount your cron configuration file for your container, so it would survive restarts.

But you should also be aware that DNS and time-based blocking are never going to be an exact match, see e.g. Feature request - Automate changes to client group assignment - #3 by Bucking_Horn.

Another option is to use the host cron job, but use docker exec to execute the command inside the container:

docker exec <Pi-hole_container_name> <command>

Examples:

# In this case, the container name is "Pihole"

docker exec Pihole pihole -b youtube.com fortnite.com

docker exec Pihole pihole -b -d youtube.com fortnite.com

I've added this line to docker-compose:

command: ["bash", "/scripts/init.sh"]

init.sh contains:

#!/bin/bash
crontab /scripts/crontab.txt
service cron start
exec "$@"

It was chatgpt that gave me this suggestion. However, pihole crashes:

...
  [i] Enabling Query Logging
  [i] Testing lighttpd config: Syntax OK
  [i] All config checks passed, cleared for startup ...
  [i] Docker start setup complete
  [i] pihole-FTL (no-daemon) will be started as pihole
s6-rc: info: service _startup successfully started
s6-rc: info: service pihole-FTL: starting
s6-rc: info: service pihole-FTL successfully started
s6-rc: info: service lighttpd: starting
s6-rc: info: service lighttpd successfully started
s6-rc: info: service _postFTL: starting
s6-rc: info: service _postFTL successfully started
  Checking if custom gravity.db is set in /etc/pihole/pihole-FTL.conf
s6-rc: info: service legacy-services: starting
s6-rc: info: service legacy-services successfully started
Stopping cron
s6-rc: info: service legacy-services: stopping
s6-rc: info: service legacy-services successfully stopped
s6-rc: info: service _postFTL: stopping
s6-rc: info: service _postFTL successfully stopped
s6-rc: info: service lighttpd: stopping
Stopping lighttpd
s6-rc: info: service lighttpd successfully stopped
s6-rc: info: service pihole-FTL: stopping
Stopping pihole-FTL
Terminated
s6-rc: info: service pihole-FTL successfully stopped
s6-rc: info: service _startup: stopping
s6-rc: info: service _startup successfully stopped
s6-rc: info: service _uid-gid-changer: stopping
s6-rc: info: service _uid-gid-changer successfully stopped
s6-rc: info: service cron: stopping
Stopping cron
s6-rc: info: service cron successfully stopped
s6-rc: info: service legacy-cont-init: stopping
s6-rc: info: service legacy-cont-init successfully stopped
s6-rc: info: service fix-attrs: stopping
s6-rc: info: service fix-attrs successfully stopped
s6-rc: info: service s6rc-oneshot-runner: stopping
s6-rc: info: service s6rc-oneshot-runner successfully stopped

What could be wrong?

Note: If i manually do 'crontab /scripts/crontab.txt' inside the container, everything is working as expected. However, it would be great if this command could be executed automatically.

(I will use the script to block domains for the whole family, including me. My kids are between 13-17 years old - they know how to bypass pihole, but we have an agreement - and this solution will help. Regarding TTL that is not a big issue for us. blocking does not have to be exact)

I made this change to init.sh, and now it is working:

#!/bin/bash
crontab /scripts/crontab.txt
# service cron start
# exec "$@"