Jay's blog

Waiting for DNS without Networkd

I'm running The Lounge as a combination IRC bouncer and client on my home server. I'm very happy with it. However, I was having one little hiccup. If I rebooted the server, The Lounge would not automatically reconnect to the IRC server on its own. It's supposed to do that.

After consulting with the folks in #thelounge on Libera.chat and looking at the logs within the client itself, I found this:

01:17:37 PM Network created, connecting to irc.libera.chat:6697...
01:17:42 PM *** Connection closed unexpectedly: Error: getaddrinfo EAI_AGAIN irc.libera.chat
01:17:42 PM Disconnected from the network, and will not reconnect. Use /connect to reconnect again.

My server connects to the internet via wifi, so it takes a little longer to get network access than if it was connected via ethernet. Sure enough, The Lounge was trying to start before I had a network connection.

Usually this would just be a matter of adding a dependency on network-online.target to The Lounge's quadlet. But I'm running Debian 12, which means that won't work. Debian is still using ifupdown for networking on headless servers. I had to make a custom service instead that checks for DNS resolution.

# /home/server-user/.config/systemd/user/dns-ready.service
[Unit]
Description=Wait for DNS Resolution
After=network.target

[Service]
Type=oneshot
ExecStart=/bin/sh -c 'until getent hosts libera.chat >/dev/null 2>&1; do echo "Waiting for DNS..."; sleep 2; done'
TimeoutStartSec=600
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

Then I needed to update the quadlet to depend on the new service.

# /home/server-user/.config/containers/systemd/thelounge.container
[Unit]
Description=TheLounge
After=network.target dns-ready.service
Requires=dns-ready.service

[Container]
ContainerName=thelounge
Image=ghcr.io/thelounge/thelounge:latest
AutoUpdate=registry
UserNS=keep-id

Network=proxy

Volume=/home/server-user/thelounge:/var/opt/thelounge:Z

[Service]
Restart=always

[Install]
WantedBy=default.target

Now The Lounge properly reconnects after a power cycle of my server.

#home server #podman #systemd