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?"
Practice
Solution
Step 1: Understand Docker image reuse
Saving a Docker image as an artifact allows you to reuse it later without rebuilding, saving time and resources.Step 2: Identify Jenkins artifact purpose
Jenkins artifacts store files from builds for later use or sharing, perfect for Docker images.Final Answer:
To reuse the image later without rebuilding it -> Option DQuick Check:
Docker image reuse = To reuse the image later without rebuilding it [OK]
- Thinking artifacts delete images
- Confusing image with container
- Assuming artifacts change OS compatibility
myimage.tar?Solution
Step 1: Recall Jenkins archiveArtifacts syntax
The correct syntax uses the parameter name 'artifacts' to specify files to archive.Step 2: Match correct parameter and spelling
archiveArtifacts artifacts: 'myimage.tar' uses 'archiveArtifacts artifacts: 'myimage.tar'', which is correct syntax.Final Answer:
archiveArtifacts artifacts: 'myimage.tar' -> Option AQuick Check:
Correct parameter is 'artifacts' = archiveArtifacts artifacts: 'myimage.tar' [OK]
- Using 'file' instead of 'artifacts'
- Misspelling 'archiveArtifacts'
- Using singular 'archiveArtifact'
stage('Save Image') {
steps {
sh 'docker save myapp:latest -o myapp.tar'
archiveArtifacts artifacts: 'myapp.tar'
}
}What will Jenkins do after this stage completes?
Solution
Step 1: Analyze docker save command
The command 'docker save myapp:latest -o myapp.tar' saves the Docker image to a file named myapp.tar.Step 2: Understand archiveArtifacts usage
The 'archiveArtifacts' step archives the saved file as a Jenkins build artifact for later use.Final Answer:
Save the Docker image as a file and archive it as a build artifact -> Option BQuick Check:
docker save + archiveArtifacts = Save the Docker image as a file and archive it as a build artifact [OK]
- Confusing save with build
- Thinking it runs containers
- Assuming image is deleted
sh 'docker save myapp:latest -o myapp.tar' archiveArtifacts file: 'myapp.tar'
Why does Jenkins fail to archive the Docker image file?
Solution
Step 1: Check archiveArtifacts parameter
The correct parameter to specify files is 'artifacts', not 'file'. Using 'file' causes failure.Step 2: Confirm docker save output
'docker save' correctly creates the file 'myapp.tar', so the problem is not with the file creation.Final Answer:
Because the parameter name should be 'artifacts', not 'file' -> Option CQuick Check:
Parameter must be 'artifacts' = Because the parameter name should be 'artifacts', not 'file' [OK]
- Using 'file' instead of 'artifacts'
- Assuming docker save fails silently
- Thinking archiveArtifacts rejects .tar files
appimage.tar from a previous Jenkins build artifact in a new pipeline. Which sequence correctly loads and runs the image?Solution
Step 1: Understand docker load syntax
To load an image from a file, use 'docker load -i filename'. This imports the image into Docker.Step 2: Run the loaded image
After loading, run the image by its tag, e.g., 'docker run appimage:latest'.Final Answer:
sh 'docker load -i appimage.tar' sh 'docker run appimage:latest' -> Option AQuick Check:
Load with '-i', then run by tag = sh 'docker load -i appimage.tar' sh 'docker run appimage:latest' [OK]
- Using 'docker run' with file instead of image tag
- Confusing 'docker save' with 'docker load'
- Omitting '-i' option in docker load
Ready to prove it?
Pass the test and this pattern moves unknown
