Run cloud-init locally with LXD

LXD offers a streamlined user experience for using Linux system containers.

Create your configuration

In this example we will create a file called user-data.yaml containing a basic cloud-init configuration:

$ cat >user-data.yaml <<EOF
#cloud-config
password: password
chpasswd:
  expire: False
ssh_pwauth: True
EOF

Initialize a container

With LXD, the following command initializes a container with the user data file we just created:

$ lxc init ubuntu-daily:jammy test-container
$ lxc config set test-container user.user-data - < userdata.yaml
$ lxc start test-container

To avoid the extra commands this can also be done at launch:

$ lxc launch ubuntu-daily:jammy test-container --config=user.user-data="$(cat userdata.yaml)"

Finally, a profile can be set up with the specific data if you need to launch this multiple times:

$ lxc profile create dev-user-data
$ lxc profile set dev-user-data user.user-data - < cloud-init-config.yaml
$ lxc launch ubuntu-daily:jammy test-container -p default -p dev-user-data

LXD configuration types

The above examples all show how to pass user data. To pass other types of configuration data use the configuration options specified below:

Data

Configuration option

user data

cloud-init.user-data

vendor data

cloud-init.vendor-data

network config

cloud-init.network-config

See the LXD Instance Configuration docs for more info about configuration values or the LXD Custom Network Configuration document for more about custom network config and using LXD with cloud-init.