使用重启策略运行容器化任务Run containerized tasks with restart policies

在 Azure 容器实例中部署容器的简便性和速度,使该服务成了可用于执行一次性任务(例如,在容器实例中生成、测试渲染作业并制作其映像)的引人注目的平台。The ease and speed of deploying containers in Azure Container Instances provides a compelling platform for executing run-once tasks like build, test, and image rendering in a container instance.

使用可配置的重启策略,可将容器指定为在完成其进程后停止。With a configurable restart policy, you can specify that your containers are stopped when their processes have completed. 由于容器实例按秒计费,我们只需针对执行任务的容器在运行时所用的计算资源付费。Because container instances are billed by the second, you're charged only for the compute resources used while the container executing your task is running.

本文演示的示例使用 Azure CLI。The examples presented in this article use the Azure CLI. 必须在本地安装 Azure CLI 2.0.21 或更高版本。You must have Azure CLI version 2.0.21 or greater installed locally.

容器重启策略Container restart policy

在 Azure 容器实例中创建容器组时,可以指定三个重启策略设置中的一个。When you create a container group in Azure Container Instances, you can specify one of three restart policy settings.

重启策略Restart policy 说明Description
Always 始终重启容器组中的容器。Containers in the container group are always restarted. 如果在创建容器时未指定重启策略,则会应用此默认设置。This is the default setting applied when no restart policy is specified at container creation.
Never 永远不重启容器组中的容器。Containers in the container group are never restarted. 容器最多运行一次。The containers run at most once.
OnFailure 仅当容器中执行的进程失败(终止且返回非零退出代码)时,才重启容器组中的容器。Containers in the container group are restarted only when the process executed in the container fails (when it terminates with a nonzero exit code). 容器至少运行一次。The containers are run at least once.

指定重启策略Specify a restart policy

重启策略的指定方式取决于容器实例的创建方式,例如,是使用 Azure CLI、Azure PowerShell cmdlet 还是 Azure 门户。How you specify a restart policy depends on how you create your container instances, such as with the Azure CLI, Azure PowerShell cmdlets, or in the Azure portal. 在 Azure CLI 中,在调用 az container create 时指定 --restart-policy 参数。In the Azure CLI, specify the --restart-policy parameter when you call az container create.

az container create \
    --resource-group myResourceGroup \
    --name mycontainer \
    --image mycontainerimage \
    --restart-policy OnFailure

一直运行到完成的示例Run to completion example

若要查看重启策略的工作方式,请基于 Microsoft aci-wordcount 映像创建一个容器实例,并指定 OnFailure 重启策略。To see the restart policy in action, create a container instance from the Microsoft aci-wordcount image, and specify the OnFailure restart policy. 此示例容器运行一个 Python 脚本,默认情况下,该脚本会分析莎士比亚著作哈姆雷特中的文本,将 10 个最常见的单词写入 STDOUT,然后退出。This example container runs a Python script that, by default, analyzes the text of Shakespeare's Hamlet, writes the 10 most common words to STDOUT, and then exits.

使用以下 az container create 命令运行示例容器:Run the example container with the following az container create command:

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

Azure 容器实例将启动该容器,然后在其应用程序(在本例中为脚本)退出时停止。Azure Container Instances starts the container, and then stops it when its application (or script, in this case) exits. 当 Azure 容器实例停止重启策略为 NeverOnFailure 的某个容器时,该容器的状态将设置为 TerminatedWhen Azure Container Instances stops a container whose restart policy is Never or OnFailure, the container's status is set to Terminated. 可以使用 az container show 命令检查容器的状态:You can check a container's status with the az container show command:

az container show --resource-group myResourceGroup --name mycontainer --query containers[0].instanceView.currentState.state

示例输出:Example output:

"Terminated"

当示例容器的状态显示为 Terminated 后,可以通过查看容器日志来查看该容器的任务输出。Once the example container's status shows Terminated, you can see its task output by viewing the container logs. 运行 az container logs 命令可查看脚本的输出:Run the az container logs command to view the script's output:

az container logs --resource-group myResourceGroup --name mycontainer

输出:Output:

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

此示例显示了由脚本发送到 STDOUT 的输出。This example shows the output that the script sent to STDOUT. 但是,容器化任务可能会将其输出写入到持久性存储供以后检索。Your containerized tasks, however, might instead write their output to persistent storage for later retrieval. 例如,写入到 Azure 文件共享For example, to an Azure file share.

后续步骤Next steps

基于任务的方案(例如,使用多个容器批量处理大型数据集)可以在运行时利用自定义的环境变量命令行Task-based scenarios, such as batch processing a large dataset with several containers, can take advantage of custom environment variables or command lines at runtime.

有关如何保存一直运行到完成的容器的输出,请参阅装载包含 Azure 容器实例的 Azure 文件共享For details on how to persist the output of your containers that run to completion, see Mounting an Azure file share with Azure Container Instances.