在容器執行個體中設定環境變數

在您的容器執行個體中設定環境變數,可讓您提供由容器執行之應用程式或指令碼的動態設定。 這類似於 --env 命令列引數 docker run

若要在容器中設定環境變數,請在建立容器執行個體時加以指定。 本文顯示您在使用 Azure CLIAzure PowerShellAzure 入口網站啟動容器時,設定環境變數的範例。

例如,如果您執行 Microsoft aci-wordcount 容器映像,可以藉由指定下列環境變數來修改其行為:

NumWords:傳送至 STDOUT 的字詞數。

MinLength:列入計算之字詞中的字元數上限。 較高的數目會略過常用字詞,例如 "of" 和 "the"。

如果您需要將祕密當成環境變數來傳遞,Azure 容器執行個體支援適用於 Windows 和 Linux 容器的安全值

注意

建議您使用 Azure Az PowerShell 模組來與 Azure 互動。 請參閱安裝 Azure PowerShell 以開始使用。 若要了解如何移轉至 Az PowerShell 模組,請參閱將 Azure PowerShell 從 AzureRM 移轉至 Az

Azure CLI 的範例

若要查看 aci-wordcount 容器的預設輸出,請先搭配此 az container create 命令來執行 (未指定環境變數):

az container create \
    --resource-group myResourceGroup \
    --name mycontainer1 \
    --image mcr.microsoft.com/azuredocs/aci-wordcount:latest \
    --restart-policy OnFailure

若要修改輸出,請啟動第二個容器,並新增 --environment-variables 引數,然後為 NumWordsMinLength 變數指定值。 (此範例假設您在 Bash 殼層或 Azure Cloud Shell 中執行 CLI。如果您使用 Windows 命令提示字元,請以雙引號指定變數,例如 --environment-variables "NumWords"="5" "MinLength"="8"。)

az container create \
    --resource-group myResourceGroup \
    --name mycontainer2 \
    --image mcr.microsoft.com/azuredocs/aci-wordcount:latest \
    --restart-policy OnFailure \
    --environment-variables 'NumWords'='5' 'MinLength'='8'

一旦這兩個容器的狀態顯示為已終止 (使用 az container show 檢查狀態),即可使用 az container logs 來顯示其記錄,以查看輸出。

az container logs --resource-group myResourceGroup --name mycontainer1
az container logs --resource-group myResourceGroup --name mycontainer2

容器的輸出會顯示您如何藉由設定環境變數,來修改第二個容器的指令碼行為。

mycontainer1

[('the', 990),
 ('and', 702),
 ('of', 628),
 ('to', 610),
 ('I', 544),
 ('you', 495),
 ('a', 453),
 ('my', 441),
 ('in', 399),
 ('HAMLET', 386)]

mycontainer2

[('CLAUDIUS', 120),
 ('POLONIUS', 113),
 ('GERTRUDE', 82),
 ('ROSENCRANTZ', 69),
 ('GUILDENSTERN', 54)]

Azure PowerShell 的範例

在 PowerShell 中設定環境變數類似於 CLI,但是使用 -EnvironmentVariable 命令列引數。

首先,使用此 New-AzContainerGroup 命令,在預設設定中啟動 aci-wordcount 容器:

New-AzContainerGroup `
    -ResourceGroupName myResourceGroup `
    -Name mycontainer1 `
    -Image mcr.microsoft.com/azuredocs/aci-wordcount:latest

現在,執行下列 New-AzContainerGroup 命令。 此命令會在填入陣列變數 envVars 後,指定 NumWords 和 MinLength 環境變數:

$envVars = @(
    New-AzContainerInstanceEnvironmentVariableObject -Name "NumWords" -Value "5"
    New-AzContainerInstanceEnvironmentVariableObject -Name "MinLength" -Value "8"
)

$containerGroup = New-AzContainerGroup -ResourceGroupName "myResourceGroup" `
    -Name "mycontainer2" `
    -Image "mcr.microsoft.com/azuredocs/aci-wordcount:latest" `
    -RestartPolicy "OnFailure" `
    -Container @(
        New-AzContainerGroupContainer -Name "mycontainer2" `
            -EnvironmentVariable $envVars
    )

一旦這兩個容器的狀態為「已終止」(使用 Get-AzContainerInstanceLog 檢查狀態),即可使用 Get-AzContainerInstanceLog 命令提取容器的記錄。

Get-AzContainerInstanceLog -ResourceGroupName myResourceGroup -ContainerGroupName mycontainer1
Get-AzContainerInstanceLog -ResourceGroupName myResourceGroup -ContainerGroupName mycontainer2

