Guides
Jenkins से Docker Image Build और Push कैसे करें
By Ravindra Bagale, DevOps trainer · LinkedIn · Published: · Last updated:
Jenkins BUILD_NUMBER से tag कर login+push करता है और digest दर्ज करता है।
Friends, Jenkins builds the image, the registry stores it, Kubernetes only pulls. Chala mitrano — tag with the build number so you can roll back.
मित्रांनो, Jenkins image बनवते, registry ठेवते, Kubernetes फक्त pull करते. Chala mitrano — build number tag ठेवा.
मित्रों, Jenkins image बनाता है, registry रखती है, Kubernetes सिर्फ pull करता है। Chala mitrano — build number tag रखें।
Quick answer
Hold this vertical picture:
- Dockerfile.
- Jenkinsfile.
- Credentials login.
- push.
- digest; latest-only से बचें.
Language: English · मराठी · हिंदी
Jenkins Docker push
Jenkins images क्यों
हर commit एक Dockerfile.
Why: Laptop drift.
What: build, tag, login, push.
How: Docker वाला agent.
Example 1: Minimal Dockerfile
Type this:
mkdir -p /tmp/jdock && printf '%s\n' 'FROM public.ecr.aws/docker/library/alpine:3.20' 'CMD ["echo","hello-from-image"]' > /tmp/jdock/Dockerfile && cat /tmp/jdock/Dockerfile
You will see:
FROM public.ecr.aws/docker/library/alpine:3.20
What it means: Known base — avoid random unverified images.
Example 2: Build like Jenkins will
Type this:
docker build -t demo-app:local /tmp/jdock && docker images demo-app:local --format '{{.Repository}}:{{.Tag}}'
You will see:
What it means: Local failure means the agent will fail the same way.
Example 9: Agent needs Docker
Type this:
printf '%s\n' 'Jenkins agent must run docker: socket mount or DinD.'
You will see:
Jenkins agent must run docker: socket mount or DinD.
What it means: Fix the agent before blaming the Dockerfile.
Concept 1: Build और tag
Numbered tags.
Why: Rollback को exact tag.
What: BUILD_NUMBER.
How: build && tag.
Example 2: Build like Jenkins will
Type this:
docker build -t demo-app:local /tmp/jdock && docker images demo-app:local --format '{{.Repository}}:{{.Tag}}'
You will see:
What it means: Local failure means the agent will fail the same way.
Example 3: Tag with build number
Type this:
docker tag demo-app:local demo-app:42 && docker images demo-app --format '{{.Tag}}' | sort
You will see:
What it means: BUILD_NUMBER becomes the rollback tag.
Example 8: latest plus numbered
Type this:
docker tag demo-app:local demo-app:latest && docker images demo-app --format '{{.Tag}}' | sort | tr '\n' ' '
You will see:
What it means: Keep numbered tags; latest is only a pointer.
Concept 2: Login और push
Jenkins credentials.
Why: Password log = incident.
What: password-stdin.
How: login → push.
Example 4: Jenkinsfile sketch
Type this:
printf '%s\n' 'pipeline { agent any; stages { stage("Build") { steps { sh "docker build -t demo-app:${BUILD_NUMBER} ." } } } }' > /tmp/jdock/Jenkinsfile && wc -l /tmp/jdock/Jenkinsfile
You will see:
What it means: Declarative: build then tag for your registry.
Example 5: Login via stdin
Type this:
printf '%s\n' 'echo "$REGISTRY_PASSWORD" | docker login registry.example.internal -u ci-bot --password-stdin' > /tmp/jdock/login.sh && chmod 750 /tmp/jdock/login.sh && head -1 /tmp/jdock/login.sh
You will see:
echo "$REGISTRY_PASSWORD" | docker login registry.example.internal -u ci-bot --password-stdin
What it means: Never echo the password into the console log.
Example 6: Push command
Type this:
printf '%s\n' 'docker push registry.example.internal/demo-app:42'
You will see:
docker push registry.example.internal/demo-app:42
What it means: Second push of same layers is fast.
Example 10: Credentials binding size
Type this:
printf '%s\n' 'withCredentials([usernamePassword(credentialsId: "reg-bot", usernameVariable: "U", passwordVariable: "P")]) { sh "echo P | docker login" }' > /tmp/jdock/cred.txt && wc -c /tmp/jdock/cred.txt
You will see:
What it means: Binding injects env vars for one step only.
Concept 3: Digest
Auditors के लिए id.
Why: Digest के बिना proof नहीं.
What: inspect.
How: inspect; set -e.
Example 7: Inspect image id
Type this:
docker image inspect demo-app:local --format '{{.Id}}' | cut -c1-19
You will see:
What it means: Infosys auditors ask for the digest after release.
Example 11: Digests format
Type this:
docker images --digests demo-app:local --format '{{.Repository}}:{{.Tag}}' | head -1
You will see:
What it means: On a real registry, imagetools inspect shows the remote digest.
Example 12: Fail fast
Type this:
printf '%s\n' 'set -e' 'docker build -t x /tmp/missing || echo BUILD_FAILED_AS_EXPECTED' > /tmp/jdock/fail.sh && bash /tmp/jdock/fail.sh
You will see:
What it means: set -e stops the pipeline on non-zero docker build.
Jenkins Docker card
1 Dockerfile 2 build 3 tag 4 login 5 push 6 digest
Suppose: docker.sock
Agent fix.
Suppose: access denied
Bot push rights.
Ravindra Bagale's Tip
At TCS and Cognizant, ECR or a private registry is standard. Reviewers ask how you avoid latest-only tags.
TCS आणि Cognizant मध्ये ECR किंवा private registry मानक. Reviewers latest-only tags कसे टाळता ते विचारतात.
TCS और Cognizant में ECR या private registry मानक है। Reviewers latest-only tags कैसे टालते हैं पूछते हैं।
Check yourself (quiz)
Q1. BUILD_NUMBER?
rollback tag
Q2. Password?
credentials
Q3. latest?
pointer
Q4. Agent?
Docker
Q5. Prove?
digest
Q6. Fail?
set -e
Reading tracking
Mark this guide when you finish. Your browser remembers it on this device.
build → tag → login → push → digest.
Did you understand? Let us try it once more.
Frequently asked questions
Why not only latest?
You cannot pin a rollback.
Where do passwords live?
Jenkins Credentials.
ECR?
Same flow with aws ecr get-login-password.
Agent error?
Install Docker or mount docker.sock.
DinD?
Docker-in-Docker when socket mount is blocked.
Prove push?
Inspect digest from the registry.