title | description | ms.topic | ms.date | ms.custom |
---|---|---|---|---|
Deploy a Go web app to Azure Container Apps |
In this tutorial, you'll learn how to use Docker, Azure Container Registry, and Azure Container Apps to deploy a Go web app to Azure. |
quickstart |
07/12/2022 |
devx-track-go, devx-track-azurecli |
In this quickstart, you'll learn to deploy a containerized Go web app to Azure Container Apps.
Azure Container Apps lets you execute application code packaged in any container without having to manage complicated cloud infrastructure or complex container orchestrators, and without worrying about the runtime or programming model. Common uses of Azure Container Apps include: Deploying API endpoints, hosting background processing applications, handling event-driven processing, and running microservices.
Follow this tutorial to walk through building a Docker image, deploying that image to Azure Container Registry, and deploying a Go web app to Azure Container Apps.
[!INCLUDE azure-subscription.md]
- Go installed: Version 1.18 or above
- Docker Desktop
To follow this tutorial, you'll need a sample application to containerize. A sample Go web app is provided in the msdocs-go-webapp-quickstart GitHub repository. Download or clone the sample application to your local workstation.
git clone https://github.com/Azure-Samples/msdocs-go-webapp-quickstart.git
cd msdocs-go-webapp-quickstart
Azure Container Registry allows you to build, store, and manage container images. You'll use it to store the Docker image that contains the sample Go web app provided in the sample repository mentioned previously.
Run the following commands to create an Azure Container Registry:
-
Create an Azure resource group.
az group create \ --name <resourceGroupName> \ --location eastus
-
Create an Azure Container Registry.
az acr create \ --resource-group <resourceGroupName> \ --name <azureContainerRegistryName> \ --sku basic \ --admin-enabled true
-
Sign in to the Azure container instance.
az acr login --name <azureContainerRegistryName>
Replace <resourceGroupName>
and <azureContainerRegistryName>
with the appropriate values. Note that your Azure Container Registry name will need to be globally unique.
Once you've created an Azure Container Registry, build and push the Docker image of the sample Go web app.
Run the following commands build and push the image to the registry:
-
Get the sign-in server information.
az acr list --query "[].loginServer"
-
Build the Docker image locally.
docker build -t <loginServer>/<imageName>:latest
-
Push the Docker image to Azure Container Registry.
docker push <loginServer>/<imageName>:latest
-
Verify the image was successfully pushed to Azure Container Registry.
az acr repository list \ --name <azureContainerRegistryName> \ --output table
Replace loginServer
, imageName
, and azureContainerRegistryName
with the appropriate values. The image name is the Docker image that is pushed to Azure Container Registry and later used to deploy to Azure Container Apps.
Now that you've got an image available in Azure Container Registry, you're ready to deploy the Azure Container App and its environment.
Azure Container Apps doesn't have the complexity of a container orchestrator, but it still needs some way to establish secure boundaries, which is where Azure Container Apps environments come in. Container Apps deployed in the same environment share the same virtual network and write logs to the same Log Analytics workspace. Before you can deploy an Azure Container App, you'll need an environment to deploy to.
Run the following commands to create an Azure Container Apps environment:
-
Get the Azure Container Registry admin password.
ACR_PASSWORD=$(az acr credential show \ --name <azureContainerRegistryName> \ --query 'passwords[0].value' \ --out tsv)
-
Create a Container Apps environment.
az containerapp env create \ --name <containerAppEnvName> \ --resource-group <resourceGroupName> \ --location "East US"
At this point, you've created an Azure Container Registry, built, and pushed a Docker image to it, and created an Azure Container Apps environment. All that's left is to deploy the application.
Run the following command to deploy the Go web app to Azure Container Apps:
az containerapp create \
--name <containerAppName> \
--resource-group <resourceGroupName> \
--environment <containerAppEnvName> \
--image "<loginServer>/<imageName>:latest" \
--registry-server "<loginServer>" \
--registry-username "<azureContainerRegistryName>" \
--registry-password "$ACR_PASSWORD" \
--target-port 8080 \
--ingress 'external'
Run the following Azure CLI command to get the FQDN (Fully Qualified Domain Name) of the web application's ingress.
APP_FQDN=$(az containerapp list --query "[].properties.configuration.ingress.fqdn")
Next, run the curl command against the FQDN and confirm output reflects the HTML of the website.
curl "$APP_FQDN"
When you're finished with the sample app, you can remove all of the resources for the app from Azure. This avoids on-going charges and keeps your Azure subscription uncluttered. Removing the resource group also removes all resources in the resource group and is the fastest way to remove all Azure resources for your app.
az group delete \
--name <resourceGroupName> \
--no-wait
[!div class="nextstepaction"] Key Azure Services for Go developers
[!div class="nextstepaction"] Configure Visual Studio Code for Go Development