每個容器的輸出會顯示您如何藉由設定環境變數,來修改容器執行的指令碼。

PS Azure:\> Get-AzContainerInstanceLog -ResourceGroupName myResourceGroup -ContainerGroupName mycontainer1
[('the', 990),
 ('and', 702),
 ('of', 628),
 ('to', 610),
 ('I', 544),
 ('you', 495),
 ('a', 453),
 ('my', 441),
 ('in', 399),
 ('HAMLET', 386)]

Azure:\
PS Azure:\> Get-AzContainerInstanceLog -ResourceGroupName myResourceGroup -ContainerGroupName mycontainer2
[('CLAUDIUS', 120),
 ('POLONIUS', 113),
 ('GERTRUDE', 82),
 ('ROSENCRANTZ', 69),
 ('GUILDENSTERN', 54)]

Azure:\

Azure 入口網站範例

若要在 Azure入口網站中啟動容器時設定環境變數,請在您建立容器時,於 [進階] 頁面上指定這些變數。

  1. 在 [進階] 頁面上,將 [重新啟動原則] 設定為 [失敗時]
  2. 在 [環境變數] 下,輸入第一個變數值為 5NumWords,然後輸入第二個變數值為 8MinLength
  3. 選取 [檢閱 + 建立],以驗證並部署容器。

Portal page showing environment variable Enable button and text boxes

若要檢視容器的記錄,請在 [設定] 下選取 [容器],然後選取 [記錄]。 類似於前面 CLI 和 PowerShell 區段中所示的輸出,您可以看到環境變數如何修改指令碼行為。 僅顯示五個字組,每個字組都至少有八個字元長。

Portal showing container log output

安全值 \(部分機器翻譯\)

具有安全值的物件是用來保存應用程式的敏感性資訊,例如密碼或是金鑰。 相較於納入容器映像,針對環境變數使用安全值更具有安全性及彈性。 另一個選項是使用祕密磁碟區,如在 Azure 容器執行個體中掛接祕密磁碟區中所述。

在容器的屬性中看不到具有安全值的環境變數 - 只能從容器內存取其值。 例如,在 Azure 入口網站或 Azure CLI 中檢視的容器屬性只會顯示安全變數的名稱,而不是其值。

藉由針對變數的類型指定 secureValue 屬性而不是一般的 value 來設定安全的環境變數。 下列 YAML 中定義的兩個變數會示範這兩個變數類型。

YAML 部署

使用下列程式碼片段來建立 secure-env.yaml 檔案。

apiVersion: 2019-12-01
location: eastus
name: securetest
properties:
  containers:
  - name: mycontainer
    properties:
      environmentVariables:
        - name: 'NOTSECRET'
          value: 'my-exposed-value'
        - name: 'SECRET'
          secureValue: 'my-secret-value'
      image: mcr.microsoft.com/oss/nginx/nginx:1.15.5-alpine
      ports: []
      resources:
        requests:
          cpu: 1.0
          memoryInGB: 1.5
  osType: Linux
  restartPolicy: Always
tags: null
type: Microsoft.ContainerInstance/containerGroups

執行下列命令,使用 YAML 來部署容器群組 (視需要調整資源群組名稱):

az container create --resource-group myResourceGroup --file secure-env.yaml

確認環境變數

執行 az container show 命令來查詢容器的環境變數:

az container show --resource-group myResourceGroup --name securetest --query 'containers[].environmentVariables'

JSON 回應會顯示非安全環境變數的索引鍵與值,但只會顯示安全環境變數的名稱:

[
  [
    {
      "name": "NOTSECRET",
      "secureValue": null,
      "value": "my-exposed-value"
    },
    {
      "name": "SECRET",
      "secureValue": null,
      "value": null
    }
  ]
]

使用 az container exec 命令 (能夠在執行中的容器中執行命令),您可以驗證是否已設定安全的環境變數。 執行下列命令,以在容器中啟動互動式 Bash 工作階段:

az container exec --resource-group myResourceGroup --name securetest --exec-command "/bin/sh"

一旦在容器內開啟互動式殼層,您就可以存取SECRET 變數的值:

root@caas-ef3ee231482549629ac8a40c0d3807fd-3881559887-5374l:/# echo $SECRET
my-secret-value

下一步

針對工作型案例,例如使用數個容器的大型資料集批次處理,可受益於執行階段上的自訂環境變數。 如需有關執行工作型容器的詳細資訊,請參閱使用重新啟動原則執行容器化工作