Thursday, February 21, 2019

A22-Use Azure DevOps to build a docker image and push to private repository

In this post, I want to explain the steps I took to create a docker image and push to a private docker hub repository using Azure DevOps. In the previous post, we looked at adding docker support to an existing ASP.NET Core SPA application that uses Angular 7.

It took me 17th attempts to get the build to work. In hindsight, it is always easy to say, that I could have read the logs or documentation but by just following the docs or the web interface of Azure DevOps, it isn’t obvious for a new user to figure this out. You can argue that Azure DevOps is great, or this and that, but it has its quirks and issues. I have been using it since it was called TFS 2010, so when I said a new user, I meant a new user from the standpoint of creating and publishing docker images.

Initially, I started with just two tasks in the build pipeline, Build an image and Push an image. It didn’t work because I didn’t built and push using dotnet build and dotnet publish tasks. After adding those two tasks, it still didn’t work because the files weren’t available inside docker container. To fix that, I had to make sure that dotnet publish was using the –output src as an argument. It is because in the docker file for the project, I am switching the working directory to /src. You can take a look at the docker file here.

After doing that it still didn’t work and because I had to make sure that the build context was point to the folder that had dockerfile. Who came up with the name build context? Seriously. They could have named it “folder containing dockerfile”? The tool tips are the worst in Azure DevOps Builds Tasks. When you are stuck in a situation none of these tooltips make any sense. For instance, I am trying to figure out what does build context means. Is it a variable? Some build folder? And the tooltip says, “Path to build context”. But what does context mean? Crazy.

Alright, on to the next hurdle I overcame. Access denied. This one I tried multiple times. First of all, I added Docker Login and it didn’t work. Then I add a separate step to add tag. It still didn’t work. Because the tag you are supposed to provide has to match your docker hub repository name. Even after adding the same tag that docker is expecting it didn’t work. The reason was that Azure DevOps uses $(Build.BuildNumber) or something as image name and that same image name is being used when it tries to push to docker repository. As the last step that made it work was adding image name in the build tasks to be the same as docker is expecting. <yourid>/<repositoryname>:tagname.

I ensured that all the docker build tasks are using the same name <yourid>/<repositoryname>:tagname. Finally, I was able to push a docker image to docker hub. What a relief!

1 comment: