GitHub automatically create + publish Docker workflow guide

If you have a working Dockerfile defined in your GitHub repository means that you can automatically create and publish your Docker image for others to use and view from the repo. At first I used DockerHub from Docker, but do to changes in their policies there are now size restrictions and a limit of only 1 public repository at the free level so GitHub docker registry is better alternative for larger Docker images (in this case I am publishing a 2 GB docker image which is over the limit for DockerHub right now)

Steps

  • First make sure you have a valid Dockerfile in your repository
  • Got to Actions tab and choose create a New workflow
  • Choose the Docker image option, it should autodetect because you already have a valid Dockerfile in your repository

Copy and paste this text into the autonamed file, it should be called docker-image.yml Make sure you search and replace the text your_image_name with what you want to make the package name display as. You can use auto naming from the repo if you used all lowercase characters (I did not) with this variable ${{ github.repository }}

#
name: Publish the image

# This workflow runs when any of the following occur:
# - A push is made to a branch called `main` or `seed`
# - A tag starting with "v" is created
# - A pull request is created or updated
on:
  push:
    branches:
      - main
      - seed
    tags:
      - v*
  pull_request:
  # This creates an environment variable called `IMAGE_NAME ` with the value `ghtoken_product_demo`.
env:
  IMAGE_NAME: your_image_name
#
jobs:
  # This pushes the image to GitHub Packages.
  push:
    runs-on: ubuntu-latest
    permissions:
      packages: write
      contents: read
      #
    steps:
      - uses: actions/checkout@v4

      - name: Build image
        run: docker build . --file Dockerfile --tag $IMAGE_NAME --label "runnumber=${GITHUB_RUN_ID}"

      - name: Log in to registry
        run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u $ --password-stdin
        #
      - name: Push image
        run: |
          IMAGE_ID=ghcr.io/${{ github.repository_owner }}/$IMAGE_NAME

          # This changes all uppercase characters to lowercase.
          IMAGE_ID=$(echo $IMAGE_ID | tr '[A-Z]' '[a-z]')
          # This strips the git ref prefix from the version.
          VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,')
          # This strips the "v" prefix from the tag name.
          [[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//')
          # This uses the Docker `latest` tag convention.
          [ "$VERSION" == "main" ] && VERSION=latest
          echo IMAGE_ID=$IMAGE_ID
          echo VERSION=$VERSION
          docker tag $IMAGE_NAME $IMAGE_ID:$VERSION
          docker push $IMAGE_ID:$VERSION
  • This will make a workflow called Publish the image
  • It will run on commits to main automatically to create a new docker image from your Dockerfile specification and then publish that image to GitHub Container registry

Once it is done you will see under Packages your Docker container image ready for distribution like this