Copying files to and from containers in Docker - Time & Space Complexity
When copying files between your computer and a Docker container, it is important to understand how the time taken grows as the file size increases.
We want to know how the copying time changes when the files get bigger or when there are more files.
Analyze the time complexity of the following Docker commands.
# Copy a file from host to container
docker cp ./localfile.txt container_id:/path/in/container/
# Copy a file from container to host
docker cp container_id:/path/in/container/file.txt ./
These commands copy a single file either into or out of a running container.
Look for repeated actions inside the copy process.
- Primary operation: Reading and writing file data byte by byte or in chunks.
- How many times: Once for each byte or chunk of the file being copied.
The time to copy grows directly with the size of the file.
| Input Size (n in MB) | Approx. Operations |
|---|---|
| 10 | 10 million byte reads and writes |
| 100 | 100 million byte reads and writes |
| 1000 | 1 billion byte reads and writes |
Pattern observation: Doubling the file size roughly doubles the copying time.
Time Complexity: O(n)
This means the time to copy grows linearly with the file size; bigger files take proportionally longer.
[X] Wrong: "Copying multiple small files is always faster than one big file of the same total size."
[OK] Correct: Each file copy has overhead, so many small files can add extra time beyond just the total size.
Understanding how file size affects copying time helps you explain performance in container workflows clearly and confidently.
"What if we copied a directory with many files instead of a single file? How would the time complexity change?"