
Q-Bits: Docker ECR Push and Pull Operations with Amazon Q Developer
This blog post explores how Amazon Q Developer can help you efficiently manage container images
Welcome to another installment of Q-Bits , our regular series showcasing cool ways Amazon employees are leveraging Amazon Q Developer. Today, we're diving into how Amazon Q Developer can assist with efficiently managing container images with Docker ECR Push and Pull Operations.
Introduction
In exploring the capabilities of Amazon Q Developer, I wanted to see if Q could assist me with Elastic Container Registry push and pull operations. For those familiar with Docker images and ECR, this may not be a difficult task. But what about someone who is starting from square one and needs some assistance? Or maybe I want Q to bootstrap the process for me to save time? Can Q help me accomplish this?
In the following sections I will showcase how Amazon Q Developer helped me create an ECR repository, push and pull containers to and from the repo, explained repository policy configurations, demonstrated image scanning and tag mutability, and provided examples of cross-account image sharing.
Setup
For this exercise I used Visual Studio Code as the IDE with the Amazon Q Developer extension . All prompts were entered into Amazon Q Chat.
Building the Image
I already had a basic Dockerfile that creates a container image for a simple Hello World application. I also built the image using commands suggested by Amazon Q Developer. For reference, below is Dockerfile I used, the python file called hello-qbits.py that serves as the app, and the docker build commands suggested by Amazon Q Developer when I asked "how do I build my docker image?"
Dockerfile
dockerfile
# Use Python as the base imageFROM alpine:latestRUN apk add --no-cache python3 py3-pip xz# Set the working directory in the containerWORKDIR /app# Copy your Python script to the containerCOPY hello-qbits.py .# Command to run when the container startsCMD ["python", "hello-qbits.py"]
hello-qbits.py
Python
print("Hello, and welcome to another installment of Q-bits!")
docker-build-command.md
plaintext
# Docker Build CommandTo build a Docker image and tag it as latest:```bashdocker build -t your-image-name:latest /path/to/dockerfile/directory```## Example for hello-qbitsBased on your repository, to build and tag the hello-qbits image:```bashdocker build -t hello-qbits:latest .```**Note**: The `.` at the end specifies the current directory as the build context, assuming your Dockerfile is in the current directory. If your Dockerfile is in a different location, replace `.` with the path to the directory containing your Dockerfile.
Creating an ECR Repo and Pushing an Image
So what is the process to get this image into ECR? Let's ask Q for the process beginning with the following prompt.
Prompt
plaintext
I am a new developer and I have created a Docker image.What do I need in place in order to get my docker image into ECR?Explain this to me in steps, keeping it as brief as possible.Format your response in markdown and write it to a file.
As you can see in the prompt, I also asked Q to format the response in markdown format and write it to a file. The reason for this is that I am using Visual Studio Code. VSCode has a built in feature that allows you to preview a file. If the file is in markdown format it is a clean way to view the file as if you were reading a REAME.md file in GitHub. That said, after I ran my prompt, Q responded with the following markdown file that includes all of the prerequisites as well as the steps with required aws cli commands and docker push commands.
ecr-guide.md
text
# Getting Your Docker Image into ECR: Step-by-Step Guide## Prerequisites1. AWS account2. AWS CLI installed and configured3. Docker installed locally4. A Docker image built locally## Steps### 1. Create an ECR Repository```bashaws ecr create-repository --repository-name qbits/my-repository```### 2. Authenticate Docker to ECR```bashaws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 123456789012.dkr.ecr.us-east-1.amazonaws.com```### 3. Tag Your Docker Image```bashdocker tag hello-qbits:latest 123456789012.dkr.ecr.us-east-1.amazonaws.com/qbits/my-repository:latest```### 4. Push the Image to ECR```bashdocker push 123456789012.dkr.ecr.us-east-1.amazonaws.com/qbits/my-repository:latest```### 5. Verify Your Image```bashaws ecr describe-images --repository-name qbits/my-repository```## Tips- Authentication tokens expire after 12 hours- Consider setting up a lifecycle policy to manage old images- Use meaningful tags for versioning (avoid using just 'latest')- For automation, you can include these steps in a CI/CD pipeline
ECR Repo Policy Configurations
Next I wanted to know more about ECR repository policy configurations and what they are for. Once again I ask Q for assistance by explaining what they are?
Prompt
plaintext
What are ECR repo policy configurations?
When Q responds to this, it remembers that I like my output in markdown format and creates a file called ecr-policy-configurations.md. It includes an explanation of policy configuration along with common policy examples. Notice this has already has an example of Cross Account Sharing.
ecr-policy-configurations.md
plaintext
# ECR Repository Policy ConfigurationsECR repository policies are JSON documents that control access to your repositories. Here are the key policy configurations:## Core Policy Elements- **Version**: Specifies the policy language version (typically "2012-10-17")- **Statement**: Contains individual permission statements- **Effect**: "Allow" or "Deny"- **Principal**: Who the policy applies to (AWS accounts, IAM users, roles)- **Action**: ECR actions to allow/deny (e.g., `ecr:PutImage`, `ecr:GetDownloadUrlForLayer`)- **Resource**: The repository ARN the policy applies to- **Condition**: Optional conditions for when the policy applies## Common Policy Types### Cross-Account Access```json{"Version": "2012-10-17","Statement": [{"Sid": "AllowCrossAccountPull","Effect": "Allow","Principal": {"AWS": "arn:aws:iam::123456789012:root"},"Action": ["ecr:BatchGetImage","ecr:GetDownloadUrlForLayer"]}]}```### Public Access```json{"Version": "2012-10-17","Statement": [{"Sid": "AllowPublicPull","Effect": "Allow","Principal": "*","Action": ["ecr:BatchGetImage","ecr:GetDownloadUrlForLayer"]}]}```## Other Repository Settings- **Image Tag Mutability**: MUTABLE or IMMUTABLE (prevents overwriting existing tags)- **Image Scanning**: Enable/disable scan on push for vulnerability detection- **Encryption**: KMS encryption settings for repository contents- **Lifecycle Policies**: Rules for automatically cleaning up unused images## CLI Commands for Policy Management```bash# Set a repository policyaws ecr set-repository-policy \--repository-name repo-name \--policy-text file://policy.json# Get current repository policyaws ecr get-repository-policy \--repository-name repo-name# Delete repository policyaws ecr delete-repository-policy \--repository-name repo-name```
Image Scanning and Tag Mutability
Now that we know about ECR repository policy configuration, I wanted to know a bit more about image scanning and tag mutability. For this I asked Amazon Q Developer to go into more detail.
Prompt
plaintext
Can you go into detail demonstrating image scanning and tag mutability?
After prompting, Amazon Q Developer once again remembers my preference for markdown formatted files and modifies the ecr-policy-configurations.md file to include more information on image scanning and tag mutability. It also provides the necessary aws cli commands to enable scanning on push as well as manual scanning. Below is the resulting markdown file.
Updated ecr-policy-configurations.md
plaintext
# ECR Repository Policy ConfigurationsECR repository policies are JSON documents that control access to your repositories. Here are the key policy configurations:## Core Policy Elements- **Version**: Specifies the policy language version (typically "2012-10-17")- **Statement**: Contains individual permission statements- **Effect**: "Allow" or "Deny"- **Principal**: Who the policy applies to (AWS accounts, IAM users, roles)- **Action**: ECR actions to allow/deny (e.g., `ecr:PutImage`, `ecr:GetDownloadUrlForLayer`)- **Resource**: The repository ARN the policy applies to- **Condition**: Optional conditions for when the policy applies## Common Policy Types### Cross-Account Access```json{"Version": "2012-10-17","Statement": [{"Sid": "AllowCrossAccountPull","Effect": "Allow","Principal": {"AWS": "arn:aws:iam::123456789012:root"},"Action": ["ecr:BatchGetImage","ecr:GetDownloadUrlForLayer"]}]}```### Public Access```json{"Version": "2012-10-17","Statement": [{"Sid": "AllowPublicPull","Effect": "Allow","Principal": "*","Action": ["ecr:BatchGetImage","ecr:GetDownloadUrlForLayer"]}]}```## Image Tag MutabilityImage tag mutability determines whether you can overwrite an existing image tag.### Setting Tag MutabilityWhen creating a repository:```bashaws ecr create-repository \--repository-name my-repo \--image-tag-mutability IMMUTABLE```For an existing repository:```bashaws ecr put-image-tag-mutability \--repository-name my-repo \--image-tag-mutability IMMUTABLE```### MUTABLE vs IMMUTABLE#### MUTABLE (Default)- Allows overwriting existing tags- Example workflow:```bash# Day 1: Push version 1.0docker push 123456789012.dkr.ecr.us-east-1.amazonaws.com/my-repo:v1# Day 2: Push updated version with same tagdocker push 123456789012.dkr.ecr.us-east-1.amazonaws.com/my-repo:v1 # Overwrites previous image```#### IMMUTABLE- Prevents overwriting existing tags- Forces unique tagging for each image version- Example workflow:```bash# Day 1: Push version 1.0docker push 123456789012.dkr.ecr.us-east-1.amazonaws.com/my-repo:v1.0# Day 2: Must use a new tag for updatesdocker push 123456789012.dkr.ecr.us-east-1.amazonaws.com/my-repo:v1.1```### Best Practices for Immutable Tags- Use semantic versioning (v1.0.1, v1.0.2)- Use git commit hashes (git-abc123)- Use build numbers (build-42)- Use timestamps (2023-04-15-1030)- Implement lifecycle policies to clean up old images## Image ScanningECR can automatically scan your container images for software vulnerabilities.### Configuring Image ScanningWhen creating a repository:```bashaws ecr create-repository \--repository-name my-repo \--image-scanning-configuration scanOnPush=true```For an existing repository:```bashaws ecr put-image-scanning-configuration \--repository-name my-repo \--image-scanning-configuration scanOnPush=true```### Scan Types#### Scan on Push- Automatically scans images when they're pushed to the repository- Immediate feedback on vulnerabilities- Example workflow:```bash# Push image (with scan-on-push enabled)docker push 123456789012.dkr.ecr.us-east-1.amazonaws.com/my-repo:latest# Image is automatically scanned after push```#### Manual Scanning- Trigger scans on-demand for any image- Useful for repositories without scan-on-push enabled- Example:```bashaws ecr start-image-scan \--repository-name my-repo \--image-id imageTag=latest```### Viewing Scan Results```bashaws ecr describe-image-scan-findings \--repository-name my-repo \--image-id imageTag=latest```Sample output:```json{"imageScanFindings": {"findings": [{"name": "CVE-2022-12345","description": "Buffer overflow in libexample","uri": "https://nvd.nist.gov/vuln/detail/CVE-2022-12345","severity": "HIGH","attributes": [{"key": "package_name","value": "libexample"},{"key": "package_version","value": "1.2.3"}]}],"findingSeverityCounts": {"HIGH": 1,"MEDIUM": 3,"LOW": 7}},"imageScanStatus": {"status": "COMPLETE","description": "Scan completed successfully"}}```### Enhanced ScanningECR also offers enhanced scanning powered by Amazon Inspector:```bash# Enable enhanced scanning at registry levelaws ecr put-registry-scanning-configuration \--scan-type ENHANCED \--rules '[{"repositoryFilters":[{"filter":"*","filterType":"WILDCARD"}],"scanFrequency":"CONTINUOUS_SCAN"}]'```Benefits:- Continuous scanning for new vulnerabilities- More comprehensive vulnerability database- Runtime vulnerability monitoring- Integration with Security Hub## Other Repository Settings- **Encryption**: KMS encryption settings for repository contents- **Lifecycle Policies**: Rules for automatically cleaning up unused images## CLI Commands for Policy Management```bash# Set a repository policyaws ecr set-repository-policy \--repository-name repo-name \--policy-text file://policy.json# Get current repository policyaws ecr get-repository-policy \--repository-name repo-name# Delete repository policyaws ecr delete-repository-policy \--repository-name repo-name```
Pulling an Image from ECR
The final piece of the puzzle would be to be able to pull the image from the ECR repo. Once again, I provide Amazon Q Developer a prompt to provide me the aws cli commands to pull the image from the ECR repo and add the output to the ecr-guide.md file I created earlier.
Prompt
plaintext
Add a new section to the ECR guide with instructions for pulling images from ECR
Updated ecr-guide.md
plaintext
# Getting Your Docker Image into ECR: Step-by-Step Guide## Prerequisites1. AWS account2. AWS CLI installed and configured3. Docker installed locally4. A Docker image built locally## Steps### 1. Create an ECR Repository```bashaws ecr create-repository --repository-name qbits/my-repository```### 2. Authenticate Docker to ECR```bashaws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 123456789012.dkr.ecr.us-east-1.amazonaws.com```### 3. Tag Your Docker Image```bashdocker tag hello-qbits:latest 123456789012.dkr.ecr.us-east-1.amazonaws.com/qbits/my-repository:latest```### 4. Push the Image to ECR```bashdocker push 123456789012.dkr.ecr.us-east-1.amazonaws.com/qbits/my-repository:latest```### 5. Verify Your Image```bashaws ecr describe-images --repository-name qbits/my-repository```## Pulling Images from ECRTo pull your image from the ECR repository:```bashdocker pull 123456789012.dkr.ecr.us-east-1.amazonaws.com/qbits/my-repository:latest```Using environment variables for a cleaner approach:```bash# Set environment variablesexport AWS_ACCOUNT_ID=123456789012export AWS_REGION=us-east-1export ECR_REPO=qbits/my-repository# Login to ECR (required before pulling)aws ecr get-login-password --region $AWS_REGION | docker login --username AWS --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com# Pull the imagedocker pull $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/$ECR_REPO:latest```## Tips- Authentication tokens expire after 12 hours- Consider setting up a lifecycle policy to manage old images- Use meaningful tags for versioning (avoid using just 'latest')- For automation, you can include these steps in a CI/CD pipeline
Conclusion
As demonstrated in this post, Amazon Q Developer was able to help me create an ECR repository, push and pull containers to and from the repo, explained repository policy configurations, demonstrated image scanning and tag mutability, and provided examples of cross-account image sharing.
As a bonus, Amazon Q Developer was able to understand my preference for outputting results in a clean and readable markdown format. Furthermore, it was able to document every single command so I could understand what each command does and provided tips along the way.
Amazon Q Developer is a powerful AI assistant that can help speed up your development process whether you are a beginner that wants detailed step by step instructions with examples, or you are a seasoned developer that wants to quickly bootstrap a project.
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article