Skip to content

Example: Build Docker container image

Blackmoon edited this page May 17, 2026 · 3 revisions

This GitHub Action example workflow automates the creation of a GitHub Actions Runner on Hetzner Cloud, builds a Docker image using that runner, and then automatically deletes the runner to avoid unnecessary costs. It utilizes the Cyclenerd/hcloud-github-runner GitHub Action for managing the runner lifecycle and leverages the docker buildx command for efficient image building and pushing to the GitHub Container Registry.

Image used: docker-ce Hetzner Cloud Docker CE App (based on Ubuntu 24.04)

name: "Docker"


on:
  workflow_dispatch:


jobs:
  create-runner:
    name: Create Runner
    runs-on: ubuntu-latest

    outputs:
      label: ${{ steps.create-runner.outputs.label }}
      server_id: ${{ steps.create-runner.outputs.server_id }}

    steps:
      - name: Create runner
        id: create-runner
        uses: wydler/hcloud-self-hosted-github-runner@c2a407295a3f3ccf0ca7473d0489849a8fd71354  # 1.0.0
        with:
          mode: create
          github_token: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
          hcloud_token: ${{ secrets.HCLOUD_TOKEN }}
          server_type: cx23
          # Use Hetzner Cloud Docker CE App (based on Ubuntu 24.04)
          # https://docs.hetzner.com/cloud/apps/list/docker-ce
          # https://github.com/hetznercloud/apps/tree/main/apps/hetzner/docker-ce
          image: docker-ce
          # Add SSH key to disable root password generation and email notifications
          # ssh_key: 123


  build:
    name: Build Image
    needs:
      - create-runner # required to get output from the create-runner job
    runs-on: ${{ needs.create-runner.outputs.label }}

    steps:
      - name: Checkout 🛎️
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2

      - name: Login to GitHub container registry 🏭
        # https://docs.docker.com/reference/cli/docker/login/
        run: docker login ghcr.io -u "${{ github.repository_owner }}" -p "${{ secrets.github_token }}"

      - name: Build Container image 🧪
        # https://docs.docker.com/reference/cli/docker/buildx/build/
        # Replace "ghcr.io/cyclenerd/hcloud-github-runner:example"
        run: |
          docker buildx build . \
            --file "Dockerfile" \
            --tag "ghcr.io/cyclenerd/hcloud-github-runner:example" \
            --push


  delete-runner:
    name: Delete Runner
    needs:
      - create-runner # required to get output from the create-runner job
      - build # required to wait when the main job is done
    runs-on: ubuntu-latest
    if: ${{ always() }} # required to stop the runner even if the error happened in the previous jobs

    steps:
      - name: Delete runner
        uses: wydler/hcloud-self-hosted-github-runner@c2a407295a3f3ccf0ca7473d0489849a8fd71354  # 1.0.0
        with:
          mode: delete
          github_token: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
          hcloud_token: ${{ secrets.HCLOUD_TOKEN }}
          name: ${{ needs.create-runner.outputs.label }}
          server_id: ${{ needs.create-runner.outputs.server_id }}

Clone this wiki locally