Run Go application in Azure Container Instance
Tags: azure, go, docker
Published: 2020-10-25 16:01:03

---
title: Run Go application in Azure Container Instance
published: true
date: 2020-10-25 16:00:00 UTC
tags: azure,golang,docker
canonical_url: https://developerschallenges.com/2020/10/25/run-go-application-in-azure-container-instance/
---

![Run GO application in Azure Container Instance](https://developerschallenges.com/images/posts/2020-10-25-run-go-application-in-azure-container-instance/hero.png)

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](https://github.com/) - a place to host your code and trigger building and publishing a docker image
- [Docker Hub account](https://hub.docker.com/) - a place to host your docker image
- [Azure account](https://azure.microsoft.com/en-us/free/) - 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](https://github.com/dalibormesaric/go-key-value-api) 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](https://hub.docker.com/repository/create) to host my container image. In my case both repositories have the same name.

![Docker Hub Create Repository](https://developerschallenges.com/images/posts/2020-10-25-run-go-application-in-azure-container-instance/docker-hub-create-repository.png)

## 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](https://hub.docker.com/_/golang/). 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](https://github.com/features/actions). Initially I wanted to use recently announced [GitHub Container Registry](https://github.blog/2020-09-01-introducing-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](https://github.com/marketplace/actions/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](https://developerschallenges.com/images/posts/2020-10-25-run-go-application-in-azure-container-instance/docker-hub-access-token.png)

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

![GitHub Secrets](https://developerschallenges.com/images/posts/2020-10-25-run-go-application-in-azure-container-instance/github-secrets.png)

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](https://github.com/dalibormesaric/go-key-value-api/actions), and my container image [ended up in Docker Hub container registry](https://hub.docker.com/r/dalibormesaric/go-key-value-api) it was time to actually publish my container image to Azure Container Instance. To do this I chose to use [Azure CLI](https://docs.microsoft.com/en-us/cli/azure/install-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
```

Finally to test the newly deployed api I used Visual Studio Code extension called [Rest Client](https://marketplace.visualstudio.com/items?itemName=humao.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](https://developerschallenges.com/images/posts/2020-10-25-run-go-application-in-azure-container-instance/rest-client.gif)

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](https://github.com/NickeManarin/ScreenToGif/)_