An Enthusiastic Programmer

Docker Hub Webhooks

|

In the preceding articles that we have talked about Docker Hub Automated Build and Docker Hub Automated Tests. In this episode that we continue to talk about the Docker Hub Webhook. Docker Hub allows your webhooks to be triggered when new images were build up. This feature is for continuous deployment(CD). You should set up an open automated deployment web API, which will be used as Docker Hub Webhook.

Create An Deployment API

This API depends on your deployment workflow, such as whether if you are using k8s or running a single docker image. Whatever types of API you are going to build, you should expose that API to the Internet. So the DockerHub can access your API.

I found some useful tools that can help developers deploy automatic deployment process API.

  1. webhook.site: On their website, you can get a link. And this website will list out all of the payloads of your link. So it can help you analyze your payload.
  2. Docker Webhook: This is an open-source application fork from Praisebetoscience/docker hub-webhook that is a simple server-side python script in a Flask framework.

I built an automated deployment process using Docker Webhook and my docker hub cd script docker-hub-cd-script.sh.

#! /bin/sh
script_name=docker-hub-cd-script.sh

for pid in $(pidof -x $script_name); do
    if [ $pid != $$ ]; then
        kill -9 $pid
    fi 
done

docker container stop docker-hub-automated-build-example

docker container rm docker-hub-automated-build-example

docker image rm -f davidwxg/docker-hub-automated-build-example:latest

docker run -p 8000:8000 -d --name docker-hub-automated-build-example davidwxg/docker-hub-automated-build-example:latest

This shell script will kill other instances of this script. Stop and remove the running container docker-hub-automated-build-example, Clean local images davidwxg/docker-hub-automated-build-example:latest, and running up a container with the latest remote image.

Then configure this script in config.py in Docker Webhook application

HOOKS = {
         'repo1': {'latest':'docker-hub-cd-script.sh'}
        }

You can get more information on Open source Docker Webhook and docker-hub-CI-CD-explanation.

Set up Webhooks

Open your docker hub dashboard, navigate to webhooks.

Altbuilding logs

This article discussed the Docker Hub webhook and according to the previous two articles Docker Hub Automated Builds and Docker Hub Automated Tests. We built up a comprehensive, automated deployment system. Hope you can enjoy this, if you have any questions or advisements, please be free to comments at DISQUS in the below.

Comments