在容器实例中设置命令行来替代默认的命令行操作Set the command line in a container instance to override the default command line operation

创建容器实例时,可以指定一个命令来替代已植入到容器映像中的默认命令行指令。When you create a container instance, optionally specify a command to override the default command line instruction baked into the container image. 此行为类似于在 --entrypoint 命令行中指定参数 docker runThis behavior is similar to the --entrypoint command-line argument to docker run.

与为容器实例设置环境变量一样,对于需要使用特定于任务的配置动态准备每个容器的批处理作业,指定启动命令行非常有用。Like setting environment variables for container instances, specifying a starting command line is useful for batch jobs where you need to prepare each container dynamically with task-specific configuration.

命令行准则Command line guidelines

  • 默认情况下,命令行在容器中指定不通过 shell 启动的单个进程By default, the command line specifies a single process that starts without a shell in the container. 例如,命令行可能运行某个 Python 脚本或可执行文件。For example, the command line might run a Python script or executable file. 该进程可以指定其他参数或自变量。The process can specify additional parameters or arguments.

  • 若要执行多个命令,请通过设置容器操作系统中支持的 shell 环境来开始命令行。To execute multiple commands, begin your command line by setting a shell environment that is supported in the container operating system. 示例:Examples:

    操作系统Operating system 默认 shellDefault shell
    UbuntuUbuntu /bin/bash
    AlpineAlpine /bin/sh
    WindowsWindows cmd

    遵循 shell 的约定,组合多个命令以按顺序运行。Follow the conventions of the shell to combine multiple commands to run in sequence.

  • 根据容器配置,你可能需要设置命令行可执行文件或自变量的完整路径。Depending on the container configuration, you might need to set a full path to the command line executable or arguments.

  • 为容器实例设置适当的重启策略,具体取决于命令行指定的是长时间运行的任务还是运行一次运行的任务。Set an appropriate restart policy for the container instance, depending on whether the command-line specifies a long-running task or a run-once task. 例如,对于只运行一次的任务,建议使用 NeverOnFailure 重启策略。For example, a restart policy of Never or OnFailure is recommended for a run-once task.

  • 如果需要容器映像中设置的默认入口点的相关信息,请使用 docker 映像检查命令。If you need information about the default entrypoint set in a container image, use the docker image inspect command.

命令行语法Command line syntax

命令行语法根据用于创建实例的 Azure API 或工具而有所不同。The command line syntax varies depending on the Azure API or tool used to create the instances. 如果指定某个 shell 环境,还应遵守该 shell 的命令语法约定。If you specify a shell environment, also observe the command syntax conventions of the shell.

  • az container create 命令:通过 --command-line 参数传递一个字符串。az container create command: Pass a string with the --command-line parameter. 示例:--command-line "python myscript.py arg1 arg2")。Example: --command-line "python myscript.py arg1 arg2").

  • New-AzureRmContainerGroup Azure PowerShell cmdlet:通过 -Command 参数传递一个字符串。New-AzureRmContainerGroup Azure PowerShell cmdlet: Pass a string with the -Command parameter. 示例:-Command "echo hello"Example: -Command "echo hello".

  • Azure 门户:在容器配置的“命令替代” 属性中,提供一个逗号分隔的字符串(不带引号)。Azure portal: In the Command override property of the container configuration, provide a comma-separated list of strings, without quotes. 示例:python, myscript.py, arg1, arg2)。Example: python, myscript.py, arg1, arg2).

  • 资源管理器模板或 YAML 文件或 Azure SDK 之一:将命令行属性指定为字符串数组。Resource Manager template or YAML file, or one of the Azure SDKs: Specify the command line property as an array of strings. 示例:资源管理器模板中的 JSON 数组 ["python", "myscript.py", "arg1", "arg2"]Example: the JSON array ["python", "myscript.py", "arg1", "arg2"] in a Resource Manager template.

    如果你熟悉 Dockerfile 语法,此格式类似于 CMD 指令的 exec 形式。If you're familiar with Dockerfile syntax, this format is similar to the exec form of the CMD instruction.

