View a markdown version of this page

Amazon ECR 入門 - AWS Identity and Access Management

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

Amazon ECR 入門

以下程式碼範例顯示做法:

  • 建立 Docker 映像

  • 建立 Amazon ECR 儲存庫

  • 刪除資源

Bash
AWS CLI 搭配 Bash 指令碼
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在範例開發人員教學課程儲存庫中設定和執行。

#!/bin/bash # Amazon ECR Getting Started Script # This script demonstrates the lifecycle of a Docker image in Amazon ECR # Set up logging LOG_FILE="ecr-tutorial.log" exec > >(tee -a "$LOG_FILE") 2>&1 echo "===================================================" echo "Amazon ECR Getting Started Tutorial" echo "===================================================" echo "This script will:" echo "1. Create a Docker image" echo "2. Create an Amazon ECR repository" echo "3. Authenticate to Amazon ECR" echo "4. Push the image to Amazon ECR" echo "5. Pull the image from Amazon ECR" echo "6. Clean up resources (optional)" echo "===================================================" # Check prerequisites echo "Checking prerequisites..." # Check if AWS CLI is installed if ! command -v aws &> /dev/null; then echo "ERROR: AWS CLI is not installed. Please install it before running this script." echo "Visit https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html for installation instructions." exit 1 fi # Check if AWS CLI is configured if ! aws sts get-caller-identity &> /dev/null; then echo "ERROR: AWS CLI is not configured properly. Please run 'aws configure' to set up your credentials." exit 1 fi # Check if Docker is installed if ! command -v docker &> /dev/null; then echo "ERROR: Docker is not installed. Please install Docker before running this script." echo "Visit https://docs.docker.com/get-docker/ for installation instructions." exit 1 fi # Check if Docker daemon is running if ! docker info &> /dev/null; then echo "ERROR: Docker daemon is not running. Please start Docker and try again." exit 1 fi echo "All prerequisites met." # Initialize variables REPO_URI="" TIMEOUT_CMD="timeout 300" # 5-minute timeout for long-running commands # Function to handle errors handle_error() { echo "ERROR: $1" echo "Check the log file for details: $LOG_FILE" echo "===================================================" echo "Resources created:" echo "- Docker image: hello-world (local)" if [ -n "$REPO_URI" ]; then echo "- ECR Repository: hello-repository" echo "- ECR Image: $REPO_URI:latest" fi echo "===================================================" echo "Attempting to clean up resources..." cleanup exit 1 } # Function to clean up resources cleanup() { echo "===================================================" echo "Cleaning up resources..." # Delete the image from ECR if it exists if [ -n "$REPO_URI" ]; then echo "Deleting image from ECR repository..." aws ecr batch-delete-image --repository-name hello-repository --image-ids imageTag=latest || echo "Failed to delete image, it may not exist or may have already been deleted." fi # Delete the ECR repository if it exists if [ -n "$REPO_URI" ]; then echo "Deleting ECR repository..." aws ecr delete-repository --repository-name hello-repository --force || echo "Failed to delete repository, it may not exist or may have already been deleted." fi # Remove local Docker image echo "Removing local Docker image..." docker rmi hello-world:latest 2>/dev/null || echo "Failed to remove local image, it may not exist or may have already been deleted." if [ -n "$REPO_URI" ]; then docker rmi "$REPO_URI:latest" 2>/dev/null || echo "Failed to remove tagged image, it may not exist or may have already been deleted." fi echo "Cleanup completed." echo "===================================================" } # Step 1: Create a Docker image echo "Step 1: Creating a Docker image" # Create Dockerfile echo "Creating Dockerfile..." cat > Dockerfile << 'EOF' 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 EOF # Build Docker image echo "Building Docker image..." $TIMEOUT_CMD docker build -t hello-world . || handle_error "Failed to build Docker image or operation timed out after 5 minutes" # Verify image was created echo "Verifying Docker image..." docker images --filter reference=hello-world || handle_error "Failed to list Docker images" echo "Docker image created successfully." # Step 2: Create an Amazon ECR repository echo "Step 2: Creating an Amazon ECR repository" # Get AWS account ID echo "Getting AWS account ID..." AWS_ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text) if [[ -z "$AWS_ACCOUNT_ID" || "$AWS_ACCOUNT_ID" == *"error"* ]]; then handle_error "Failed to get AWS account ID. Make sure your AWS credentials are configured correctly." fi echo "AWS Account ID: $AWS_ACCOUNT_ID" # Get current region AWS_REGION=$(aws configure get region) if [[ -z "$AWS_REGION" ]]; then AWS_REGION="us-east-1" # Default to us-east-1 if no region is configured echo "No AWS region configured, defaulting to $AWS_REGION" else echo "Using AWS region: $AWS_REGION" fi # Create ECR repository echo "Creating ECR repository..." REPO_RESULT=$(aws ecr create-repository --repository-name hello-repository) if [[ -z "$REPO_RESULT" || "$REPO_RESULT" == *"error"* ]]; then handle_error "Failed to create ECR repository" fi # Extract repository URI REPO_URI="$AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/hello-repository" echo "Repository URI: $REPO_URI" # Step 3: Authenticate to Amazon ECR echo "Step 3: Authenticating to Amazon ECR" echo "Getting ECR login password..." aws ecr get-login-password --region "$AWS_REGION" | docker login --username AWS --password-stdin "$AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com" || handle_error "Failed to authenticate to ECR" echo "Successfully authenticated to ECR." # Step 4: Push the image to Amazon ECR echo "Step 4: Pushing the image to Amazon ECR" # Tag the image echo "Tagging Docker image..." docker tag hello-world:latest "$REPO_URI:latest" || handle_error "Failed to tag Docker image" # Push the image with timeout echo "Pushing image to ECR..." $TIMEOUT_CMD docker push "$REPO_URI:latest" || handle_error "Failed to push image to ECR or operation timed out after 5 minutes" echo "Successfully pushed image to ECR." # Step 5: Pull the image from Amazon ECR echo "Step 5: Pulling the image from Amazon ECR" # Remove local image to ensure we're pulling from ECR echo "Removing local tagged image..." docker rmi "$REPO_URI:latest" || echo "Warning: Failed to remove local tagged image" # Pull the image with timeout echo "Pulling image from ECR..." $TIMEOUT_CMD docker pull "$REPO_URI:latest" || handle_error "Failed to pull image from ECR or operation timed out after 5 minutes" echo "Successfully pulled image from ECR." # List resources created echo "===================================================" echo "Resources created:" echo "- Docker image: hello-world (local)" echo "- ECR Repository: hello-repository" echo "- ECR Image: $REPO_URI:latest" echo "===================================================" # Ask user if they want to clean up resources echo "" echo "===========================================" echo "CLEANUP CONFIRMATION" echo "===========================================" echo "Do you want to clean up all created resources? (y/n): " read -r CLEANUP_CHOICE if [[ "$CLEANUP_CHOICE" =~ ^[Yy]$ ]]; then # Step 6: Delete the image from ECR echo "Step 6: Deleting the image from ECR" DELETE_IMAGE_RESULT=$(aws ecr batch-delete-image --repository-name hello-repository --image-ids imageTag=latest) if [[ -z "$DELETE_IMAGE_RESULT" || "$DELETE_IMAGE_RESULT" == *"error"* ]]; then echo "Warning: Failed to delete image from ECR" else echo "Successfully deleted image from ECR." fi # Step 7: Delete the ECR repository echo "Step 7: Deleting the ECR repository" DELETE_REPO_RESULT=$(aws ecr delete-repository --repository-name hello-repository --force) if [[ -z "$DELETE_REPO_RESULT" || "$DELETE_REPO_RESULT" == *"error"* ]]; then echo "Warning: Failed to delete ECR repository" else echo "Successfully deleted ECR repository." fi # Remove local Docker images echo "Removing local Docker images..." docker rmi hello-world:latest 2>/dev/null || echo "Warning: Failed to remove local image" echo "All resources have been cleaned up." else echo "Resources were not cleaned up. You can manually clean up later with:" echo "aws ecr batch-delete-image --repository-name hello-repository --image-ids imageTag=latest" echo "aws ecr delete-repository --repository-name hello-repository --force" echo "docker rmi hello-world:latest" echo "docker rmi $REPO_URI:latest" fi echo "===================================================" echo "Tutorial completed!" echo "Log file: $LOG_FILE" echo "==================================================="

如需 AWS SDK 開發人員指南和程式碼範例的完整清單,請參閱 搭配 AWS SDK 使用此服務。此主題也包含有關入門的資訊和舊版 SDK 的詳細資訊。