How to run commands on Linux Container (LXD) instance at provision launch time

To run commands on an LXD container instance at provision launch time, you can use cloud-init. Cloud-init is a tool that is commonly used to customize Linux instances when they launch in a cloud environment. It can be used to run commands, install packages, write files, and perform other tasks.

Here is an example of how to use cloud-init to run commands on an LXD container at launch time:

  1. Create a cloud-init file with the desired configuration, such as:
#cloud-config
package_update: true
packages:
- nano
runcmd:
- echo "Hello World!" > /root/hello.txt
  1. Launch an LXD container using the cloud-init file:
lxc launch images:ubuntu/bionic mycontainer -c user.user-data="$(cat cloud-init.yaml)"
  1. Verify that the commands in the cloud-init file were run:
lxc exec mycontainer -- cat /root/hello.txt

Note: The above example uses the Ubuntu bionic image, and installs the nano editor. The runcmd section runs the command echo "Hello World!" > /root/hello.txt.

You can use cloud-init with any Linux distribution that is supported by LXD, and you can customize it to run any commands you like.

Leave a Comment