Skip to main content

Automate with Hooks

Monk is able to run any action in response to an event. This mechanism is good for defining scripted actions that should happen in given circumstances over containers and other objects. It can be used to define custom recovery scenarios and even intricate auto-scaling schemes.

Basic example

This is a very basic example of how a hook can be applied to generate a file inside a container every time it is started:

namespace: foobars

defines: runnable
defines: containers
image: alpine:latest
entrypoint: <- `/bin/sh /root/`
container-started: hello-world

defines: files
path: /root/
container: bar
contents: "while true; do sleep 1; date; done"

defines: actions
code: exec("bar", "/bin/sh", "-c", `echo "Hello World" > /tmp/hello`)

The container itself will just hang out and wait but this is perfect for us since we'll be inspecting its filesystem contents.

Run the template with:

monk load basic.yaml
monk run foobars/foo

Now use the shell command to enter the container:

monk shell foobars/foo

See if /tmp/hello exists:

$ cat /tmp/hello
Hello World

This confirms that the hello-world action was in fact triggered by container-started event. The action ran the exec operator that used shell echo to place a file in the container.

More on hooks

Monk supports the following hooks at the moment:

Applicable inHookDescription
containercontainer-startingFires before the container start.
containercontainer-startedFires after the container start.
containercontainer-stoppingFires before the container stop.
containercontainer-stoppedFires after the container stop.

All hooks are asynchronous at the moment, meaning that the action called by the hook will run in parallel with other processes and won't block the hooked process.

Rate this page