How to copy a script into a container and run it?

After creating a container I have a list of tasks I would like to run.

Is the recommended way simply to copy the script into the node fs or is there a better/correct way to do it?

Here is my shell version:

sudo cp /var/lib/lxc/$N/rootfs/usr/local/sbin/
sudo lxc-attach -n $N -- /usr/local/sbin/ $I $N

Ansible version:

  - name: Copy node init script
      dest: "/var/lib/lxc/{{ item.hostname }}/rootfs/usr/local/sbin/"
      owner: root
      group: root
      mode: '0700'
    with_items: "{{ nodes }}"
    notify: Execute node init script

  - name: Execute node init script
      name: "{{ item.hostname }}"
      container_command: "/usr/local/sbin/ {{ item.ip }} {{ item.hostname }}"
    with_items: "{{ nodes }}"

LXD supports cloud-init

  cloud-init.vendor-data: |
      - abc
      - xyz
      - [echo, hello world]

if you want to get into Canonicals Application orchestrator / operator tool… you point it at LXD

lxd init <when setting up you must disable IPV6>


sudo snap install juju --classic
juju bootstrap localhost <follow all defaults>

once you have juju installed you can start doing deployments of entire containers and “charms” … and I stick mostly to bash based charms but they have examples of more advanced things in their charmhub community

juju deploy ubuntu < this will deploy a instance of ubuntu to the LXD host configured >

I usually do a watch -c juju status --color to watch the progress of the instantiation

from there if you just had a script you want to copy and run you can juju scp <machine #>