My incentive is to start and stop a program – which normally runs in the foreground – when a container is started / stopped. Ideally that program can be passed some arguments, e.g. as parameters or env-vars, like the container’s name or whole config.
My first idea was to (ab)use the lxc.hook.pre-start-var and define a script. However that way it appears very hard to manage and keep track of the application. Making sure it’s running, it keeps running and it getting gracefully stopped during container shutdown.
My second idea was to invoke such a program the same way e.g. forkproxy is being started and stopped during container runs - as incus is already taking care of it getting started, running and stopped. However I couldn’t find any info on how versatile that is and if I can just hook up other programs other than forkproxy.
My third idea was to set up systemd service files during creation/launching of containers. One for each. Is there a way / parameter to run commands on the host during container-creation/-launch?
Not really. We actually make it somewhat purposefully hard to do that as we’ve found that hooks like those in LXC were a big source of hard to debug issues when they were in the blocking startup/shutdown path.
Also as Incus can run clustered, having hooks on the filesystem of some servers but not others could quickly make things messy.
Our go-to answer for this has typically been to use our API instead.
We have /1.0/events which will send you a bunch of different events. In this case, you’d be looking for instance-started, instance-stopped, instance-restarted and instance-shutdown.
These days you have two main ways to get those events. The first would be to attach to the websocket API (/1.0/events), potentially filter for lifecycle events and projects (/1.0/events?project=foo&type=lifecycle) and then react to those.
But another, potentially simpler approach now is to use our ability to send log events over an HTTP(S) webhook. You can configure Incus with something like:
This will then get called every time an instance-XYZ event is emitted within the project foo and will notify that endpoint consistently across the cluster too.