80.6 Project 72: Build and push a Docker image to Amazon ECR
Friends, images must land in a registry before Kubernetes. Today we build an image on a docker agent, log in to Amazon ECR, tag with the git SHA, and push.
मित्रांनो, Kubernetes आधी image registry मध्ये हवी. आज docker agent वर build, Amazon ECR login, git SHA tag, आणि push.
दोस्तों, Kubernetes से पहले image registry में चाहिए. आज docker agent पर build, Amazon ECR login, git SHA tag, और push.
What you will build in this project
- Build the image on a docker-labelled agent.
- Authenticate to ECR without long-lived keys when possible.
- Tag with git SHA.
- Push and verify the digest.
1. Start from a real company story
At Amazon Pay India partner team in Bengaluru, someone retagged latest over a known-good image and checkout pods pulled a broken layer.
Rough cost: about ₹22 lakh in failed payments that hour.
Kabir banned floating latest in prod and required SHA tags from Jenkins.
Today we push to ECR the safe way.
Ravindra Bagale's Tip
Students leave AWS access keys in Jenkins plain text. Prefer IRSA or instance roles and short tokens. Keep that habit.
Students AWS keys plain Jenkins मध्ये ठेवतात. IRSA किंवा instance roles वापरा. सवय ठेवा.
Students AWS keys plain Jenkins में रखते हैं. IRSA या instance roles इस्तेमाल करें. आदत रखिए.
2. Start and prepare
Step 1: Why ECR
Why: Managed registry next to EKS. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.
What: echo why. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.
Example 1: a1
Type this:
echo 'ECR holds images close to EKS with IAM controls'
You will see:
ECR holds images close to EKS with IAM controls
What it means: Why.
Example 2: a2
Type this:
mkdir -p /tmp/jen72/app && echo 'Today: Dockerfile, ecr login, SHA tag, push'
You will see:
Today: Dockerfile, ecr login, SHA tag, push
What it means: Scope.
Example 3: a3
Type this:
echo 'Never deploy :latest to production'
You will see:
Never deploy :latest to production
What it means: Rule.
Step 2: Dockerfile
Why: Tiny web image. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.
What: printf Dockerfile. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.
Example 4: b1
Type this:
printf '%s\n' 'FROM public.ecr.aws/docker/library/nginx:1.27-alpine' 'COPY index.html /usr/share/nginx/html/index.html' > /tmp/jen72/app/Dockerfile && cat /tmp/jen72/app/Dockerfile
You will see:
FROM public.ecr.aws/docker/library/nginx:1.27-alpine
What it means: Dockerfile.
Example 5: b2
Type this:
printf '%s\n' 'shop cart from jenkins' > /tmp/jen72/app/index.html && cat /tmp/jen72/app/index.html
You will see:
shop cart from jenkins
What it means: Page.
Example 6: b3
Type this:
echo 'Pin base tags; avoid FROM nginx:latest'
You will see:
Pin base tags; avoid FROM nginx:latest
What it means: Pin.
Step 3: ECR repo
Why: Create once. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.
What: aws tip. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.
Example 7: c1
Type this:
echo 'aws ecr create-repository --repository-name shop/cart --region ap-south-1'
You will see:
aws ecr create-repository --repository-name shop/cart --region ap-south-1
What it means: Create.
Example 8: c2
Type this:
printf '%s\n' 'ACCOUNT=123456789012' 'REGION=ap-south-1' 'REPO=shop/cart' > /tmp/jen72/ECR.env && cat /tmp/jen72/ECR.env
You will see:
REPO=shop/cart
What it means: Env.
Example 9: c3
Type this:
echo 'Image URI = ACCOUNT.dkr.ecr.REGION.amazonaws.com/REPO'
You will see:
Image URI = ACCOUNT.dkr.ecr.REGION.amazonaws.com/REPO
What it means: URI shape.
3. Build the main pieces
Step 4: Login stage
Why: Password to docker. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.
What: ecr get-login-password. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.
Example 10: d1
Type this:
printf '%s\n' ' stage(\'ECR Login\') {' ' steps {' ' sh \'aws ecr get-login-password --region $REGION | docker login --username AWS --password-stdin $ACCOUNT.dkr.ecr.$REGION.amazonaws.com\'' ' }' ' }' > /tmp/jen72/LOGIN.txt && cat /tmp/jen72/LOGIN.txt
You will see:
stage('ECR Login') {
What it means: Login.
Example 11: d2
Type this:
echo 'Agent needs AWS credentials via instance role or short-lived keys'
You will see:
Agent needs AWS credentials via instance role or short-lived keys
What it means: IAM.
Example 12: d3
Type this:
echo 'docker login writes to the agent docker config — cleanWs later'
You will see:
docker login writes to the agent docker config — cleanWs later
What it means: Cleanup tip.
Step 5: Build and tag
Why: SHA from git. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.
What: build stage. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.
Example 13: e1
Type this:
printf '%s\n' ' stage(\'Build\') {' ' steps {' ' script { env.GIT_SHA = sh(returnStdout: true, script: \'git rev-parse --short HEAD\').trim() }' ' sh \'docker build -t $ACCOUNT.dkr.ecr.$REGION.amazonaws.com/$REPO:$GIT_SHA .\'' ' }' ' }' > /tmp/jen72/BUILD.txt && head -6 /tmp/jen72/BUILD.txt
You will see:
stage('Build') {
What it means: Build.
Example 14: e2
Type this:
echo 'Short SHA keeps tags readable; full SHA also fine'
You will see:
Short SHA keeps tags readable; full SHA also fine
What it means: SHA.
Example 15: e3
Type this:
echo 'Build context is the repo root with the Dockerfile'
You will see:
Build context is the repo root with the Dockerfile
What it means: Context.
Step 6: Push stage
Why: Upload layers. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.
What: push. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.
Example 16: f1
Type this:
printf '%s\n' ' stage(\'Push\') {' ' steps {' ' sh \'docker push $ACCOUNT.dkr.ecr.$REGION.amazonaws.com/$REPO:$GIT_SHA\'' ' }' ' }' > /tmp/jen72/PUSH.txt && cat /tmp/jen72/PUSH.txt
You will see:
stage('Push') {
What it means: Push.
Example 17: f2
Type this:
echo 'Copy the digest line from the push output into the build description'
You will see:
Copy the digest line from the push output into the build description
What it means: Digest.
Example 18: f3
Type this:
echo 'Optional: also tag and push :build-$BUILD_NUMBER for humans'
You will see:
Optional: also tag and push :build-$BUILD_NUMBER for humans
What it means: Human tag.
4. Automate and verify
Step 7: Agent label
Why: docker capable. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.
What: label tip. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.
Example 19: g1
Type this:
printf '%s\n' 'agent { label \'docker\' }' 'Agent user must be in the docker group' > /tmp/jen72/AGENT.txt && cat /tmp/jen72/AGENT.txt
You will see:
agent { label 'docker' }
What it means: Agent.
Example 20: g2
Type this:
echo 'Prefer DinD carefully; rootless or mounted docker.sock with review'
You will see:
Prefer DinD carefully; rootless or mounted docker.sock with review
What it means: DinD tip.
Example 21: g3
Type this:
echo 'Same agent role from Ansible project 66 works here'
You will see:
Same agent role from Ansible project 66 works here
What it means: Reuse.
Step 8: Verify
Why: Describe image. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.
What: aws cli. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.
Example 22: h1
Type this:
echo 'aws ecr describe-images --repository-name shop/cart --image-ids imageTag=$GIT_SHA'
You will see:
aws ecr describe-images --repository-name shop/cart --image-ids imageTag=$GIT_SHA
What it means: Describe.
Example 23: h2
Type this:
echo 'docker pull URI:SHA && docker image inspect URI:SHA --format '{{.Id}}''
You will see:
docker pull URI:SHA && docker image inspect URI:SHA --format '{{.Id}}'
What it means: Pull proof.
Example 24: h3
Type this:
printf '%s\n' 'Acceptance: describe-images shows the SHA tag' > /tmp/jen72/ACCEPT.txt && cat /tmp/jen72/ACCEPT.txt
You will see:
Acceptance: describe-images shows the SHA tag
What it means: Gate.
Step 9: Lifecycle tip
Why: Expire untagged. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.
What: policy note. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.
Example 25: i1
Type this:
printf '%s\n' 'ECR lifecycle: expire untagged after 14 days' 'Keep last 20 prod SHA tags' > /tmp/jen72/LIFECYCLE.txt && cat /tmp/jen72/LIFECYCLE.txt
You will see:
ECR lifecycle: expire untagged after 14 days
What it means: Lifecycle.
Example 26: i2
Type this:
echo 'Untagged layers appear after retags — clean them'
You will see:
Untagged layers appear after retags — clean them
What it means: Untagged.
Example 27: i3
Type this:
echo 'Scan on push can be enabled in ECR settings'
You will see:
Scan on push can be enabled in ECR settings
What it means: Scan tip.
5. Harden and accept
Step 10: Story Kabir
Why: latest overwrite. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.
What: story. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.
Example 28: j1
Type this:
printf '%s\n' 'At Amazon Pay India partner team in Bengaluru, latest was retagged over a good image and pods pulled a broken layer.' 'Kabir required SHA tags from Jenkins and banned latest in prod.' > /tmp/jen72/STORY.txt && wc -l /tmp/jen72/STORY.txt
You will see:
2 /tmp/jen72/STORY.txt
What it means: Story.
Example 29: j2
Type this:
echo 'Rough cost: about ₹22 lakh in failed payments that hour'
You will see:
Rough cost: about ₹22 lakh in failed payments that hour
What it means: Cost.
Example 30: j3
Type this:
echo 'Kabir: PASS'
You will see:
Kabir: PASS
What it means: Sign-off.
Step 11: Proof
Why: Files ready. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.
What: ls. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.
Example 31: k1
Type this:
ls /tmp/jen72 /tmp/jen72/app
You will see:
/tmp/jen72:\nACCEPT.txt\nAGENT.txt\nBUILD.txt\nECR.env\nLIFECYCLE.txt\nLOGIN.txt\nPUSH.txt\nSTORY.txt\napp\n\n/tmp/jen72/app:\nDockerfile\nindex.html
What it means: Tree.
Example 32: k2
Type this:
grep GIT_SHA /tmp/jen72/BUILD.txt | head -1
You will see:
script { env.GIT_SHA = sh(returnStdout: true, script: 'git rev-parse --short HEAD').trim() }
What it means: SHA line.
Example 33: k3
Type this:
echo 'Next: Deploy to EKS from Jenkins project 73'
You will see:
Next: Deploy to EKS from Jenkins project 73
What it means: Bridge.
Step 12: Acceptance
Why: SHA image in ECR. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.
What: final. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.
Example 34: z1
Type this:
echo 'Image pushed with git SHA tag'
You will see:
Image pushed with git SHA tag
What it means: Push.
Example 35: z2
Type this:
echo 'describe-images confirms tag'
You will see:
describe-images confirms tag
What it means: Verify.
Example 36: z3
Type this:
echo 'No latest used for prod deploy'
You will see:
No latest used for prod deploy
What it means: Policy.
Check yourself
Q1. Who fixed Amazon Pay latest tag?
Kabir
Q2. Registry?
ECR
Q3. Tag style?
git SHA
Q4. Avoid in prod?
latest
Q5. Login tool?
aws ecr
Q6. Agent label?
docker
Friends, you pushed a SHA-tagged image to ECR. Did you understand? Let us try it once more. Pull the digest back with docker pull and confirm it matches the push output.
मित्रांनो, तुम्ही SHA-tagged image ECR वर push केली. docker pull ने digest परत आणा आणि push output शी जुळते हे तपासा.
दोस्तों, आपने SHA-tagged image ECR पर push की. docker pull से digest वापस लाएं और push output से मिलान करें.
Lab: try at home
- Write Dockerfile.
- ECR login stage.
- Build tag push.
- Verify digest.
Recap
- SHA tags not latest.
- OIDC over long keys.
- Digest is the truth.
- docker agent label.
- Scan comes next chapters.