将 Amazon ECR 与 Amazon CLI 结合使用 - Amazon ECR
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

将 Amazon ECR 与 Amazon CLI 结合使用

以下步骤将指导您完成首次使用 Docker CLI 和 Amazon CLI 将容器镜像推送到私有 Amazon ECR 存储库所需的步骤。

有关可用于管理 Amazon 资源的其他工具的更多信息,包括不同的 Amazon 开发工具包、IDE 工具包和 Windows PowerShell 命令行工具,请参阅 http://aws.amazon.com/tools/

先决条件

开始之前,请确保您已完成对 Amazon ECR 进行设置中的步骤。

如果您尚未安装最新 Amazon CLI 和 Docker 并且未准备好使用,请使用以下步骤来安装这两个工具。

安装 Amazon CLI

您可以使用 Amazon 命令行工具,在命令行中发出命令来执行 Amazon ECR 和其他 Amazon 任务。与使用控制台相比,此方法更快、更方便。命令行工具也非常适用于构建执行 Amazon 任务的脚本。

要对 Amazon ECR 使用 Amazon CLI,请安装最新版本的 Amazon CLI。有关信息,请参阅《Amazon Command Line Interface 用户指南》中的安装 Amazon Command Line Interface

安装 Docker

Docker 适用于许多不同的操作系统,包括大多数现代 Linux 分发版 (如 Ubuntu) 甚至 MacOS 和 Windows。有关如何在特定的操作系统上安装 Docker 的更多信息,请转到 Docker 安装指南

您无需本地开发系统即可使用 Docker。如果您已在使用 Amazon EC2,则可启动 Amazon Linux 2023 实例并安装 Docker 以开始使用。

如果您已安装 Docker,请跳到步骤 1:创建 Docker 镜像

使用 Amazon Linux 2023 AMI 在 Amazon EC2 实例上安装 Docker
  1. 使用最新版 Amazon Linux 2023 AMI 启动实例。有关更多信息,请参阅适用于 Linux 实例的 Amazon EC2 用户指南中的启动实例

  2. 连接到您的 实例。有关更多信息,请参阅适用于 Linux 实例的 Amazon EC2 用户指南中的连接到您的 Linux 实例

  3. 更新实例上已安装的程序包和程序包缓存。

    sudo yum update -y
  4. 安装最新的 Docker Community Edition 程序包。

    sudo yum install docker
  5. 启动 Docker 服务。

    sudo service docker start
  6. ec2-user 添加到 docker 组,以便您能够执行 Docker 命令,而无需使用 sudo

    sudo usermod -a -G docker ec2-user
  7. 退出,再重新登录以接受新的 docker 组权限。您可以关闭当前的 SSH 终端窗口并在新终端窗口中重新连接到实例,完成这一过程。您的新 SSH 会话将具有相应的 docker 组权限。

  8. 验证 ec2-user 是否能在没有 sudo 的情况下运行 Docker 命令。

    docker info
    注意

    在某些情况下,您可能需要重新启动实例,以便为 ec2-user 提供访问 Docker 进程守护程序的权限。如果您看到以下错误,请尝试重启您的实例:

    Cannot connect to the Docker daemon. Is the docker daemon running on this host?

步骤 1:创建 Docker 镜像

在本步骤中,您将创建简单 Web 应用程序的 Docker 映像,并在本地系统或 Amazon EC2 实例上测试此映像。

创建简单 Web 应用程序的 Docker 镜像
  1. 创建名为 Dockerfile 的文件。Dockerfile 是一个清单文件,描述了用于 Docker 镜像的基本镜像以及要安装的项目以及在此项目上运行的内容。有关 Dockerfile 的更多信息,请转到 Dockerfile 参考

    touch Dockerfile
  2. 编辑您刚刚创建的 Dockerfile 并添加以下内容。

    FROM public.ecr.aws/amazonlinux/amazonlinux:latest # Install dependencies RUN yum update -y && \ yum install -y httpd # Install apache and write hello world message RUN echo 'Hello World!' > /var/www/html/index.html # Configure apache RUN echo 'mkdir -p /var/run/httpd' >> /root/run_apache.sh && \ echo 'mkdir -p /var/lock/httpd' >> /root/run_apache.sh && \ echo '/usr/sbin/httpd -D FOREGROUND' >> /root/run_apache.sh && \ chmod 755 /root/run_apache.sh EXPOSE 80 CMD /root/run_apache.sh

    此 Dockerfile 使用 Amazon ECR Public 上托管的 Amazon Linux 2 公有映像。RUN 指令更新包缓存,安装一些适用于 Web 服务器的软件包,然后将“Hello World!” 内容写入 Web 服务器的文档根目录。EXPOSE 指令在容器上公开端口 80,CMD 指令启动 Web 服务器。

  3. 从您的 Dockerfile 生成 Docker 镜像。

    注意

    Docker 的某些版本可能需要在以下命令中使用 Dockerfile 完整路径,而不是所示的相对路径。

    docker build -t hello-world .
  4. 列出容器映像。

    docker images --filter reference=hello-world

    输出:

    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    hello-world         latest              e9ffedc8c286        4 minutes ago       194MB
  5. 运行新构建的镜像。-p 80:80 选项将容器上公开的端口 80 映射到主机系统上的端口 80。有关 docker run 的更多信息,请转到 Docker 运行参考

    docker run -t -i -p 80:80 hello-world
    注意

    来自 Apache Web 服务器的输出将显示在终端窗口中。您可以忽略“Could not reliably determine the fully qualified domain name”消息。

  6. 打开浏览器并指向正在运行 Docker 并托管您的容器的服务器。

    • 如果您使用的是 EC2 实例,这将是服务器的 Public DNS 值,此值与您用于通过 SSH 连接到实例的地址相同。确保实例的安全组允许端口 80 上的入站流量。

    • 如果您正在本地运行 Docker,可将您的浏览器指向 http://localhost/

    • 如果您正在 Windows 或 Mac 计算机上使用 docker-machine,请使用 docker-machine ip 命令查找托管 Docker 的 VirtualBox VM 的 IP 地址,并将 machine-name 替换为您正在使用的 Docker 计算机的名称。

      docker-machine ip machine-name

    您应看到一个显示“Hello World!”语句的 网页。

  7. 通过键入 Ctrl + c 来停止 Docker 容器。

