Docker image as artifact in Jenkins - Time & Space 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?
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.
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.
The time to build grows as the number of layers and size of each layer increase.
| Input Size (layers) | Approx. Operations (build steps) |
|---|---|
| 10 | 10 layer builds + 1 archive |
| 100 | 100 layer builds + 1 archive |
| 1000 | 1000 layer builds + 1 archive |
Pattern observation: The build time grows roughly linearly with the number of layers.
Time Complexity: O(n)
This means the build time increases in direct proportion to the number of Docker image layers.
[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.
Understanding how build time grows with image complexity helps you design efficient pipelines and explain trade-offs clearly.
"What if we cache Docker layers between builds? How would the time complexity change?"