在 Azure 容器執行個體中掛接 emptyDir 磁碟區

了解如何在 Azure 容器執行個體中掛接 emptyDir 磁碟區,以在容器群組中的容器之間共用資料。 使用 emptyDir 磁碟區作為容器化工作負載的暫時性快取。

注意

目前只有 Linux 容器才能掛接 emptyDir 磁碟區。 在我們設法將所有功能導入 Windows 容器的同時,您可以先在概觀中找到目前的平台差異。

emptyDir 磁碟區

emptyDir 磁碟區提供了可供容器群組中每個容器存取的可寫入的目錄。 群組中的容器可以讀取和寫入磁碟區中的相同檔案,且可以在每個容器中使用相同或不同的路徑掛接它。

部分範例會針對 emptyDir 磁碟區使用下列項目:

  • 暫用空間
  • 長時間執行工作期間的檢查點
  • 側車容器所擷取和應用程式容器所提供的儲存資料

emptyDir 磁碟區中的資料會透過容器損毀保存。 不過,重新啟動的容器不保證會保存 emptyDir 磁碟區中的資料。 如果您停止容器群組,emptyDir 磁碟區就不會保存。

Linux emptyDir 磁碟區的大小上限為 50 GB。

掛接 emptyDir 磁碟區

若要在容器執行個體中掛接 emptyDir 磁碟區,您可以使用 Azure Resource Manager 範本YAML 檔案或其他程式設計方法來部署容器群組。

首先,在檔案的容器群組 properties 區段中填入 volumes 陣列。 接下來,針對您想要掛接 emptyDir 磁碟區所在容器群組中的每個容器,填入容器定義之 properties 區段中的 volumeMounts 陣列。

例如,下列 Resource Manager 範本會建立一個由兩個容器 (個別掛接了 emptyDir 磁碟區) 組成的容器群組:

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "variables": {
    "container1name": "aci-tutorial-app",
    "container1image": "mcr.microsoft.com/azuredocs/aci-helloworld:latest",
    "container2name": "aci-tutorial-sidecar",
    "container2image": "mcr.microsoft.com/azuredocs/aci-tutorial-sidecar"
  },
  "resources": [
    {
      "type": "Microsoft.ContainerInstance/containerGroups",
      "apiVersion": "2021-03-01",
      "name": "volume-demo-emptydir",
      "location": "[resourceGroup().location]",
      "properties": {
        "containers": [
          {
            "name": "[variables('container1name')]",
            "properties": {
              "image": "[variables('container1image')]",
              "resources": {
                "requests": {
                  "cpu": 1,
                  "memoryInGb": 1.5
                }
              },
              "ports": [
                {
                  "port": 80
                }
              ],
              "volumeMounts": [
                {
                  "name": "emptydir1",
                  "mountPath": "/mnt/empty"
                }
              ]
            }
          },
          {
            "name": "[variables('container2name')]",
            "properties": {
              "image": "[variables('container2image')]",
              "resources": {
                "requests": {
                  "cpu": 1,
                  "memoryInGb": 1.5
                }
              },
              "volumeMounts": [
                {
                  "name": "emptydir1",
                  "mountPath": "/mnt/empty"
                }
              ]
            }
          }
        ],
        "osType": "Linux",
        "ipAddress": {
          "type": "Public",
          "ports": [
            {
              "protocol": "tcp",
              "port": "80"
            }
          ]
        },
        "volumes": [
          {
            "name": "emptydir1",
            "emptyDir": {}
          }
        ]
      }
    }
  ]
}

若要查看容器群組部署的範例,請參閱使用 Resource Manager 範本部署多容器群組使用 YAML 檔案部署多容器群組

下一步

了解如何在 Azure 容器執行個體中掛接其他類型的磁碟區: