0
0
Jenkinsdevops~5 mins

Docker image as artifact in Jenkins - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you build software, you want to save the exact version of your app so you can use it later. Docker images let you package your app and its environment together. Storing these images as artifacts means you keep a safe copy to use in testing or production.
When you want to save a tested version of your app to deploy later without rebuilding.
When you need to share your app image between different teams or environments.
When you want to keep track of app versions for rollback if something goes wrong.
When you want to automate your build and deployment process using Jenkins pipelines.
When you want to reduce build time by reusing existing Docker images.
Config File - Jenkinsfile
Jenkinsfile
pipeline {
  agent any
  stages {
    stage('Build Docker Image') {
      steps {
        script {
          dockerImage = docker.build('my-app:1.0')
        }
      }
    }
    stage('Save Docker Image as Artifact') {
      steps {
        sh 'docker save my-app:1.0 -o my-app-1.0.tar'
        archiveArtifacts artifacts: 'my-app-1.0.tar', fingerprint: true
      }
    }
  }
}

This Jenkinsfile defines a pipeline with two stages:

  • Build Docker Image: Builds a Docker image named my-app:1.0.
  • Save Docker Image as Artifact: Saves the image to a tar file and archives it as a Jenkins artifact for later use.
Commands
Builds a Docker image named 'my-app' with tag '1.0' from the current directory's Dockerfile.
Terminal
docker build -t my-app:1.0 .
Expected OutputExpected
Sending build context to Docker daemon 2.048kB Step 1/3 : FROM alpine:3.14 ---> a24bb4013296 Step 2/3 : COPY app.sh /app.sh ---> Using cache ---> 5d41402abc4b Step 3/3 : CMD ["/app.sh"] ---> Running in 7b8f9c3e1a2b Removing intermediate container 7b8f9c3e1a2b Successfully built 5d41402abc4b Successfully tagged my-app:1.0
-t - Assigns a name and tag to the image
Saves the Docker image 'my-app:1.0' into a tar file named 'my-app-1.0.tar' to store or share.
Terminal
docker save my-app:1.0 -o my-app-1.0.tar
Expected OutputExpected
No output (command runs silently)
-o - Specifies the output file for the saved image
Lists the saved Docker image file with human-readable size to confirm it was created.
Terminal
ls -lh my-app-1.0.tar
Expected OutputExpected
-rw-r--r-- 1 user user 12M Jun 1 12:00 my-app-1.0.tar
-lh - Lists files with human-readable sizes
Key Concept

If you remember nothing else from this pattern, remember: saving your Docker image as an artifact lets you reuse and share the exact app version safely.

Common Mistakes
Not tagging the Docker image before saving it.
Without a tag, it's hard to identify or reuse the image version later.
Always use the '-t' flag with 'docker build' to name and tag your image clearly.
Forgetting to save the image to a file before archiving.
Jenkins cannot archive the image directly; it needs a file artifact.
Use 'docker save' to export the image to a tar file before archiving.
Not archiving the saved image file in Jenkins.
Without archiving, the image file won't be stored or accessible after the build.
Use 'archiveArtifacts' step in Jenkins to save the tar file as a build artifact.
Summary
Build a Docker image with a clear name and tag using 'docker build -t'.
Save the built image to a tar file using 'docker save' for storage or sharing.
Archive the saved tar file as a Jenkins artifact to keep it safe and accessible.