How to avoid "Failed to connect to bus: No such file or directory" from timedatectl on new debian container?

When I try to set the timezone of a debian container immediately after launching it, I get the error “Failed to connect to bus: No such file or directory”. If I wait a few seconds after launching, then I don’t get the error. Is there a better workaround other than sleeping a constant amount of time?

To reproduce, run this script on an LXD host:

#!/bin/sh

lxc launch images:debian/11 time1
lxc exec time1 timedatectl set-timezone Europe/Athens

result:

Creating time1
Starting time1                              
Failed to connect to bus: No such file or directory

Workaround:

#!/bin/sh

lxc launch images:debian/11 time2
sleep 5
lxc exec time2 timedatectl set-timezone Europe/Athens
snap list lxd
Name  Version      Rev    Tracking          Publisher   Notes
lxd   5.4-82d05d6  23339  latest/candidate  canonical✓  -

You might find its more reliable to use LXD cloud-init settings to apply this config.

See cloud-init - LXD documentation