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
- Browse to the Azure portal and search for App Services. Click on App Services.
- On the App Services page, Click + Add.
- 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 - 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 - Click the Create button
- When the deployment is complete, click the Go to resource button
- 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.
- 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
- Install the Azure CLI tools on your local machine.
- Install Docker Desktop on your local machine.
- 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 - Clone the sample application repository.
git clone https://github.com/dave-mccollough/sample-go-webapp.git
- Change directories into the folder.
cd sample-go-webapp
- Run the following command to build the application image
docker build --tag sample-go-webapp .
- 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
- 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. - 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. - 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. - 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. - 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 aLogin Succeeded
message. - 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
- 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.
- 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
- 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. - 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. - 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.
- 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!