I'm using Pi-hole in a docker container with host-mode networking, and I'd like to see my other (mostly non-hostmode-networking) docker container names in the Pi-hole client list.
The client list shows requests from 172.27.0.2, 172.27.0.6, 172.27.0.9, etc. for all the other docker containers, whereas I'd like to see the container names like 'traefik', 'nginx', 'owntracks'.
Except the IP's of the Docker containers are liable to change, new containers might appears, etc, etc.
So managing manually with local DNS quickly becomes unmanageable.
That's exactly what I was thinking about. I was half-expecting such a feature to be directly available in docker.
Anyway, run this periodically and add this to a file in a separate directory you mount into your Pi-hole container. Then specify this directory using hostsdir=... in a config file inside /etc/dnsmasq.d/
New or changed files are read automatically without the need to send a signal to FTL.
Are you able to dig containers by name in your Docker network internally, e.g. what does dig calibre return when run from within your nginx container?
EDIT: If so, taking a look at the server returned by that dig may open an opportunity to have Pi-hole forward DNS requests for 172.16.0.0/12 (or an appropriately smaller range) to that server, provided Pi-hole's container joins the same Docker network.
Yes, thats pretty much what I decided on, but I wasn't sure if I'd have to use the API to bounce the name resolving in the Pi-hole container. Are you sure it reads in any file changes ?
For anyone following along in the future wanting to get this working :
Place the following in pihole/etc-dnsmasq.d/02-grok_docker.conf :
hostsdir=/etc/pihole/localdns
Make the directory pihole/etc-pihole/localdns, ensuring the cron user will be able to write to it (eg. chmod g+w localdns, and chgrp users localdns, or similar)
Stick something like this in /etc/crontab :
# Update our Docker container IPs in the Pi-hole local DNS
0,15,30,45 * * * * user1 /usr/local/bin/update_pihole_localdns_docker
Plop the following into /usr/local/bin/update_pihole_localdns_docker and chmod +x it!
#!/usr/bin/env bash
# This script relies on 'jq', you might need to apt/yum install it!
DNS_DIR="/where/ever/your/docker/is/pihole/etc-pihole/localdns"
DNS_FILE="docker_containers.list"
if [[ -w "$DNS_DIR" ]]; then
docker network ls -f driver=bridge --format '{{.Name}}' | \
xargs docker network inspect | \
jq '.[].Containers|.[]|[.IPv4Address,.Name]|@csv' | \
sed 's#\/16##;s#\"##g;s#\\##g;s#,# #' > "$DNS_DIR"/"$DNS_FILE"
fi
Then restart your Pi-hole container and hopefully it will resolve your docker image names to their IPs too.
Should be, yes. There will be an inotify watcher attached to the file. I say should as I'm not sure if inotify works on files that are docker mounted and actually live on another system. This you will have to find out, I guess. Trigger a manual change and watch /var/log/pihole.log in your Pi-hole