Local DNS from file for docker

I am trying to add local DNS entries on my docker setup. On the local DNS page the following is show:

The order of locally defined DNS records is:

  1. The device's host name and pi.hole
  2. Configured in a config file in /etc/dnsmasq.d/
  3. Read from /etc/hosts
  4. Read from the "Local (custom) DNS" list (stored in /etc/pihole/custom.list)

So I am mounting a file 97-custom-dns.conf to my docker setup. This file is seen inside of the container, but FTL will not run and shows the following error:
FTL failed to start due to bad option at line 1 of /etc/dnsmasq.d/97-custom-dns.conf
What is the correct format for this file?
I am currently using a simple file that matches the custom.list file when you add an entry manually.

192.168.1.10 mypc.home
192.168.1.11 device.home

Here is how pi-hole populated my /etc/hosts file.
In my docker-compose.yaml:

version: "3"

services:
  pihole:
    container_name: pihole
    image: pihole/pihole:latest
    environment:
      - TZ=America/Los_Angeles
      - FTLCONF_LOCAL_IPV4=192.168.1.200
      - FTLCONF_LOCAL_IPV6=0000:0000:0000:0000:0000:ffff:c0a8:01c8
      - TEMPERATUREUNIT=f

    hostname: pihole
    domainname: home.arpa

    volumes:
      - pihole:/etc/pihole
      - dnsmasq:/etc/dnsmasq.d

    networks:
      default:
        ipv4_address: 192.168.1.200

    restart: unless-stopped

volumes:
  pihole: {}
  dnsmasq: {}

networks:
  default:
    external:
      name: my-net
root@pihole:/etc# cat /etc/hosts
127.0.0.1	localhost
::1	localhost ip6-localhost ip6-loopback
fe00::0	ip6-localnet
ff00::0	ip6-mcastprefix
ff02::1	ip6-allnodes
ff02::2	ip6-allrouters
192.168.1.200	pihole.home.arpa pihole

I passed it:

    ip: 192.168.1.200
    hostname: pihole
    domainname: home.arpa

So maybe the format is:
ip domainname hostname

This is a guess on my part.

Why not simply use Local DNS | DNS Records to manage those names via Pi-hole's UI?

I think this is different than the 'Local DNS' option. I tried that format anyway, and FTL still had an error starting up.

That is what I have done so far. I am hoping to add a file with the domains/IPs when starting up the container so I wouldn't have to manually input the records if I get a new device or start with a fresh pi-hole install.

1 Like

I can see how this would be handy to do. Nice thing about docker, is we can test these things out.

I don't understand what 1. is trying to say:

  1. The device's host name and pi.hole

Also, the note:
Only the first record will trigger an address-to-name association.

Does that mean if there is something in /etc/dnsmasq.d/ that it will not try and read /etc/hosts?

Then you are almost there, I think:
Local DNS Records -as managed via Pi-hole's UI- are indeed stored in a file, as mentioned:

You may edit that file and add one IP-address-to-hostname association per line, with at most one IPv4 and one IPv6 appearing for the same hostname, e..g.

192.168.1.10 laptop
fe80::10 laptop

If that would fit your bill, you're done.
And that would come with the additional benefit of having your names accessible via Pi-hole's UI. :wink:

As for configuration files in `/etc/dnsmasq.d/` (click for more)

Handling other files would only be necessary if you'd require more control over hostname records, e.g. defining more than one IPv4 for a name.

A custom dnsmasq configuration will give you the most detailed control, but you'd have to familiarise yourself with dnsmasq's documentation.
host-record would seem the most obvious option to use here, but you'd have to look into others as well.


I'd recommend to try editing /etc/pihole/custom.list first.

On the docker side of things. How are you getting the file in the container? Dockerfile, or docker-compose.yaml?

Pi-hole doesn't populate the hosts file - that's done by the host OS and/or user input.

For simple hostname definitions, there's really no need to look any further:
The easiest way to define local hostnames would be via Pi-hole's UI, which stores its definitions in /etc/pihole/custom.list.
That file could also be used to populate it from some external source, provided the limitations I've mentioned above are met.

For Docker, both that file under /etc/pihole as well as any custom dnsmasq configuration under /etc/dnsmasq.d/ would have to be stored in respective volume mounts, as catered for by our sample configuration.

1 Like

Good catch. Thank you for pointing that out.

I'm going to try and make a docker setup with a custom.list. I like the custom.list idea for the reasons you pointed out. You can quickly see if your file took, and you can make changes with the web gui.

Might be a day or two, have a lot going on today. Thank you for your feed back. :smiley:

I have been trying to mount a file to the /etc/dnsmasq.d/ folder. Other files work ok when mounting. My docker compose is as shown:

    volumes:
      - ./pihole/99-edns.conf:/etc/dnsmasq.d/99-edns.conf
      - ./pihole/04-pihole-static-dhcp.conf:/etc/dnsmasq.d/04-pihole-static-dhcp.conf
      - ./pihole/98-dns-server-addr.conf:/etc/dnsmasq.d/98-dns-server-addr.conf
      #- ./pihole/97-custom-dns.conf:/etc/dnsmasq.d/97-custom-dns.conf

The first 3 files successfully mount and are added into pihole configuration without error.

#99-edns.conf
edns-packet-max=1232
#04-pihole-static-dhcp.conf
dhcp-host=10:DA:43:xx:xx:xx,DDWRT,192.168.1.3,infinite
dhcp-host=B8:27:EB:xx:xx:xx,pi3,192.168.1.5,infinite
...
##98-dns-server-addr.conf
dhcp-option=option:dns-server,192.168.1.7

I am trying to add the 97-custom-dns.conf file and can't quite workout the format. Based on the other files it seems it should maybe have some sort of string=key format, but I don't know what that would be for dns entries.

I was just looking through the dnsmasq.conf example file and I think the format for the local custom DNS should be as shown on line 76-82:

# Add domains which you want to force to an IP address here.
# The example below send any host in double-click.net to a local
# web-server.
#address=/double-click.net/127.0.0.1

# --address (and --server) work with IPv6 addresses too.
#address=/www.thekelleys.org.uk/fe80::20d:60ff:fe36:f83

I'll try this today and report back.

I can confirm that adding a file with DNS entries as shown works!

# Add domains which you want to force to an IP address here.
# The example below send any host in double-click.net to a local
# web-server.
address=/double-click.net/127.0.0.1

# --address (and --server) work with IPv6 addresses too.
address=/www.thekelleys.org.uk/fe80::20d:60ff:fe36:f83

It does not however show those entries in the GUI under '"Local DNS > DNS Records'.

Looks like mounting a custom 'custom.list' does show the records in the Local DNS Records though so this might be the best option! I mounted it with my docker compose:
- ./pihole/custom.list:/etc/pihole/custom.list
and the file looked like:

# custom.list
1.2.3.4 test.local
1.2.3.5 something.com
2.3.4.5 test.org

Thanks for the inspriration and help guys.