Github Container Registry with Docker and .Net Core and Azure DevOps

Today we will learn how to use Github Container Registry and we will create an docker image of a .Net Core application through Azure DevOps and publish the image to Github Container Registry and use it later.

Things required,

  1. Visual Studio 2019
  2. Personal Github Account( I will be using personal github account)
  3. Azure DevOps Account (you can create one it will be free)

Let’s Start,

  1. At first we need to enable the docker or container registry feature. To do that login to your github account, then click on your account and select Feature Preview, then click on enable. Now it will show only disable option.

2. Now go to Visual Studio 2019, Create a New Project of type ASP.Net Core Web App(Model-View-Controller) and click on Next

3. Now provide a Name for your application and click on Next. After clicking Next, please select framework or sdk , I am selecting .Net Core 3.1 as it is having Long Term Support and Select Enable Docker and Keep Docker OS as linux and please click on Create. We are enabling Docker so Visual Studio will add a dockerfile which will contain the steps to create the image, otherwise we have to do it manually.

4. Now our App is ready , we are going to build and run the default one and see if it is working. Let’s Select Docker in the place of IIS Express and build and run the app.

Visual Studio
Browser

So our App is running perfectly.

5. Let’s add our Application Codebase to Source Control, I will be adding it to Github, but if you want to use any other Git base Remote Server(Azure Repos), you can do it. You can do it by running git commands on the solution folder (i.e. git commit, git add ., git commit -m “”, git remote add etc). I am going to use Visual Studio for this one.

6. Now we will create a Pipeline using Azure DevOps. Lets login to Azure DevOps. Go to Pipelines, and click on New Pipeline. We are going to use the classic editor for better understanding. Now Select a Source for me it is Github( to use GitHub you need to provide credentials and token details I have already provided that so I already have the access) and click on continue.

7. Now we need to select a Template, we will going to select Docker Container and please click on Apply.

8. Now we are ready to create our Pipeline Tasks, first we need to provide connection details of our Github Registry. To do that, please generate a Personal Token from Github, first click on Account after that Select Settings and then Select Developer Settings and then Click on Personal Access Token(PATs), we will be using this to access the Github Container Registry. Please do remember to select Read and Write access on Packages for this token.

Then click on Generate Token and copy and keep the token safe with you in a secured place for the time being.

9. Now back to our pipeline, Our pipeline is currently looking like this. Select Build an image task.

Now we will change the Container Registry Type to Container Registry.

Then click on +New to Create a new Docker Registry Service Connection. It will open a new Page to Create a Service Connection. Please provide your github registered email id in Docker ID and recently copied token in the Password and also provide the same email in Email. Provide a unique and understandable name for the connection.

Now please select newly created Service Connection in the Docker Registry Service Connection and provide the correct location for Dockerfile in the Build Image Task and please select or check other options as it is in the below screenshot.

Build an image

Now we need to make similar kind of changes in the Push an Image Task.

Push an image

Now Save the Pipeline, please do not run the pipeline now.

9. We need to modify our Dockerfile as it is in the project level not in the solution level so Build Context will be different and we need to change a few things to make it work. we will change line no 10, 12 and 13. In line no 10, we are removing top level folder as we are already in that folder and we are changing the sequence of line no 12 and 13 as below snippet.

Updated Dockerfile

11. Now run the Pipeline. If everything is okay, it will look like this.

12. Lets jump to Github to see our Images, Login to Github, Go to Your Profile and then Select the Packages Tab. Under Packages Tab , you can find newly build images.

13. Let’s pull this image and deploy in local docker desktop and see how it is working to do that. Lets pull the latest image by running

docker pull ghcr.io/arkapravasinha/mvcapp.githubregistry.demo:latest

14. Let’s run it. by entering below command

docker run --name test -p  8181:80 -d ghcr.io/arkapravasinha/mvcapp.githubregistry.demo:latest

15. Let’s see the result. go to your browser and browse http://localhost:8181/

We have successfully deployed the docker image in local docker container environment.

Please let me know if anyone is having any doubt or query regarding this one.

Coder @ Heart

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store