更新 Azure 容器实例中的容器Update containers in Azure Container Instances

在正常操作容器实例期间,你可能会发现有必要更新容器组中正在运行的容器。During normal operation of your container instances, you may find it necessary to update the running containers in a container group. 例如,你可能想要更新映像版本、更改 DNS 名称、更新环境变量,或刷新其应用程序已崩溃的容器的状态。For example, you might wish to update the image version, change a DNS name, update environment variables, or refresh the state of a container whose application has crashed.

通过使用至少一个已修改的属性重新部署现有组,来更新正在运行的容器组中的容器。Update the containers in a running container group by redeploying an existing group with at least one modified property. 更新某个容器组时,该组中所有正在运行的容器会就地重启(通常在同一基础容器主机上进行)。When you update a container group, all running containers in the group are restarted in-place, usually on the same underlying container host.

备注

已终止或已删除的容器组无法更新。Terminated or deleted container groups can't be updated. 容器组终止(处于“成功”或“失败”状态)或被删除后,必须将该组作为新组进行部署。Once a container group has terminated (is in either a Succeeded or Failed state) or has been deleted, the group must be deployed as new. 请参阅其他限制See other limitations.

更新容器组Update a container group

若要更新现有容器组,请执行以下操作:To update an existing container group:

  • 发出 create 命令(或使用 Azure 门户),指定现有组的名称Issue the create command (or use the Azure portal) and specify the name of an existing group
  • 修改或添加至少一个支持在重新部署时进行更新的组属性。Modify or add at least one property of the group that supports update when you redeploy. 某些属性不支持更新Certain properties don't support updates.
  • 使用先前提供的值设置其他属性。Set other properties with the values you provided previously. 如果未为某个属性设置值,则该属性将恢复为其默认值。If you don't set a value for a property, it reverts to its default value.

提示

YAML 文件有助于维护容器组的部署配置,你可以从其着手来部署已更新的组。A YAML file helps maintain a container group's deployment configuration, and provides a starting point to deploy an updated group. 如果使用了其他方法来创建组,则可使用 az container export 将配置导出到 YAML。If you used a different method to create the group, you can export the configuration to YAML by using az container export,

示例Example

以下 Azure CLI 示例更新具有新 DNS 名称标签的容器组。The following Azure CLI example updates a container group with a new DNS name label. 由于该组的 DNS 名称标签属性是可以更新的属性,因此会重新部署容器组,并重启其容器。Because the DNS name label property of the group is one that can be updated, the container group is redeployed, and its containers restarted.

具有 DNS 名称标签 myapplication-staging 的初始部署:Initial deployment with DNS name label myapplication-staging:

# Create container group
az container create --resource-group myResourceGroup --name mycontainer \
    --image nginx:alpine --dns-name-label myapplication-staging

使用新的 DNS 名称标签“myapplication”更新容器组,并使用先前使用的值设置其余属性:Update the container group with a new DNS name label, myapplication, and set the remaining properties with the values used previously:

# Update DNS name label (restarts container), leave other properties unchanged
az container create --resource-group myResourceGroup --name mycontainer \
    --image nginx:alpine --dns-name-label myapplication

更新的好处Update benefits

更新现有容器组的主要好处是加快部署速度。The primary benefit of updating an existing container group is faster deployment. 重新部署现有容器组时,会从前一部署缓存的配置中提取该组的容器映像层。When you redeploy an existing container group, its container image layers are pulled from those cached by the previous deployment. 此过程不会像新部署中一样从注册表中提取所有全新的映像层,而只会提取修改的层(如果有)。Instead of pulling all image layers fresh from the registry as is done with new deployments, only modified layers (if any) are pulled.

如果更新容器组而不是删除再部署新的容器组,则基于大型容器映像的应用程序(例如 Windows Server Core)的部署速度会有明显的改善。Applications based on larger container images like Windows Server Core can see significant improvement in deployment speed when you update instead of delete and deploy new.

限制Limitations

  • 并非容器组的所有属性都支持更新。Not all properties of a container group support updates. 若要更改某个容器组的某些属性,必须先删除,再重新部署该组。To change some properties of a container group, you must first delete, then redeploy the group. 请参阅需要容器删除操作的属性See Properties that require container delete.
  • 更新某个容器组时,该容器组中的所有容器会重启。All containers in a container group are restarted when you update the container group. 无法对多容器组中的特定容器执行更新或就地重启。You can't perform an update or in-place restart of a specific container in a multi-container group.
  • 容器组的 IP 地址通常会在更新之后保留,但不能保证保持不变。The IP address of a container group is typically retained between updates, but isn't guaranteed to remain the same. 只要将容器组部署到相同的基础主机,容器组就会保留其 IP 地址。As long as the container group is deployed to the same underlying host, the container group retains its IP address. 有些 Azure 内部事件可能会导致容器组重新部署到其他主机,尽管这很少见。Although rare, there are some Azure-internal events that can cause redeployment to a different host. 为了避免此问题,建议对容器实例使用 DNS 名称标签。To mitigate this issue, we recommend using a DNS name label for your container instances.
  • 已终止或已删除的容器组无法更新。Terminated or deleted container groups can't be updated. 容器组停止(处于“已终止”状态)或被删除后,该组将作为新组进行部署。Once a container group is stopped (is in the Terminated state) or deleted, the group is deployed as new.

需要容器删除操作的属性Properties that require container delete

并非所有容器组属性都可以更新。Not all container group properties can be updated. 例如,若要更改容器的重启策略,必须先删除容器组,然后再重新创建它。For example, to change the restart policy of a container, you must first delete the container group, then create it again.

更改以下属性需要在重新部署容器组前将容器组删除:Changes to these properties require container group deletion prior to redeployment:

  • OS 类型OS type
  • CPU、内存或 GPU 资源CPU, memory, or GPU resources
  • 重启策略Restart policy
  • 网络配置文件Network profile

删除再重新创建某个容器组时,不是“重新部署”该组,而是新建一个容器组。When you delete a container group and recreate it, it's not "redeployed," but created new. 将从注册表中全新提取所有映像层,而不是从前一个部署缓存的配置中提取。All image layers are pulled fresh from the registry, not from those cached by a previous deployment. 由于部署到不同的基础主机,容器的 IP 地址也可能会更改。The IP address of the container might also change due to being deployed to a different underlying host.

后续步骤Next steps

本文中多次提到了容器组Mentioned several times in this article is the container group. Azure 容器实例中的每个容器部署在容器组中,容器组可以包含多个容器。Every container in Azure Container Instances is deployed in a container group, and container groups can contain more than one container.

Azure 容器实例中的容器组Container groups in Azure Container Instances

部署多容器组Deploy a multi-container group

手动停止或启动 Azure 容器实例中的容器Manually stop or start containers in Azure Container Instances