🚨 AWS 128:设置 Amazon ECR 并推送 Docker 镜像

发布: (2026年1月8日 GMT+8 12:23)
3 min read
原文: Dev.to

Source: Dev.to

Hey Cloud Builders 👋
Welcome to Day 28 of the #100DaysOfCloud Challenge! In this post we’ll create a private Amazon Elastic Container Registry (ECR) repository, build a custom Python app Docker image, and push it to the registry.

Overview

Amazon ECR 是一个完全托管的 Docker 容器注册表,帮助开发者安全地存储、管理和部署容器镜像。使用私有注册表可以让你的应用制品对公众保持隐藏。

Step‑by‑Step Workflow

我们将按以下步骤有序进行:

  1. 创建仓库
  2. 构建 Docker 镜像
  3. 使用 ECR 进行 Docker 认证
  4. 标记并推送镜像
  5. 验证上传结果

Create the Repository

aws ecr create-repository \
    --repository-name devops-ecr \
    --region us-east-1

Note: ECR 仓库是区域(region)特定的。确保你的 AWS CLI 已配置为与你打算创建仓库的同一区域。

创建完成后,记录 Repository URI,形如:

.dkr.ecr..amazonaws.com/devops-ecr

Build the Docker Image

cd /root/pyapp          # Directory containing the Dockerfile
docker build -t pyapp .   # Build a local image named "pyapp"

Authenticate Docker with ECR

生成一个 12 小时有效的登录令牌并将其传递给 Docker:

aws ecr get-login-password --region  |
docker login --username AWS --password-stdin .dkr.ecr..amazonaws.com

Tag and Push the Image

# Tag the local image with the full ECR URI
docker tag pyapp:latest .dkr.ecr..amazonaws.com/devops-ecr:latest

# Push the image to the repository
docker push .dkr.ecr..amazonaws.com/devops-ecr:latest

Verify the Upload

  1. 打开 ECR 控制台
  2. 选择 devops-ecr 仓库。
  3. 确认出现了带有 latest 标签的镜像。

你也可以在另一台机器上(先完成认证)尝试拉取镜像,以确保其已正确存储:

docker pull .dkr.ecr..amazonaws.com/devops-ecr:latest

Common Issues & Tips

IssueResolution
Incorrect regionVerify that the repository URI matches the region configured in your CLI.
Access deniedEnsure the IAM user has the AmazonEC2ContainerRegistryFullAccess policy attached.
Missing tagTag the local image with the full ECR URI before pushing.
Login expiryDocker login sessions expire after 12 hours; re‑run the login command when needed.

Next Steps

镜像存放在 ECR 后,你可以将其部署到 ECSEKSLambda 等服务。使用 CI/CD 流水线实现构建‑推送的自动化是自然的下一步。

Resources

  • KodeKloud Engineer – Practice Labs – Try these tasks in a real AWS environment.
  • LinkedIn: Hritik Raj
  • GitHub: 100 Days of Cloud (⭐ Support the journey)
Back to Blog

相关文章

阅读更多 »

启动 AWS EC2 实例

介绍 本指南将带您完成启动 AWS EC2 实例、安装 Docker 并在 Docker 容器中运行 NGINX 的全过程。完成后您将…

如何部署 AWS ELASTIC BEANSTALK

概述:AWS Elastic Beanstalk 是一种托管的云服务,允许您部署和运行 Web 应用程序,而无需担心底层基础设施。我...