How do I pass a cloud-init config when creating an LXD container?

Solution 1:

It appears you need to wait longer. Since you have both package_update and package_upgrade specified as true, you're essentially run apt-get update; apt-get upgrade (which can take more than 5 seconds) before tree can get installed or the runcmd executes.

Your exact commands work for me if I substitute the sleep 5 with

lxc exec x -- bash -c "cloud-init status --wait"
sleep 2  # because of the `package_reboot_if_required: true`