How to publish Docker image in GitHub

GitHub actions are really cool, but, with GitHub repositories is better. Pushing a Docker image is very easy to setup. We need only a developer token from the GitHub’s profile and few lines in actions.

Get the GitHub Token

Connected to GitHub? Go to https://github.com/settings/tokens and click on Generate a new token. Select write:packages scope to be able to push the Docker image.

Use GitHub Action

Create a file in the repository .github/workflows/ci.yml and then paste this code in it:

name: publish

on: [push]

jobs:
  publish-docker-image:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2

    - name: Login to GitHub Container Registry
      uses: docker/login-action@v1
      with:
        registry: ghcr.io
        username: ${{ github.actor }}
        password: ${{ secrets.TOKEN }}

    - name: Build the Docker image
      run: |
        docker build . --tag ghcr.io/yourname/package-name:latest
        docker run ghcr.io/yourname/package-name:latest
        docker push ghcr.io/yourname/package-name:latest

When it’s done ✅ 📦 , the package will appear in packages tab:
https://github.com/yourname?tab=packages

To be able to pull the image, docker agent must login in using GitHub Token, just save the token in .bashrc/.zshrc and run this command:

echo $CR_PAT | docker login ghcr.io -u yourname --password-stdin

Then, you can pull the image and run it 🐳

You Might Also Like

Leave a Reply