DEV Community

Dalibor Mesarić
Dalibor Mesarić

Posted on • Originally published at developerschallenges.com on

Run Go application in Azure Container Instance

Run GO application in Azure Container Instance

I haven’t had many opportunities to use containers in my day-to-day work, so I wanted to finally try out this workflow. I also wrote a web api application in Go, so why not use it for this exercise.


Getting started

There are some prerequisites to achieve this. You will need:

  • GitHub account - a place to host your code and trigger building and publishing a docker image
  • Docker Hub account - a place to host your docker image
  • Azure account - a place to run your application inside an Azure Container Instance

I’m primarily a .NET developer, but I had a chance to try Go for a bit. Here is the source code for my simple Key-Value API application.

Apart from having a GitHub repository to host my code, I had to create a repository in Docker Hub to host my container image. In my case both repositories have the same name.

Docker Hub Create Repository

Modifying the solution

There were two things I had to add to my solution in order to get the container hosting workflow to work. Firstly, I added Dockerfile to my project by following instruction from Golang’s official Docker Hub page. This was pretty straightforward and I was able to run my application inside a docker container locally in no time.

The second thing I had to do was to create a docker image and push it to a container registry. For this I chose to use GitHub Actions. Initially I wanted to use recently announced GitHub Container Registry, but as it turns out, its public images are not really public. Azure Container Instance has issues accessing those, and until that is resolved, I will be using Docker Hub. I followed instructions from Docker’s own GitHub Action on how to build and push docker images to Docker Hub.

As a part of this process I had to create a new access token in Docker Hub, and add it to my GitHub repository as a secret, together with my Docker Hub username. This way GitHub Action is able to access information that is not meant to be public.

When creating a new access token make sure you give it a good descriptive name, so you know which repository or application it belongs to.

Docker Hub Access Token

In GitHub I gave the secrets the same name as I used in the GitHub Action yaml.

GitHub Secrets

There is also a possibility to link your GitHub account to docker hub instead of creating an access token, but I chose not to do that.

Hosting and testing the application

Once my GitHub Action successfully ran, and my container image ended up in Docker Hub container registry it was time to actually publish my container image to Azure Container Instance. To do this I chose to use Azure CLI, but you can do it in Azure Portal as well.

These are the two commands I used to create a new Resource Group and a new Azure Container Instance. Notice the --image dalibormesaric/go-key-value-api part which points to my Docker Hub repository.

az group create --location westeurope --name rg-go-key-value-api
az container create --resource-group rg-go-key-value-api --name aci-go-key-value-api --image dalibormesaric/go-key-value-api --cpu 1 --memory 1 --dns-name-label go-key-value-api --ports 9000
Enter fullscreen mode Exit fullscreen mode

Finally to test the newly deployed api I used Visual Studio Code extension called Rest Client. I just replaced localhost with the dns name I used to create the Azure Container Instance in test.http, and it worked!

Rest Client

As a final note, make sure to stop or delete your unused Azure resources to avoid unwanted costs.

Conclusion

Overall, the experience is pretty smooth, except for the unfortunate setback in trying to use GitHub Container Registry. Through Azure CLI I’m able to bring up instances of my applications on-demand and extend my workflows by taking advantage of cloud compute.


To record gifs I use ScreenToGif

Top comments (0)