Deploy your first Go Application to Azure App Service

In my last blog post I demonstrated how you can deploy a Python Flask application from GitHub to Azure App Service, but what if your application isn’t written in a language or framework with first class support in Azure App Service? In this post I’ll demonstrate how you can deploy a simple Go application from either Docker Hub or Azure Container Registry to Azure App Service.

Let’s get started with deploying from Docker Hub!

Prerequisites

  • Azure Subscription

Deploy from Docker Hub

  1. Browse to the Azure portal and search for App Services. Click on App Services.


  2. On the App Services page, Click + Add.


  3. Fill out the following fields on the Create Web App blade:
    Project Details
    Create a new resource group
    Instance Details
    Enter the name of your application
    The publish radio button should be set to Docker Container
    The Operating System radio button should be set to Linux
    Select a Region
    Click the Docker button


  4. On the Docker tab, you will configure App Services to pull the Go sample application container image from my Docker Hub account (if you’re interested in setting up your own Docker Hub account, you can find the instructions here).

    The Options dropdown should be set to Single Container
    The Image Source dropdown should be set to Docker Hub
    Docker hub options
    Access Type should be set to Public
    Image and tag should be: davemccollough/sample-go-webapp:latest
    Click the Review + create button

  5. Click the Create button


  6. When the deployment is complete, click the Go to resource button


  7. You will be redirected to the overview page for your web application. Here you can access all the resources relating to your web application. Click the URL link to launch the Go application we just deployed.



  8. It’s a good idea to clean up any resources related to your app service to avoid any unnecessary charges. You can do this by deleting the resource group you created for this application.

Deploy from Azure Container Registry

Configure your local environment

  1. Install the Azure CLI tools on your local machine.
  2. Install Docker Desktop on your local machine.
  3. After installing Docker, open a terminal window and verify Docker is installed by running the following command
    docker --version
    If Docker was successfully installed, you should receive similar output

  4. Clone the sample application repository.
    git clone https://github.com/dave-mccollough/sample-go-webapp.git
  5. Change directories into the folder.
    cd sample-go-webapp
  6. Run the following command to build the application image
    docker build --tag sample-go-webapp .
  7. You can test that the application runs locally by running the following command.
    docker run -p 80:80 sample-go-webapp
    Open a browser and browse to localhost:80 to verify the app is running.

Configure Azure Container Registry

  1. Open a terminal window and login to Azure using the az login command.
    az login
    If the Azure CLI tools are not installed, refer to step 1 of the previous section
    A browser window will open prompting you to login to your Azure account


    When you’ve successfully logged in, you will receive a JSON response in your terminal window.

  2. Create the resource group using the az group create command.
    az group create --name go-webapp-rg --location eastus

    This command will return a JSON response confirming your resource group was created.

  3. Create the Azure Container Registry by running the az acr create command.
    az acr create --name <your-acr-name> --resource-group go-webapp-rg --sku Basic --admin-enabled true

    You should replace <your-acr-name> with a name for your Azure Container Registry. The name should contain only letters and numbers and be unique across Azure.

    This command will return a JSON response confirming your container registry was created.

  4. You will need to retrieve the credentials for the container registry you created in step 3 by running the following command.
    az acr credential show --resource-group go-webapp-rg --name <your-acr-name>

    This command will return a JSON response with your container registry username and password.

  5. Using the values returned in the JSON response in step 4, login to your container registry using the docker login command.

    docker login <your-acr-name>.azurecr.io --username <registry-username>

    You will be prompted for a password. Enter the password value from step 4.
    If your login was successful, you will receive a Login Succeeded message.

  6. Before pushing your docker image from your local machine to the Azure Container Registry, you will want to tag your image. Run the following command to tag your image.

    docker tag sample-go-webapp <your-acr-name>.azurecr.io/sample-go-webapp:latest

  7. After tagging your docker image, let’s push the image to your Azure Container Registry by running the following command.

    docker push <your-acr-name>.azurecr.io/sample-go-webapp:latest

    It may take a few minutes for the image to upload.

  8. To confirm your image has uploaded, you can run the az acr repository list command.”
    az acr repository list -n <your-acr-name>

    This command will return a JSON response listing your container images.

Deploy to Azure App Services

  1. Create the Azure App Service plan using the az appservice plan create command.

    az appservice plan create --name appsvcplan --resource-group go-webapp-rg --is-linux

    This command will return a JSON response confirming your App Service plan was created.

  2. Create web application using the az web create command.

    az webapp create --resource-group go-webapp-rg --plan appsvcplan --name <your-app-name> --deployment-container-image-name <your-acr-name>.azurecr.io/sample-go-webapp:latest

    You should replace <your-app-name> with a name for your web application. The name should be unique across Azure.

  3. After the web application has been deployed, you can browse to the App Service in Azure. Click the URL link to launch the containerized Go application you just deployed.




  4. It’s a good idea to clean up any resources related to your app service to avoid any unnecessary charges. You can do this by deleting the resource group you created for this application.

Summary

In this blog post we deployed a sample Go application to Azure App service from both Docker Hub and Azure Container Services. Thanks to Michael Levan for getting me started with Go. If you’re interested, check out his YouTube channel for some great Go content!

Thank you for reading!


Posted

in

by

Tags:

%d bloggers like this: