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

使用 Docker CLI 将第一个映像推送到专用 Docker 容器注册表Push your first image to a private Docker container registry using the Docker CLI

Azure 容器注册表存储和管理专用 Docker 容器映像,其方式类似于 Docker Hub 存储公共 Docker 映像。An Azure container registry stores and manages private Docker container images, similar to the way Docker Hub stores public Docker images. 可以使用 Docker 命令行接口 (Docker CLI) 对容器注册表执行登录推送提取和其他操作。You can use the Docker command-line interface (Docker CLI) for login, push, pull, and other operations on your container registry.

以下步骤从公共 Docker 中心注册表下载正式的 Nginx 映像,为专用 Azure 容器注册表标记该映像,将其推入到注册表,然后从注册表提取。In the following steps, you download an official Nginx image from the public Docker Hub registry, tag it for your private Azure container registry, push it to your registry, and then pull it from the registry.

先决条件Prerequisites

  • Azure 容器注册表 - 在 Azure 订阅中创建容器注册表。Azure container registry - Create a container registry in your Azure subscription. 例如,使用 Azure 门户Azure CLIFor example, use the Azure portal or the Azure CLI.
  • Docker CLI - 还必须在本地安装 Docker。Docker CLI - You must also have Docker installed locally. Docker 提供的包可在任何 macOSWindowsLinux 系统上轻松配置 Docker。Docker provides packages that easily configure Docker on any macOS, Windows, or Linux system.

登录到注册表Log in to a registry

通过多种方式验证专用容器注册表。There are several ways to authenticate to your private container registry. 在命令行中操作时,建议的方法是使用 Azure CLI 命令 az acr loginThe recommended method when working in a command line is with the Azure CLI command az acr login. 例如,若要登录到名为 myregistry 的注册表:For example, to log in to a registry named myregistry:

az acr login --name myregistry

也可以使用 docker login 登录。You can also log in with docker login. 例如,你可能在自动化方案中向注册表分配了服务主体For example, you might have assigned a service principal to your registry for an automation scenario. 运行以下命令时,收到提示后,请以交互方式提供服务主体 appID(用户名)和密码。When you run the following command, interactively provide the service principal appID (username) and password when prompted. 有关管理登录凭据的最佳做法,请参阅 docker login 命令参考:For best practices to manage login credentials, see the docker login command reference:

docker login myregistry.azurecr.io

完成后,这两个命令将返回 Login SucceededBoth commands return Login Succeeded once completed.

提示

使用 docker login 以及标记要推送到注册表的映像时,请始终指定完全限定的注册表名称(全部小写)。Always specify the fully qualified registry name (all lowercase) when you use docker login and when you tag images for pushing to your registry. 在本文的示例中,完全限定的名称为 myregistry.azurecr.ioIn the examples in this article, the fully qualified name is myregistry.azurecr.io.

提取正式的 Nginx 映像Pull the official Nginx image

首次将公共 Nginx 映像提取到本地计算机。First, pull the public Nginx image to your local computer.

docker pull nginx

在本地运行容器Run the container locally

执行以下 docker run 命令,在端口 8080 上以交互方式启动 Nginx 容器的本地实例 (-it)。Execute following docker run command to start a local instance of the Nginx container interactively (-it) on port 8080. --rm 参数指定在停止容器时应将其删除。The --rm argument specifies that the container should be removed when you stop it.

docker run -it --rm -p 8080:80 nginx

浏览到 http://localhost:8080,查看由正在运行的容器中的 Nginx 提供服务的默认网页。Browse to http://localhost:8080 to view the default web page served by Nginx in the running container. 应看到类似于下面的页面:You should see a page similar to the following:

本地计算机上的 Nginx

由于已使用 -it 以交互方式启动了容器,因此在浏览器中导航到该容器后,可在命令行中查看 Nginx 服务器的输出。Because you started the container interactively with -it, you can see the Nginx server's output on the command line after navigating to it in your browser.

若要停止并删除容器,请按 Control+CTo stop and remove the container, press Control+C.

创建映像的别名Create an alias of the image

运行 docker tag,使用注册表的完全限定路径创建映像的别名。Use docker tag to create an alias of the image with the fully qualified path to your registry. 此示例指定了 samples 命名空间,以免注册表根目录中出现混乱。This example specifies the samples namespace to avoid clutter in the root of the registry.

docker tag nginx myregistry.azurecr.io/samples/nginx

有关使用命名空间进行标记的详细信息,请参阅 Azure 容器注册表的最佳做法存储库命名空间部分。For more information about tagging with namespaces, see the Repository namespaces section of Best practices for Azure Container Registry.

将映像推送到注册表Push the image to your registry

使用专用注册表的完全限定路径标记映像后,可以使用 docker push 将其推送到注册表:Now that you've tagged the image with the fully qualified path to your private registry, you can push it to the registry with docker push:

docker push myregistry.azurecr.io/samples/nginx

从注册表中提取映像Pull the image from your registry

使用 docker pull 命令从注册表提取映像:Use the docker pull command to pull the image from your registry:

docker pull myregistry.azurecr.io/samples/nginx

启动 Nginx 容器Start the Nginx container

使用 docker run 命令运行已从注册表提取的映像:Use the docker run command to run the image you've pulled from your registry:

docker run -it --rm -p 8080:80 myregistry.azurecr.io/samples/nginx

浏览到 http://localhost:8080 以查看正在运行的容器。Browse to http://localhost:8080 to view the running container.

若要停止并删除容器,请按 Control+CTo stop and remove the container, press Control+C.

删除映像(可选)Remove the image (optional)

如果不再需要 Nginx 映像,可以使用 docker rmi 命令在本地将其删除。If you no longer need the Nginx image, you can delete it locally with the docker rmi command.

docker rmi myregistry.azurecr.io/samples/nginx

若要从 Azure 容器注册表中删除映像,可以使用 Azure CLI 命令az acr repository deleteTo remove images from your Azure container registry, you can use the Azure CLI command az acr repository delete. 例如,以下命令删除 samples/nginx:latest 标记引用的清单、所有唯一的层数据以及引用此清单的其他所有标记。For example, the following command deletes the manifest referenced by the samples/nginx:latest tag, any unique layer data, and all other tags referencing the manifest.

az acr repository delete --name myregistry --image samples/nginx:latest

后续步骤Next steps

了解基础知识后,便可以开始使用注册表了!Now that you know the basics, you're ready to start using your registry! 例如,将容器映像从注册表部署到:For example, deploy container images from your registry to:

可以选择安装适用于 Visual Studio Code 的 Docker 扩展以及适用于 Azure 容器注册表的 Azure 帐户扩展。Optionally install the Docker Extension for Visual Studio Code and the Azure Account extension to work with your Azure container registries. 通过 Azure 容器注册表拉取和推送映像,或者运行 ACR 任务,这一切都可以在 Visual Studio Code 中进行。Pull and push images to an Azure container registry, or run ACR Tasks, all within Visual Studio Code.