How to fix "unable to bind to port 53" error

If you get the following error messages when trying to start a pihole container on docker. This is how to fix it.

Problem

When trying to run pihole in a container on docker, you get:

Error starting userland proxy: listen tcp4 0.0.0.0:53: bind: address already in use

or

Error response from daemon: driver failed programming external connectivity on endpoint pihole failed to bind port 0.0.0.0:53/tcp: Error starting userland proxy: listen tcp4 0.0.0.0:53: bind: address already in use

Cause

Another service is using port 53 already.

On Ubuntu this is systemd-resolved.

Fix

  1. Edit /etc/systemd/resolved.conf
  2. Uncomment the line with DNSStubListener and change yes to no. Save. Exit
  3. sudo systemctl restart systemd-resolved.service
  4. sudo rm /etc/resolv.conf
  5. sudo ln -s /run/systemd/resolve/resolv.conf /etc/resolv.conf

Steps 1-3 stops systemd-resolved from binding to port 53. This prevents the system from doing DNS lookups. Steps 4-5 fixes that.