COPY instruction for adding files in Docker - Time & Space Complexity
We want to understand how the time to copy files into a Docker image changes as the number of files grows.
How does adding more files affect the time Docker takes to build the image?
Analyze the time complexity of the following Dockerfile snippet.
COPY src/ /app/
This command copies all files from the local src/ folder into the image's /app/ folder during build.
Look at what happens when copying multiple files.
- Primary operation: Copying each file from source to destination.
- How many times: Once for each file in the
src/folder.
As the number of files increases, the time to copy grows roughly in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 files | 10 copy operations |
| 100 files | 100 copy operations |
| 1000 files | 1000 copy operations |
Pattern observation: Doubling the number of files roughly doubles the time taken.
Time Complexity: O(n)
This means the time to copy files grows linearly with the number of files.
[X] Wrong: "Copying many files takes the same time as copying one file."
[OK] Correct: Each file must be copied separately, so more files mean more work and more time.
Understanding how file operations scale helps you explain build times and optimize Dockerfiles in real projects.
What if we changed COPY to copy a single large file instead of many small files? How would the time complexity change?