在运行的 Azure 容器实例中执行命令Execute a command in a running Azure container instance

Azure 容器实例支持在运行的容器中执行命令。Azure Container Instances supports executing a command in a running container. 在应用程序开发和故障排除期间,在已启动的容器中运行命令特别有用。Running a command in a container you've already started is especially helpful during application development and troubleshooting. 此功能的最常见用途是启动交互式 shell,以便能够在运行的容器中调试问题。The most common use of this feature is to launch an interactive shell so that you can debug issues in a running container.

使用 Azure CLI 运行命令Run a command with Azure CLI

Azure CLI 中使用 az container exec 在运行的容器中执行命令:Execute a command in a running container with az container exec in the Azure CLI:

az container exec --resource-group <group-name> --name <container-group-name> --exec-command "<command>"

例如,若要在 Nginx 容器中启动 Bash shell:For example, to launch a Bash shell in an Nginx container:

az container exec --resource-group myResourceGroup --name mynginx --exec-command "/bin/bash"

在以下示例输出中,Bash shell 已在运行的 Linux 容器中启动,提供一个可用于执行 ls 的终端:In the example output below, the Bash shell is launched in a running Linux container, providing a terminal in which ls is executed:

root@caas-83e6c883014b427f9b277a2bba3b7b5f-708716530-2qv47:/# ls
bin   dev  home  lib64  mnt  proc  run   srv  tmp  var
boot  etc  lib   media  opt  root  sbin  sys  usr
root@caas-83e6c883014b427f9b277a2bba3b7b5f-708716530-2qv47:/# exit
exit
Bye.

多容器组Multi-container groups

如果容器组包含多个容器(例如应用程序容器和日志记录分支),请使用 --container-name 指定要在其中运行命令的容器的名称。If your container group has multiple containers, such as an application container and a logging sidecar, specify the name of the container in which to run the command with --container-name.

例如,容器组 mynginx 包含两个容器:nginx-apploggerFor example, in the container group mynginx are two containers, nginx-app and logger. 若要在 nginx-app 容器中启动 shell:To launch a shell on the nginx-app container:

az container exec --resource-group myResourceGroup --name mynginx --container-name nginx-app --exec-command "/bin/bash"

限制Restrictions

Azure 容器实例目前支持使用 az container exec 启动单个进程;不能传递命令参数。Azure Container Instances currently supports launching a single process with az container exec, and you cannot pass command arguments. 例如,不能像 sh -c "echo FOO && echo BAR" 一样串接命令,或执行 echo FOOFor example, you cannot chain commands like in sh -c "echo FOO && echo BAR", or execute echo FOO.

后续步骤Next steps

排查 Azure 容器实例中的容器和部署问题中了解其他故障排除工具和常见部署问题。Learn about other troubleshooting tools and common deployment issues in Troubleshoot container and deployment issues in Azure Container Instances.