在 Azure 容器实例中装载 emptyDir 卷
了解如何在 Azure 容器实例中装载 emptyDir 卷以在容器组中的容器之间共享数据。 使用 emptyDir 卷作为容器化工作负荷的临时缓存。
备注
当前只有 Linux 容器能装载 emptyDir 卷。
emptyDir 卷提供了容器组中的每个容器都可访问的可写目录。 该组中的容器可以读取和写入此卷中的相同文件,并且可以在每个容器中使用相同或不同路径装载此卷。
一些示例用于 emptyDir 卷:
- 暂存空间
- 长时间运行任务期间的检查点
- 存储由挎斗容器检索的数据以及由应用程序容器提供的数据
emptyDir 卷中的数据将一直保留到容器崩溃。 但是,并不保证重新启动的容器能够持久保留 emptyDir 卷中的数据。 如果停止容器组,则不会持久保留 emptyDir 卷。
Linux emptyDir 卷的最大大小为 50 GB。
若要在容器实例中装载 emptyDir 卷,可以使用 Azure 资源管理器模板、YAML 文件或其他编程方法来部署容器组。
首先,在该文件的容器组 properties
节中填充 volumes
数组。 接下来,针对容器组中希望装载 emptyDir 卷的每个容器,在容器定义的 properties
节中填充 volumeMounts
数组。
例如,以下资源管理器模板创建了一个包含两个容器的容器组,每个容器均装载了 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": {}
}
]
}
}
]
}
若要查看容器组部署的示例,请参阅使用资源管理器模板部署多容器组和使用 YAML 文件部署多容器组。
了解如何在 Azure 容器实例中装载其他卷类型: