🚨 AWS 128:设置 Amazon ECR 并推送 Docker 镜像
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
我们将按以下步骤有序进行:
- 创建仓库
- 构建 Docker 镜像
- 使用 ECR 进行 Docker 认证
- 标记并推送镜像
- 验证上传结果
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
- 打开 ECR 控制台。
- 选择
devops-ecr仓库。 - 确认出现了带有
latest标签的镜像。
你也可以在另一台机器上(先完成认证)尝试拉取镜像,以确保其已正确存储:
docker pull .dkr.ecr..amazonaws.com/devops-ecr:latest
Common Issues & Tips
| Issue | Resolution |
|---|---|
| Incorrect region | Verify that the repository URI matches the region configured in your CLI. |
| Access denied | Ensure the IAM user has the AmazonEC2ContainerRegistryFullAccess policy attached. |
| Missing tag | Tag the local image with the full ECR URI before pushing. |
| Login expiry | Docker login sessions expire after 12 hours; re‑run the login command when needed. |
Next Steps
镜像存放在 ECR 后,你可以将其部署到 ECS、EKS 或 Lambda 等服务。使用 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)