步骤 2:向您的默认注册表验证身份

安装并配置 Amazon CLI 后,向默认注册表验证 Docker CLI 的身份。这样一来,docker 命令可以通过 Amazon ECR 推送和提取镜像。Amazon CLI 提供 get-login-password 命令来简化身份验证过程。

要使用 get-login-password 针对 Amazon ECR 注册表验证 Docker,请运行 aws ecr get-login-password 命令。将身份验证令牌传递给 docker login 命令时,将值 AWS 用作用户名,并指定要对其进行身份验证的 Amazon ECR 注册表 URI。如果对多个注册表进行身份验证,则必须针对每个注册表重复该命令。

重要

如果收到错误,请安装或更新到最新版本的 Amazon CLI。有关更多信息,请参阅《Amazon Command Line Interface 用户指南》中的安装 Amazon Command Line Interface

  • get-login-password (Amazon CLI)

    aws ecr get-login-password --region region | docker login --username AWS --password-stdin aws_account_id.dkr.ecr.region.amazonaws.com
  • Get-ECRLoginCommand (Amazon Tools for Windows PowerShell)

    (Get-ECRLoginCommand).Password | docker login --username AWS --password-stdin aws_account_id.dkr.ecr.region.amazonaws.com

步骤 3:创建存储库

现在您已拥有可推送到 Amazon ECR 的镜像,还必须创建一个存储库来保存它。在本示例中,您创建一个名称为 hello-repository 的存储库,稍后将推送 hello-world:latest 镜像到这里。要创建存储库,请运行以下命令:

aws ecr create-repository \ --repository-name hello-repository \ --region region

步骤 4:推送镜像到 Amazon ECR

现在您可以推送镜像到上一部分中创建的 Amazon ECR 存储库。您使用 docker CLI 推送镜像,但必须满足一些先决条件才能正常工作:

  • 安装最低版本的 docker:1.7

  • 已使用 docker login 配置 Amazon ECR 授权令牌。

  • Amazon ECR 存储库存在且用户有向该存储库推送的权限。

在满足这些先决条件后,即可将镜像推送到您在帐户的默认注册表中新创建的存储库中。

标记镜像并推送到 Amazon ECR
  1. 列出您存储在本地的镜像,以识别要标记和推送的镜像。

    docker images

    输出:

    REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
    hello-world         latest              e9ffedc8c286        4 minutes ago       241MB
  2. 标记镜像并推送到存储库。

    docker tag hello-world:latest aws_account_id.dkr.ecr.region.amazonaws.com/hello-repository
  3. 推送镜像。

    docker push aws_account_id.dkr.ecr.region.amazonaws.com/hello-repository

    输出:

    The push refers to a repository [aws_account_id.dkr.ecr.region.amazonaws.com/hello-repository] (len: 1)
    e9ae3c220b23: Pushed
    a6785352b25c: Pushed
    0998bf8fb9e9: Pushed
    0a85502c06c9: Pushed
    latest: digest: sha256:215d7e4121b30157d8839e81c4e0912606fca105775bb0636EXAMPLE size: 6774

步骤 5:从 Amazon ECR 提取镜像

在推送镜像到 Amazon ECR 存储库后,可以从其他位置提取该镜像。可使用 docker CLI 提取镜像,但必须满足以下几个先决条件才能正常使用:

  • 安装最低版本的 docker:1.7

  • 已使用 docker login 配置 Amazon ECR 授权令牌。

  • Amazon ECR 存储库存在且用户有从该存储库提取的权限。

在满足这些先决条件后,即可提取您的镜像。要从 Amazon ECR 提取示例镜像,请运行以下命令:

docker pull aws_account_id.dkr.ecr.region.amazonaws.com/hello-repository:latest

输出:

latest: Pulling from hello-repository
0a85502c06c9: Pull complete
0998bf8fb9e9: Pull complete
a6785352b25c: Pull complete
e9ae3c220b23: Pull complete
Digest: sha256:215d7e4121b30157d8839e81c4e0912606fca105775bb0636EXAMPLE
Status: Downloaded newer image for aws_account_id.dkr.region.amazonaws.com/hello-repository:latest

步骤 6:删除镜像

如果您不再需要一个存储库中的某个镜像,则可以使用 batch-delete-image 命令将其删除。要删除镜像,您必须指定它所在的存储库,并指定镜像的 imageTagimageDigest 值。以下示例删除 hello-repository 存储库中镜像标签为 latest 的镜像。

aws ecr batch-delete-image \ --repository-name hello-repository \ --image-ids imageTag=latest \ --region region

步骤 7:删除存储库

如果您不再需要整个存储库中的所有镜像,您可以删除存储库。默认情况下,您不能删除包含镜像的存储库;但是,--force 标记允许此操作。要删除包含镜像的存储库 (及其中的所有镜像),请运行以下命令。

aws ecr delete-repository \ --repository-name hello-repository \ --force \ --region region