示例Examples

Azure CLIAzure CLI 门户Portal 模板Template
单个命令Single command --command-line "python myscript.py arg1 arg2" 命令替代python, myscript.py, arg1, arg2Command override: python, myscript.py, arg1, arg2 "command": ["python", "myscript.py", "arg1", "arg2"]
多个命令Multiple commands --command-line "/bin/bash -c 'mkdir test; touch test/myfile; tail -f /dev/null'" 命令替代/bin/bash, -c, mkdir test; touch test/myfile; tail -f /dev/nullCommand override: /bin/bash, -c, mkdir test; touch test/myfile; tail -f /dev/null "command": ["/bin/bash", "-c", "mkdir test; touch test/myfile; tail -f /dev/null"]

Azure CLI 示例Azure CLI example

例如,修改 microsoft/aci-wordcount 容器映像的行为,该映像分析莎士比亚的《哈姆雷特》 中的文本来查找最常出现的单词。As an example, modify the behavior of the microsoft/aci-wordcount container image, which analyzes text in Shakespeare's Hamlet to find the most frequently occurring words. 你可以设置一个指向不同文本源的命令行,而不是分析《哈姆雷特》 。Instead of analyzing Hamlet, you could set a command line that points to a different text source.

若要查看 microsoft/aci-wordcount 容器在分析默认文本时的输出,请使用以下 az container create 命令来运行它。To see the output of the microsoft/aci-wordcount container when it analyzes the default text, run it with the following az container create command. 未指定任何启动命令行,因此将运行默认容器命令。No start command line is specified, so the default container command runs. 为了进行说明,此示例设置了环境变量来查找长度至少为五个字母且排名前 3 的单词:For illustration purposes, this example sets environment variables to find the top 3 words that are at least five letters long:

az container create \
    --resource-group myResourceGroup \
    --name mycontainer1 \
    --image mcr.microsoft.com/azuredocs/aci-wordcount:latest \
    --environment-variables NumWords=3 MinLength=5 \
    --restart-policy OnFailure

在容器的状态显示为 Terminated 后(使用 az container show 来检查状态),通过 az container logs 显示日志来查看输出。Once the container's state shows as Terminated (use az container show to check state), display the log with az container logs to see the output.

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

输出:Output:

[('HAMLET', 386), ('HORATIO', 127), ('CLAUDIUS', 120)]

现在,通过指定一个不同的命令行来分析不同文本。Now set up a second example container to analyze different text by specifying a different command line. 容器执行的 Python 脚本 wordcount.py 接受一个 URL 作为参数,并处理该页面的内容而不是默认内容。The Python script executed by the container, wordcount.py, accepts a URL as an argument, and processes that page's content instead of the default.

例如,若要确定《罗密欧和朱丽叶》 中长度至少为五个字母且排名前 3 的单词,请使用以下命令:For example, to determine the top 3 words that are at least five letters long in Romeo and Juliet:

az container create \
    --resource-group myResourceGroup \
    --name mycontainer2 \
    --image mcr.microsoft.com/azuredocs/aci-wordcount:latest \
    --restart-policy OnFailure \
    --environment-variables NumWords=3 MinLength=5 \
    --command-line "python wordcount.py http://shakespeare.mit.edu/romeo_juliet/full.html"

同样,容器状态显示为 Terminated 后,可通过显示容器的日志来查看输出:Again, once the container is Terminated, view the output by showing the container's logs:

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

输出:Output:

[('ROMEO', 177), ('JULIET', 134), ('CAPULET', 119)]

后续步骤Next steps

基于任务的方案(例如对使用多个容器的数据库进行批处理)可以在运行时充分利用自定义命令行。Task-based scenarios, such as batch processing a large dataset with several containers, can benefit from custom command lines at runtime. 若要详细了解如何运行基于任务的容器,请参阅使用重启策略运行容器化任务For more information about running task-based containers, see Run containerized tasks with restart policies.