Docker Pipeline plugin in Jenkins - Time & Space Complexity
We want to understand how the time taken by a Jenkins pipeline using the Docker Pipeline plugin changes as we add more steps or containers.
Specifically, how does the number of Docker operations affect the total execution time?
Analyze the time complexity of the following Jenkins pipeline snippet using the Docker Pipeline plugin.
pipeline {
agent any
stages {
stage('Build') {
steps {
script {
docker.image('maven:3.6.3-jdk-11').inside {
sh 'mvn clean install'
}
}
}
}
}
}
This pipeline runs a Maven build inside a Docker container using the Docker Pipeline plugin.
Look for repeated actions that take time.
- Primary operation: Starting and stopping the Docker container for the build.
- How many times: Once per pipeline run in this example, but could be multiple if repeated in loops or multiple stages.
As the number of Docker container runs increases, the total time grows roughly in direct proportion.
| Input Size (number of container runs) | Approx. Operations (container start/stop) |
|---|---|
| 1 | 1 container start + 1 container stop |
| 10 | 10 container starts + 10 container stops |
| 100 | 100 container starts + 100 container stops |
Pattern observation: Each additional container run adds a fixed amount of work, so time grows linearly.
Time Complexity: O(n)
This means the total time grows linearly with the number of Docker container runs in the pipeline.
[X] Wrong: "Starting a Docker container inside the pipeline is instant and does not affect time much."
[OK] Correct: Starting and stopping containers takes noticeable time, so more container runs add up and increase total pipeline time.
Understanding how pipeline steps scale with input helps you design efficient CI/CD workflows and explain your reasoning clearly in interviews.
"What if we reused a single Docker container for multiple build steps instead of starting a new one each time? How would the time complexity change?"
Practice
docker.build() method in Jenkins Docker Pipeline plugin?Solution
Step 1: Understand the role of
This method is used to create a Docker image from a Dockerfile in the workspace.docker.build()Step 2: Differentiate from other Docker Pipeline methods
Running containers usesdocker.image().inside { }, pushing usesdocker.withRegistry(), and removing images is not handled bydocker.build().Final Answer:
To build a Docker image from a Dockerfile -> Option CQuick Check:
docker.build()builds images [OK]
- Confusing build with push or run commands
- Thinking
docker.build()runs containers - Assuming it handles image removal
Solution
Step 1: Identify the correct method to run commands inside a container
Thedocker.image('myimage').inside { }syntax runs commands inside a container created from the image.Step 2: Check other options for syntax errors or misuse
docker.run()anddocker.container()are not valid Jenkins Pipeline methods;docker.build()builds images but does not run containers.Final Answer:
docker.image('myimage').inside { sh 'echo Hello' } -> Option AQuick Check:
Usedocker.image().inside { }to run commands [OK]
docker.image().inside { } to run commands inside containers [OK]- Using
docker.build()to run commands - Trying to use non-existent methods like
docker.run() - Confusing image build and container run syntax
sh command inside the container?
def img = docker.build('myapp:latest')
img.inside {
sh 'echo Hello from Docker'
}Solution
Step 1: Understand the build and run sequence
Thedocker.build('myapp:latest')creates the image, thenimg.inside { }runs commands inside a container from that image.Step 2: Analyze the
The shell commandshcommand inside the containerecho Hello from Dockerruns inside the container and outputs the string.Final Answer:
Hello from Docker -> Option BQuick Check:
Commands insideimg.inside { }run and output normally [OK]
img.inside { } run inside container [OK]- Assuming build only creates logs, not image
- Thinking the command runs outside the container
- Expecting an error if image was just built
docker.withRegistry('https://myregistry.com', 'my-credentials') {
def img = docker.build('myapp')
img.push()
}
img.inside {
sh 'echo Running inside container'
}Solution
Step 1: Check scope of
The variableimgvariableimgis defined inside thedocker.withRegistryblock, so it is not accessible outside that block.Step 2: Identify the error with
Usingimg.insideusageimg.insideoutside the block whereimgis defined causes a runtime error becauseimgis undefined there.Final Answer:
Theimg.insideblock is outside thedocker.withRegistryblock and will fail -> Option DQuick Check:
Variable scope matters in Jenkins pipeline blocks [OK]
- Assuming variables are global across blocks
- Confusing credentials ID syntax
- Thinking push must be outside withRegistry
Solution
Step 1: Build image and run tests inside container first
The image must be built before running tests inside it, sodocker.build()andimg.inside { }come first.Step 2: Push image inside
Pushing requires registry credentials, sodocker.withRegistry()block after testsimg.push()must be insidedocker.withRegistry()block after tests pass.Step 3: Verify variable scope and order
def img = docker.build('myapp:test') img.inside { sh 'run-tests.sh' } docker.withRegistry('https://registry.example.com', 'creds') { img.push() } correctly builds image, runs tests inside container, then pushes image with credentials in proper order and scope.Final Answer:
def img = docker.build('myapp:test') img.inside { sh 'run-tests.sh' } docker.withRegistry('https://registry.example.com', 'creds') { img.push() } -> Option AQuick Check:
Build -> Test inside container -> Push with credentials [OK]
- Pushing image before running tests
- Using
img.insideafter pushing outside scope - Defining variables inside wrong blocks
Ready to prove it?
Pass the test and this pattern moves unknown
