This is a basic example that uses the container-collection package ("github.com/inspektor-gadget/inspektor-gadget/pkg/container-collection") to print a message when a container is created or removed.
$ go build .
Start the example in one terminal. It'll print the already existing containers:
$ sudo ./container-collection
Container added: "local-registry" pid 6135
In another terminal, run a container:
$ docker run --name hi -rm ubuntu /bin/bash -c "echo hi && sleep 2"
hi
The previous terminal will contain messages indicating the creation and removal of this container:
$ sudo ./container-collection
Container added: "local-registry" pid 6135
Container added: "hi" pid 130258
Container removed: "hi" pid 130258
The kube-container-collection
example
contains a more complex setup using this package.