0
0
Jenkinsdevops~5 mins

Docker image as artifact in Jenkins - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Docker image as artifact
O(n)
Understanding Time Complexity

We want to understand how the time to build and save a Docker image changes as the image size or layers grow.

How does the process scale when the Docker image gets bigger or more complex?

Scenario Under Consideration

Analyze the time complexity of the following Jenkins pipeline snippet that builds and archives a Docker image.

pipeline {
  agent any
  stages {
    stage('Build') {
      steps {
        script {
          dockerImage = docker.build("my-app:${env.BUILD_NUMBER}")
          dockerImage.save("docker-image.tar")
        }
      }
    }
    stage('Archive') {
      steps {
        archiveArtifacts artifacts: "docker-image.tar", fingerprint: true
      }
    }
  }
}

This pipeline builds a Docker image and archives it as an artifact for later use.

Identify Repeating Operations

Look for repeated or costly steps in the pipeline.

  • Primary operation: Building the Docker image layers.
  • How many times: Each layer is processed once during build; archiving happens once per build.
How Execution Grows With Input

The time to build grows as the number of layers and size of each layer increase.

Input Size (layers)Approx. Operations (build steps)
1010 layer builds + 1 archive
100100 layer builds + 1 archive
10001000 layer builds + 1 archive

Pattern observation: The build time grows roughly linearly with the number of layers.

Final Time Complexity

Time Complexity: O(n)

This means the build time increases in direct proportion to the number of Docker image layers.

Common Mistake

[X] Wrong: "Archiving the Docker image takes as much time as building it."

[OK] Correct: Archiving is a single step that happens once after build, so its time cost is small compared to building multiple layers.

Interview Connect

Understanding how build time grows with image complexity helps you design efficient pipelines and explain trade-offs clearly.

Self-Check

"What if we cache Docker layers between builds? How would the time complexity change?"