Skip to the content.

Lab: Create a Custom Docker GitHub Action

Objective:

In this lab, you will create a custom GitHub Action using Docker. The action will use a Docker container to execute a simple task, such as printing a message.


Prerequisites:


Step 1: Set Up Your Docker GitHub Action Directory

  1. Navigate to your existing GitHub repository where you want to create the custom Docker action.
  2. Create the following directory structure:

    mkdir -p .github/actions/hello-docker
    cd .github/actions/hello-docker
    
  3. Inside the .github/actions/hello-docker directory, create the following files:
    • Dockerfile
    • entrypoint.sh
    • action.yml

Step 2: Create the Dockerfile

The Dockerfile will define the Docker image for your custom action.

Create the file .github/actions/hello-docker/Dockerfile with the following content:

# Use an official Node.js runtime as a parent image
FROM node:16

# Set the working directory in the container
WORKDIR /usr/src/app

# Copy the entrypoint script to the container
COPY entrypoint.sh /usr/src/app/entrypoint.sh

# Make the entrypoint script executable
RUN chmod +x entrypoint.sh

# Set the default command to run the entrypoint script
ENTRYPOINT ["./entrypoint.sh"]

This Dockerfile:


Step 3: Create the Entry Point Script (entrypoint.sh)

The entrypoint.sh script will be executed when the Docker container runs. This script can do anything you like. For simplicity, it will just print a message.

Create the file .github/actions/hello-docker/entrypoint.sh with the following content:

#!/bin/bash

# Print a message passed via the action input or a default message
echo "Hello from the Docker Action! Here is your input: $1"

This script:


Step 4: Create the Action Metadata (action.yml)

The action.yml file defines the metadata for the GitHub Action, including its inputs, description, and how it runs.

Create the file .github/actions/hello-docker/action.yml with the following content:

name: 'Hello Docker Action'
description: 'A simple Docker action that prints a message.'

inputs:
  message:
    description: 'The message to print.'
    required: true
    default: 'Hello, World!'

runs:
  using: 'docker'
  image: 'Dockerfile'

This action.yml:


Step 5: Build and Test the Action Locally (Optional)

Before pushing the action to GitHub, you can build and test it locally using Docker (optional step).

  1. Build the Docker image:

    docker build -t hello-docker-action .
    
  2. Run the Docker container, passing in an input value:

    docker run hello-docker-action "This is a custom message!"
    

You should see the output:

Hello from the Docker Action! Here is your input: This is a custom message!

Step 6: Commit the Action Files

Once you’ve created the Docker action, commit the files to your repository:

git add .github/actions/hello-docker/*
git commit -m "Create Hello Docker action"
git push origin main

Step 7: Create a Workflow to Use the Action

Now that you have created the custom Docker action, you need to create a workflow that will use this action.

  1. In the root of your repository, create the .github/workflows directory if it doesn’t already exist.
  2. Create a new file called docker-action.yml in .github/workflows with the following content:
name: Custom Action - Docker - Print Message

on:
  push:
    paths:
      - '.github/actions/print-message-container-action/**'
      - '.github/workflows/custom-action-docker-print-message.yml'
  workflow_dispatch:

jobs:
  hello-docker:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout Repository
        uses: actions/checkout@v4

      - name: Run Hello Docker Action
        uses: ./.github/actions/print-message-container-action
        with:
          message: '**** GitHub Actions is Awesome! ****'

This workflow:


Step 8: Run the Workflow

  1. Push the workflow to your repository:

    git add .github/workflows/docker-action.yml
    git commit -m "Add workflow to run Hello Docker action"
    git push origin main
    
  2. Go to the Actions tab in your GitHub repository to see the workflow run.

  3. Check the output of the “Run Hello Docker Action” step. You should see the custom message printed in the logs.


Conclusion:

In this lab, you have:

You can now expand on this basic example to build more complex Docker-based GitHub Actions. For example, you could create an action that runs tests inside a Docker container or deploys an app using Docker.