DEV Community

Deselikem for GitHub

Posted on • Updated on

How to Publish a Docker Image to GitHub's Container Registry

During my summer internship, I learned about publishing a docker image to GitHub's Container Registry using GitHub Actions. I decided it would be a nice thing to share so i made a blog post .

Step 1: Create a new repository

For this tutorial, I'll be naming my repository “publish-to-gcr”. After we're done naming our repository, we're gonna want to make sure that our repository is public.

Image description

Step 2: Create a file

You can create any program with files of your choice, but for my example, I'll create an app.js file. Inside of the app.js file, I'll console.log the words, "Hello, world!"

Image description

Step 3: Create a Docker file

At the root of the project, I created a Docker file with the following contents.

FROM node:alpine
COPY . /app
WORKDIR /app
CMD node app.js
Enter fullscreen mode Exit fullscreen mode

Image description

Step 4: Create an action

At the root of my project, I created a .github/workflows folder. Inside of it, I created a file called publish.yml. Inside of my publish.yml, I wrote the following code (please note that you will need to replace some values such as the user name:

name: publish
on: [push]
jobs:
publish-hello-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.GITHUB_TOKEN }}
- name: Build the hello-docker Docker image
run: |
       docker build . --tag ghcr.io/deselikem/hello-docker-gcr-demo:latest
       docker run ghcr.io/deselikem/hello-docker-gcr-demo:latest
       docker push ghcr.io/deselikem/hello-docker-gcr-demo:latest
Enter fullscreen mode Exit fullscreen mode

Step 5: Push and commit your changes to trigger the action

Head over to the Actions icon in your repository. You should see the action that we created using the text editor. If everything was done correctly, the actions should have been run and we can go check to see all the steps that the Action took to make sure that it's properly published.

Image description

Step 6: Check out your package that's been published on GitHub's container registry!

In your repo, if you scroll down, you will see a section highlighting the packages associated with your repository. It should the one you just made!

Image description

Image description

If you prefer video tutorials over blog tutorials, I created a video tutorial for you to follow. Check it out!

Top comments (3)

Collapse
 
blackgirlbytes profile image
Rizèl Scarlett

niiice!

Collapse
 
egnuez profile image
Emiliano Nuñez

Good job! Why "docker run ghcr.io/deselikem/hello-docker-gcr-demo:latest " line?

Collapse
 
benitoite profile image
Benitoite

That's really optional but some packages may be integrated with an app test suite that can provide even more value to this workflow beyond mere deployment.