• Tue Mar 18 2025
  • 2 minutes

Minimal Automatic Deployment Using DockerHub Webhook

  • devops
  • docker
  • cicd

Automating deployment is crucial for streamlining the development workflow. Instead of manually deploying code every time a change is pushed to the repository, an automated deployment system ensures a seamless and efficient process.

However, not all teams have access to advanced infrastructure like Kubernetes, CI/CD platforms, or expensive cloud services. Many developers rely on free repository hosting (such as GitHub and Docker Hub) and old-school VPS servers with minimal cost and resources. In such cases, a lightweight, webhook-based deployment approach provides a simple and effective solution.

This tutorial covers setting up an automated deployment pipeline using Docker Hub webhooks and a webhook server, making it ideal for low-cost, minimal-resource environments.

Image cicd-workflow

PS: Building a Docker image is a separate process that can be done on your local machine or through an external CI/CD system. Once the image is built and pushed to Docker Hub, the next step is to automate its deployment. This is where webhooks come into play.

Create Deployment Script

The first step is to create a deployment script. This script will handle removing the existing container, pulling the latest image, and running the updated container.

#!/bin/bash

docker stop <container_name> || true
docker rm <container_name> || true
docker pull <image_tag>
docker run -d --name <container_name> <image_tag>

Save the script in a suitable location on your server, and don’t forget to make it executable

chmod +x deploy.sh

Install & Configure Webhook

The next step is to install and configure the webhook. I am using the webhook package from https://github.com/adnanh/webhook. First, install it using the following command:

sudo apt update && sudo apt install -y webhook

Next, we need to create a configuration file for the webhook. This configuration defines a series of actions triggered when the webhook is called. You can also send payloads, perform validation, and more.

[
  {
    "id": "deploy",
    "execute-command": "/home/admin/scripts/deploy.sh",
    "command-working-directory": "/home/admin/scripts"
  }
]

Save the configuration file on your server, then start the webhook server.

webhook -verbose -hotreload -hooks=/home/admin/webhook/hooks.json

It will start up on default port 9000 and will provide you with one HTTP endpoint

http://yourserver:9000/hooks/{hook_id}

By performing HTTP GET or POST request to that endpoint, your specified script would be executed.

You can change the ip, port, protocol etc. Documentation is your friend 😉.

Setting In Docker Hub

Next, we need to add a webhook to Docker Hub by following these steps:

  1. Login into your docker hub account
  2. Go to your repository
  3. Go to Webhooks tabs
  4. Insert webhook name and URL and hit (+) icon

Voilà! From now on, whenever a new image is pushed, Docker Hub will send a webhook to your server, triggering the deployment script automatically. 🚀