您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

在 Docker 容器中运行 Azure CLIRun Azure CLI in a Docker container

可以使用 Docker 运行已预装 Azure CLI 的独立 Linux 容器。You can use Docker to run a standalone Linux container with the Azure CLI pre-installed. Docker 可让你快速开始创建一个用于运行 CLI 的隔离环境。Docker gets you started quickly with an isolated environment to run the CLI in. 映像也可以用作你自己的部署的基础。The image can also be used as a base for your own deployments.

在 Docker 容器中运行Run in a Docker container

备注

Azure CLI 已迁移到 Microsoft 容器注册表The Azure CLI has migrated to Microsoft Container Registry. Docker 中心上的现有标记仍然受支持,但新版本将仅作为 mcr.microsoft.com/azure-cli 提供。Existing tags on Docker Hub are still supported, but new releases will only be available as mcr.microsoft.com/azure-cli.

请使用 docker run 安装 CLI。Install the CLI using docker run.

docker run -it mcr.microsoft.com/azure-cli

备注

若要从用户环境中选取 SSH 密钥,请使用 -v ${HOME}/.ssh:/root/.ssh 在环境中装载 SSH 密钥。If you want to pick up the SSH keys from your user environment, use -v ${HOME}/.ssh:/root/.ssh to mount your SSH keys in the environment.

docker run -it -v ${HOME}/.ssh:/root/.ssh mcr.microsoft.com/azure-cli

CLI 作为 /usr/local/bin 中的 az 命令安装在映像中。The CLI is installed on the image as the az command in /usr/local/bin. 若要登录,请运行 az login 命令。To sign in, run the az login command.

  1. 运行 login 命令。Run the login command.

    az login
    

    如果 CLI 可以打开默认浏览器,它将这样做并加载 Azure 登录页。If the CLI can open your default browser, it will do so and load an Azure sign-in page.

    否则,请在 https://aka.ms/devicelogin 处打开浏览器页,然后输入终端中显示的授权代码。Otherwise, open a browser page at https://aka.ms/devicelogin and enter the authorization code displayed in your terminal.

  2. 在浏览器中使用帐户凭据登录。Sign in with your account credentials in the browser.

若要详细了解不同的身份验证方法,请参阅使用 Azure CLI 登录To learn more about different authentication methods, see Sign in with the Azure CLI.

更新 Docker 映像Update Docker image

使用 Docker 进行更新需要拉取新映像和重新创建任何现有的容器。Updating with Docker requires both pulling the new image and re-creating any existing containers. 因此,应先行尝试,避免将托管 CLI 的容器用作数据存储。For this reason, you should try to avoid using a container that hosts the CLI as a data store.

使用 docker pull 更新本地映像。Update your local image with docker pull.

docker pull mcr.microsoft.com/azure-cli

卸载 Docker 映像Uninstall Docker image

如果你决定卸载 Azure CLI,我们会很遗憾。If you decide to uninstall the Azure CLI, we're sorry to see you go. 在卸载之前,请使用 az feedback 命令告诉我们在哪些方面需要改进或修复。Before you uninstall, use the az feedback command to let us know what could be improved or fixed. 我们的目标是消除 Azure CLI 中的 bug 并使其变得用户友好。Our goal is to make the Azure CLI bug-free and user-friendly. 如果你在发现 bug 后提出 GitHub 问题,我们将十分感激。If you found a bug, we'd appreciate it if you file a GitHub issue.

停止运行 CLI 映像的任何容器后,请删除该映像。After halting any containers running the CLI image, remove it.

docker rmi mcr.microsoft.com/azure-cli

后续步骤Next Steps

现在你已准备好使用 Azure CLI,下面简要介绍其功能和常用命令。Now that you're ready to use the Azure CLI, take a short tour of its features and common commands.