Docker Hooks

Hook Generator

To generator a starter hook:

$ kubes new hook docker
      create  .kubes/config/hooks/docker.rb

Docker Hooks

Hook Description
build When kubes builds Docker image.
push When kubes pushes the Docker image.

Here are some examples of running custom hooks before and after the docker commands.

Build Hooks

.kubes/config/hooks/docker.rb

before("build",
  execute: "echo 'docker build before hook'",
)

after("build",
  execute: "echo 'docker build after hook'",
)

Results in:

$ kubes docker build
Running docker before build hook.
=> echo 'docker build before hook'
docker build before hook
=> docker build -t gcr.io/tung-275700/demo:kubes-2020-10-10T20-06-28-2e80bf4 -f Dockerfile .
Running docker after build hook.
=> echo 'docker build after hook'
docker build after hook
$

Push Hooks

before("push",
  execute: "echo 'docker push before hook'",
)

after("push",
  execute: "echo 'docker push after hook'",
)

Results in:

$ kubes docker push
Running docker before push hook.
=> echo 'docker push before hook'
docker push before hook
=> docker push gcr.io/tung-275700/demo:kubes-2020-10-10T20-06-28-2e80bf4
Running docker after push hook.
=> echo 'docker push after hook'
docker push after hook
$

exit_on_fail option

By default, if the hook commands fail, then kubes will exit with the original hook error code. You can change this behavior with the exit_on_fail option.

before("build"
  execute: "/command/will/fail/but/will/continue",
  exit_on_fail: false,
)

General Form

before(COMMAND_NAME, OPTIONS)

The command name corresponds to the docker commands: apply, delete, etc.

Hook Options

Name Description
label A human-friendly label so you can see what hooks is being run.
execute The script or command to run. IE: path/to/some/script.sh
exit_on_fail Whether or not to continue process if the script returns an failed exit code.

Ruby Hooks

Instead of using a script for the hook execute option, you can also use a Ruby object. This provides some more control over the current process. See: Ruby Hooks