在 Azure 容器实例中装载 emptyDir 卷Mount an emptyDir volume in Azure Container Instances

了解如何在 Azure 容器实例中装载 emptyDir 卷以在容器组中的容器之间共享数据。Learn how to mount an emptyDir volume to share data between the containers in a container group in Azure Container Instances. 使用 emptyDir 卷作为容器化工作负荷的临时缓存。Use emptyDir volumes as ephemeral caches for your containerized workloads.

备注

当前只有 Linux 容器能装载 emptyDir 卷。Mounting an emptyDir volume is currently restricted to Linux containers.

emptyDir 卷emptyDir volume

emptyDir 卷提供了容器组中的每个容器都可访问的可写目录。The emptyDir volume provides a writable directory accessible to each container in a container group. 该组中的容器可以读取和写入此卷中的相同文件,并且可以在每个容器中使用相同或不同路径装载此卷。Containers in the group can read and write the same files in the volume, and it can be mounted using the same or different paths in each container.

一些示例用于 emptyDir 卷:Some example uses for an emptyDir volume:

  • 暂存空间Scratch space
  • 长时间运行任务期间的检查点Checkpointing during long-running tasks
  • 存储由挎斗容器检索的数据以及由应用程序容器提供的数据Store data retrieved by a sidecar container and served by an application container

emptyDir 卷中的数据将一直保留到容器崩溃。Data in an emptyDir volume is persisted through container crashes. 但是,并不保证重新启动的容器能够持久保留 emptyDir 卷中的数据。Containers that are restarted, however, are not guaranteed to persist the data in an emptyDir volume. 如果停止容器组,则不会持久保留 emptyDir 卷。If you stop a container group, the emptyDir volume is not persisted.

装载 emptyDir 卷Mount an emptyDir volume

若要将 emptyDir 卷装载到容器实例中,必须使用 Azure 资源管理器模板进行部署。To mount an emptyDir volume in a container instance, you must deploy using an Azure Resource Manager template.

首先,在模板的容器组 properties 节中填充 volumes 数组。First, populate the volumes array in the container group properties section of the template. 接下来,针对容器组中希望装载 emptyDir 卷的每个容器,在容器定义的 properties 节中填充 volumeMounts 数组。Next, for each container in the container group in which you'd like to mount the emptyDir volume, populate the volumeMounts array in the properties section of the container definition.

例如,以下资源管理器模板创建了一个包含两个容器的容器组,每个容器均装载了 emptyDir 卷:For example, the following Resource Manager template creates a container group consisting of two containers, each of which mounts the emptyDir volume:

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "variables": {
    "container1name": "aci-tutorial-app",
    "container1image": "microsoft/aci-helloworld:latest",
    "container2name": "aci-tutorial-sidecar",
    "container2image": "microsoft/aci-tutorial-sidecar"
  },
  "resources": [
    {
      "name": "volume-demo-emptydir",
      "type": "Microsoft.ContainerInstance/containerGroups",
      "apiVersion": "2018-02-01-preview",
      "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": {}
          }
        ]
      }
    }
  ]
}

若要查看使用 Azure 资源管理器模板进行的容器实例部署示例,请参阅在 Azure 容器实例中部署多容器组To see an example of container instance deployment with an Azure Resource Manager template, see Deploy multi-container groups in Azure Container Instances.

后续步骤Next steps

了解如何在 Azure 容器实例中装载其他卷类型:Learn how to mount other volume types in Azure Container Instances: