Docker agent in Jenkinsfile - Time & Space Complexity
We want to understand how the time Jenkins takes to run a pipeline changes when using a Docker agent.
Specifically, how does the pipeline's execution time grow as we add more steps inside the Docker container?
Analyze the time complexity of the following Jenkinsfile snippet using a Docker agent.
pipeline {
agent {
docker {
image 'python:3.12'
}
}
stages {
stage('Run Tests') {
steps {
sh 'pytest tests/'
}
}
}
}
This pipeline runs tests inside a Docker container using the specified Python image.
Look for repeated actions that affect execution time.
- Primary operation: Running the test command inside the Docker container.
- How many times: Once per pipeline run, but the test command itself may run multiple tests internally.
As the number of tests grows, the time to run them inside the Docker container grows roughly in proportion.
| Input Size (number of tests) | Approx. Operations (test runs) |
|---|---|
| 10 | 10 test executions |
| 100 | 100 test executions |
| 1000 | 1000 test executions |
Pattern observation: The time grows linearly as the number of tests increases.
Time Complexity: O(n)
This means the total time grows directly with the number of tests run inside the Docker container.
[X] Wrong: "Using a Docker agent makes the pipeline run in constant time regardless of test count."
[OK] Correct: The Docker agent just provides an environment; the time depends on how many tests run inside it.
Understanding how pipeline steps scale inside Docker helps you explain build times clearly and shows you know how environments affect execution.
"What if we added multiple stages each running tests inside separate Docker agents? How would the time complexity change?"