AWS ECR 存储库 - 如何从一个账户复制图像并推送到另一个账户

AWS ECR Repository - How to copy images from one account and push to another account

我有两个帐户 - 帐户 A 和帐户 B。在帐户 A 中,我有一个策略,帐户 B 中的用户可以与帐户 A 交互。我在两个帐户中都有一个存储库。帐户 B 没有设置策略(不确定我是否需要帐户 A 的策略才能与之交互)。

我的问题是如何将账户 A 中的 ecr 图像推送到账户 B。我想要一份账户 A 图像的副本到账户 B。这是否可能。

这不是 ECR 当前支持的功能,因此您需要执行以下步骤才能从一个帐户迁移到另一个帐户:

  • aws ecr get-login-password --region <region> | docker login --username AWS --password-stdin <aws_account_id>.dkr.ecr.<region>.amazonaws.com - 运行 这对于源帐户
  • docker pull $SOURCE_IMAGE:$VERSION - 将最新标签拉到本地
  • docker tag $SOURCE_IMAGE:$VERSION $TARGET_IMAGE:$VERSION - Tag 基于原始源图像的新图像
  • aws ecr get-login-password --region <region> | docker login --username AWS --password-stdin <aws_account_id>.dkr.ecr.<region>.amazonaws.com - 运行 目标账户
  • docker push $TARGET_IMAGE:$VERSION - Push docker 图像到目标 ECR 帐户。

如果您想将所有存储库从特定区域移动到另一个帐户(目标帐户),请使用以下脚本。

  • 它将列出帐户 A 中的所有回购
  • 从账号A中一张一张的拉取一张图片
  • 在账户 B 中创建回购
  • 标记图像
  • 推送图片到账号B
#!/bin/bash
TARGET_ACCOUNT_REGION="us-west-2"
DESTINATION_ACCOUNT_REGION="us-west-2"
DESTINATION_ACCOUNT_BASE_PATH="123456.dkr.ecr.$DESTINATION_ACCOUNT_REGION.amazonaws.com/"


REPO_LIST=($(aws ecr describe-repositories --query 'repositories[].repositoryUri' --output text --region $TARGET_ACCOUNT_REGION))
REPO_NAME=($(aws ecr describe-repositories --query 'repositories[].repositoryName' --output text --region $TARGET_ACCOUNT_REGION))


for repo_url in ${!REPO_LIST[@]}; do
        echo "star pulling image ${REPO_LIST[$repo_url]} from Target account"
        docker pull ${REPO_LIST[$repo_url]}


        # Create repo in destination account, remove this line if already created
        aws ecr create-repository --repository-name ${REPO_NAME[$repo_url]}
        docker tag   ${REPO_LIST[$repo_url]} $DESTINATION_ACCOUNT_BASE_PATH/${REPO_NAME[$repo_url]} 
        docker push $DESTINATION_ACCOUNT_BASE_PATH/${REPO_NAME[$repo_url]} 
done

确保您已经获得两个帐户的登录令牌或在脚本中添加这些命令。

        aws ecr get-login-password --region $TARGET_ACCOUNT_REGION | docker login --username AWS --password-stdin ${REPO_LIST[$repo_url]}
        # destination account login, make sure profile set for accoutn destination
        aws ecr get-login-password --region $DESTINATION_ACCOUNT_REGION --profile destination_account | docker login --username AWS --password-stdin ${REPO_LIST[$repo_url]}

aws-cli-cheatsheet

或者您可以使用其中之一

Amazon ECR uses registry settings to configure features at the registry level. The private registry settings are configured separately for each Region. Currently, the only registry setting is the replication setting, which is used to configure cross-Region and cross-account replication of the images in your repositories

交叉 Region/account AWS 中的复制功能

AWS推出了CRR(跨区域复制)和CAR(跨账户复制)Click here for more info