Ravindra BagaleCourses & study guides मराठी Track your progress

Guides

Jenkins मधून Docker Image Build आणि Push कसे करावे

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.

Quick answer

Hold this vertical picture:

  1. Dockerfile.
  2. Jenkinsfile.
  3. Credentials login.
  4. push.
  5. digest; latest-only टाळा.

Language: English · मराठी · हिंदी

CheckoutDockerfile in repo docker buildLocal image Tag BUILD_NUMBERUnique name docker loginRegistry auth docker pushRemote digest
Jenkins Docker push

Jenkins images का

प्रत्येक commit एकच Dockerfile.

Why: Laptop builds 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:

demo-app:local

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:

demo-app:local

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:

42
local

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:

42 latest local

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:

1 /tmp/jdock/Jenkinsfile

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:

120 /tmp/jdock/cred.txt

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:

sha256:

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:

demo-app:local

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:

BUILD_FAILED_AS_EXPECTED

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.

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.