0
0
Jenkinsdevops~10 mins

Docker image as artifact in Jenkins - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Docker image as artifact
Start Jenkins Job
Checkout Code
Build Docker Image
Tag Docker Image
Push Image to Registry
Use Image as Artifact in Next Steps
End
This flow shows how Jenkins builds a Docker image, tags it, pushes it to a registry, and uses it as an artifact for later steps.
Execution Sample
Jenkins
pipeline {
  agent any
  stages {
    stage('Build') {
      steps {
        script {
          sh 'docker build -t myapp:${BUILD_NUMBER} .'
          sh 'docker push myapp:${BUILD_NUMBER}'
        }
      }
    }
  }
}
This Jenkins pipeline builds a Docker image tagged with the build number and pushes it to a Docker registry.
Process Table
StepActionCommand RunResultNext Step
1Start Jenkins JobN/AJob triggeredCheckout Code
2Checkout Codegit clone repoCode available locallyBuild Docker Image
3Build Docker Imagedocker build -t myapp:1 .Image 'myapp:1' createdTag Docker Image
4Tag Docker ImageImplicit in build commandImage tagged as 'myapp:1'Push Image to Registry
5Push Image to Registrydocker push myapp:1Image pushed to registryUse Image as Artifact
6Use Image as ArtifactN/AImage ready for deployment or next stepsEnd
7EndN/APipeline finished successfullyN/A
💡 Pipeline ends after image is pushed and ready as artifact for next steps.
Status Tracker
VariableStartAfter Step 3After Step 5Final
BUILD_NUMBER1111
Docker Image Tagnonemyapp:1myapp:1myapp:1
Image in Registrynonoyesyes
Key Moments - 3 Insights
Why do we tag the Docker image with ${BUILD_NUMBER}?
Tagging with ${BUILD_NUMBER} creates a unique image version for each build, as shown in execution_table step 3 and 4, preventing overwriting previous images.
What happens if the docker push fails?
If docker push fails (step 5), the image is not stored in the registry and cannot be used as an artifact, so the pipeline usually stops or retries.
Is the image available locally after the build?
Yes, after step 3, the image is built and stored locally on the Jenkins agent before pushing to the registry.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the Docker image tag after step 3?
Amyapp:latest
Blatest
Cmyapp:1
Dnone
💡 Hint
Check the 'Result' column in step 3 of the execution_table.
At which step is the Docker image pushed to the registry?
AStep 5
BStep 3
CStep 4
DStep 6
💡 Hint
Look for the 'Push Image to Registry' action in the execution_table.
If BUILD_NUMBER was '2', how would the Docker image tag change after step 3?
Amyapp:1
Bmyapp:2
Cmyapp:latest
Dmyapp
💡 Hint
Refer to variable_tracker for BUILD_NUMBER and Docker Image Tag values.
Concept Snapshot
Jenkins builds Docker images tagged uniquely (e.g., with BUILD_NUMBER).
The image is pushed to a Docker registry.
This image acts as an artifact for deployment or later pipeline stages.
Tagging prevents overwriting previous images.
Push must succeed for artifact availability.
Full Transcript
This visual execution shows how Jenkins builds a Docker image as an artifact. The pipeline starts by checking out code, then builds a Docker image tagged with the build number. The image is stored locally and then pushed to a Docker registry. Once pushed, the image is available as an artifact for deployment or further pipeline steps. Tagging with the build number ensures each image is unique. If pushing fails, the image is not available remotely. This process helps manage Docker images reliably in CI/CD pipelines.