Host your Golang app on Azure App Services using Docker

Host your Golang app on Azure App Services using Docker

Yes! Azure App Service supports Golang, in a way!

This blog is a detailed breakdown of my learning's from Microsoft Reactor's How to use Azure to Kickstart your DevOps Lifecycle I Build, Test and Deploy by Liam Hampton

Prerequisities

  1. Azure subscription (free/student work too)
  2. Azure-cli
  3. Docker

    here are some resources to help you

What are we going to do?

  1. Create a Docker image with the Golang app. (Golang is not supported directly)
  2. Upload it to an Azure Container Registry.
  3. Create Azure App Service.

Setting up code

The project is written in Go, which I have never used before. Liam explained a bit about how it works in the video. I cloned the repo from github.com/liamchampton/devops-series-1

In simple, what happens is that the main.go file brings together different parts of the website from views folder. And the Template that Liam used has some licenses so the footer has to stay, don't remove it when you make changes.

All I wanted is to get this to work, so I only changed a little bit of the main.go file.

DevopsReactorpart1_01_Codechange.png

The main_test.go is a simple test file that verifies if the files mentioned are present on not. To run the test usego test.

go.mod file has all of the dependencies for the project to run.

Run go run main.go to run the application locally. The ip:3000 site doesnt work well for some reason. Go to localhost:3000 to see the amazing site.

Docker File

Let's create a Docker file. Here we are creating someting called a Multi-stage Docker build. But why? you ask..

If we just run the Golang build we get a Docker image of 700+MB so we compile the code and build it. Then we create a new build and copy only the necessary files into the new build.

This way we can get a ~15MB docker image. Crazy isnt it?

Let's break down the Dockerfile, its very simple.

#Gets a Golang image of a specific version which is based on Alpine
#AS build is like an alias, we can use it to say, docker get the image named build that 
#you created before and do this to it
FROM golang:1.17.6-alpine3.15 AS build

#change your current directory to this 
WORKDIR /home

#Copy files from the folder where the docker image is present into the ./ directory of the container
COPY go.mod ./
COPY go.sum ./

#Get the go dependencies 
RUN go mod download

#Create a directory called views
RUN mkdir views

#Copy all the files that end with .go into ./
COPY *.go ./

#Copy everything in views folder into the newly created ./views
COPY views ./views

#This is to build Go and get the build output into a folder called website
RUN GOSS=linux GOARCH=amd64 go build -ldflags "-s -w" -o website

#Grant the website folder Executable permission
RUN chmod +x website

#Get a new Alpine image
FROM alpine:3.15

#Copy only the website and view folders from the other container into this
COPY --from=build home/views /views
COPY --from=build home/website /

#Make the port 3000 open
EXPOSE 3000

#The data in websites folder is what people should see when they access the website
ENTRYPOINT [ "/website" ]

Use docker build --platform linux/amd64 --tag doa-ep1:v1 .

  • --platform -> specifies which platform you want to build, in our case we are going to host it on a Linux Appservice which is amd64 architecture.
  • --tag -> lets you identify the image later, doa-ep1 is the name DevOps on Docker Episode1 and v1 is the version
  • "." -> in the end is to specify, use the Dockerfile in this directory.

DevopsReactorpart1_02_DockerImage.png

We have a Docker image!

Create Azure Container Registry

Run az login to log into your Azure account.

First to create a Azure App service we need a Resource Group.

Resource Group is a logical container to group all of your resources. Just helps you stay organized and identify all the resources being used for a project etc.

Run az group create -l eastus2 -n liam-devops-series to create one.

  • -l -> Location of the resource group, I tried VM's and they are cheap in EastUS so I use it most of the time these days.

  • -n -> Name of the resource group

Nice! Time for an App Service.

Azure Container Registry(ACR) is similar to docker hub or any central repository where you can store all of your docker images and then pull them from it. ACR is a Private repository that we are creating to host out own docker images.

Create a new ACR az acr create --resource-group liam-devops-series -n devopsonazure982 --sku Basic --admin-enabled true

  • --resource-group -> We want this ACR to be under the resource group we created before
  • --n -> Name of the ACR, should be unique.
  • --sku -> Stock Keeping Unit is the storage plan for the Container Registry. Read more Here.
  • --admin-enabled -> Grants you permission to push images, pull them and do other admin stuff with the ACR

Use az acr login -n NameOfYourACR with the name of your ACR. In my case I run az acr login -n devopsonazure982

Push docker image into ACR

Now lets create an image and tag it with our ACR

DevopsReactorpart1_03_taggedImages.png

Next we have to push it into the ACR.

DevopsReactorpart1_04_PushToACR.png

Create an Azure App Service

Azure App Service is a PaaS(Platform as a Service) offering, meaning the hardware, OS, software running on the OS, everything is managed by Microsoft. All you do is give it code and App Service runs/hosts it.

Pick the correct resource group, give the App service a name, and select Docker Container on Linux and F1 Size.

DevopsReactorpart1_05_AppServiceOptions.png

Next change the settings to get the Docker image from

DevopsReactorpart1_06_SelectDockerImage.png

Once the resource is created, check the URL and there we have our site!

DevopsReactorpart1_07_FinalResource.png

DevopsReactorpart1_08_website.png


Thank you for reading, you can find me on Twitter, read my blogs on Hashnode and Medium