Docker Crontab - Time based DNS Filter

I was planning on using crontab to setup a time based DNS Filter, but when I try to edit crontab, I get the following error:

crontab -e
update-alternatives: no alternatives for editor

Is contab supported inside of docker?

You are missing (as stated) a text editor...

Install one of those and you'll be able to edit the crontab file.

I think nano will work good for you ...

Keep in mind that when and if the docker image is updated, your editor will be gone ...

If the image gets updated, will that remove the crontab edits that I input?

The best way to add a crontab is to create a file with whatever rules you want outside the container. Then volume mount that file into the container's /etc/cron.d/ folder:

docker run -v "$(pwd)/my-cron:/etc/cron.d/my-cron" ... the rest ... pihole/pihole

This style doesn't require an editor inside the container and will survive docker rm pihole ; docker run <new pihole> which is required as part of the updating procedure.

1 Like

I have followed your instructions, but can't seem to get it working...I am using docker compose and I added the following line to the pihole section and and re-ran docker-compose up which recreated the image.

volumes:
  - ${USERDIR}/docker/pihole/crontab/blockschedule:/etc/cron.d/blockschedule

I created a file named blockschedule:

#Block at 3:30 PM Monday To Thursday
30 15   * * 1-4 root    PATH="$PATH:/usr/local/bin/" pihole -b googlevideo.com m.youtube.com www.youtube.com ytimg.com ytimg.l.google.com
30 15   * * 1-4 root    PATH="$PATH:/usr/local/bin/" pihole --wild googlevideo.com ytimg.com

#Block at 8 PM Sunday
00 20   * * 0   root    PATH="$PATH:/usr/local/bin/" pihole -b googlevideo.com m.youtube.com www.youtube.com ytimg.com ytimg.l.google.com
00 20   * * 0   root    PATH="$PATH:/usr/local/bin/" pihole --wild googlevideo.com ytimg.com

#Unblock at 9 PM Sunday To Thursday
0 21    * * 0-4 root    PATH="$PATH:/usr/local/bin/" pihole -b -d googlevideo.com m.youtube.com www.youtube.com ytimg.com ytimg.l.google.com
0 21    * * 0-4 root    PATH="$PATH:/usr/local/bin/" pihole --wild -d googlevideo.com ytimg.com

Here is the directory of /etc/cron.d inside the pi-hole docker console

root@pihole:/etc/cron.d# ls -la
total 28
drwxr-xr-x  1 root root 4096 Feb  7 15:29 .
drwxrwxr-x  1 root root 4096 Feb  7 16:23 ..
-rw-r--r--  1 root root  102 Oct  7  2017 .placeholder
-rw-rw-r--+ 1 root root  767 Feb  7 16:22 blockschedule
-rw-r--r--  1 root root  712 Jan  1  2017 php
-rw-r--r--  1 root root 1704 Feb  3 18:05 pihole

I verified that I am able to see /etc/cron.d/blockschedule inside the pi-hole docker console. But when I go the the blacklist section in the PI-hole gui, none of the entries are showing up. What could I be missing?