Share via


將 Go Web 應用程式部署至 Azure Container Apps

在本快速入門中,您將瞭解如何將容器化的 Go Web 應用程式部署至 Azure Container Apps。

Azure Container Apps 可讓您執行封裝在任何容器中的應用程式程序代碼,而不需要管理複雜的雲端基礎結構或複雜的容器協調器,也不必擔心運行時間或程序設計模型。 Azure Container Apps 的常見用法包括:部署 API 端點、裝載背景處理應用程式、處理事件驅動處理,以及執行微服務。

遵循本教學課程,逐步解說建置 Docker 映像、將該映射部署至 Azure Container Registry,以及將 Go Web 應用程式部署至 Azure Container Apps。

必要條件

  • Azure 訂用帳戶:如果您沒有 Azure 訂用帳戶,請在開始前建立免費帳戶

下載範例應用程式

若要遵循本教學課程,您需要範例應用程式才能容器化。 msdocs-go-webapp-quickstart GitHub 存放庫中會提供範例 Go Web 應用程式。 下載或複製範例應用程式至本機工作站。

git clone https://github.com/Azure-Samples/msdocs-go-webapp-quickstart.git

cd msdocs-go-webapp-quickstart

建立 Azure Container Registry

Azure Container Registry 可讓您建置、儲存及管理容器映射。 您將使用它來儲存 Docker 映像,其中包含先前所述的範例存放庫中提供的範例 Go Web 應用程式。

執行下列命令來建立 Azure Container Registry:

  1. 建立 Azure 資源群組。

    az group create \
        --name <resourceGroupName> \
        --location eastus
    
  2. 建立 Azure Container Registry。

    az acr create \
        --resource-group <resourceGroupName> \
        --name <azureContainerRegistryName> \
        --sku basic \
        --admin-enabled true
    
  3. 登入 Azure 容器實例。

    az acr login --name <azureContainerRegistryName>  
    

將和 <azureContainerRegistryName> 取代<resourceGroupName>為適當的值。 請注意,您的 Azure Container Registry 名稱必須是全域唯一的。

建置和推送 Docker 映射

建立 Azure Container Registry 之後,請建置並推送範例 Go Web 應用程式的 Docker 映像。

執行下列命令建置並將映像推送至登錄:

  1. 取得登入伺服器資訊。

    az acr show \
        --name <azureContainerRegistryName> \
        --resource-group <resourceGroupName> \
        --query loginServer \
        --output tsv  
    
  2. 在本機建置 Docker 映像。

    docker build -t <loginServer>/<imageName>:latest .
    
  3. 將 Docker 映像推送至 Azure Container Registry。

    docker push <loginServer>/<imageName>:latest
    
  4. 確認映像已成功推送至 Azure Container Registry。

    az acr repository list \
        --name <azureContainerRegistryName> \
        --output table
    

將、 imageNameazureContainerRegistryName 取代loginServer為適當的值。 映射名稱是推送至 Azure Container Registry 的 Docker 映射,稍後用來部署至 Azure Container Apps。

既然您已在 Azure Container Registry 中取得映像,即可部署 Azure Container App 及其環境。

建立 Azure Container Apps 環境

Azure Container Apps 沒有容器協調器的複雜性,但仍需要一些方法來建立安全界限,也就是 Azure Container Apps 環境所在的位置。 部署在相同環境中的容器應用程式會共用相同的虛擬網路,並將記錄寫入相同的Log Analytics工作區。 您必須先部署環境才能部署 Azure Container App。

執行下列命令來建立 Azure Container Apps 環境:

  1. 取得 Azure Container Registry 管理員密碼。

    ACR_PASSWORD=$(az acr credential show \
        --name <azureContainerRegistryName> \
        --query 'passwords[0].value' \
        --out tsv)
    
  2. 建立容器應用程式環境。

    az containerapp env create \
        --name <containerAppEnvName> \
        --resource-group <resourceGroupName> \
        --location "East US"
    

部署至 Azure 容器應用程式

此時,您已建立 Azure Container Registry、建置和推送 Docker 映射,並建立 Azure Container Apps 環境。 剩下的就是部署應用程式。

執行下列命令,將 Go Web 應用程式部署至 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'

確認 Web 應用程式 URL

執行下列 Azure CLI 命令,以取得 Web 應用程式輸入的 FQDN (完整功能變數名稱)。

APP_FQDN=$(az containerapp show \
    --name <containerAppName> \
    --resource-group <resourceGroupName> \
    --query properties.configuration.ingress.fqdn \
    --output tsv)

接下來,對 FQDN 執行 curl 命令,並確認輸出會反映網站的 HTML。

curl "https://$APP_FQDN"

清除資源

當您完成範例應用程式時,您可以從 Azure 移除應用程式的所有資源。 這可避免持續收費,並讓您的 Azure 訂用帳戶保持不整整。 拿掉資源群組也會移除資源群組中的所有資源,而且是移除應用程式所有 Azure 資源最快的方式。

az group delete \
    --name <resourceGroupName> \
    --no-wait

下一步