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?"
Practice
docker cp command do?Solution
Step 1: Understand the purpose of
Thedocker cpdocker cpcommand is designed to transfer files or folders between the host machine and a Docker container.Step 2: Differentiate from other Docker commands
Other options relate to images, environment variables, or logs, which are not handled bydocker cp.Final Answer:
Copies files or folders between your computer and a Docker container -> Option AQuick Check:
docker cp = copy files/folders [OK]
- Confusing
docker cpwith image transfer commands - Thinking it copies logs or environment variables
- Assuming it copies container settings
app.log from your computer to a container named webapp at path /var/log/?Solution
Step 1: Identify source and destination in
The syntax isdocker cpdocker cp [source] [destination]. To copy from host to container, source is local file, destination is container:path.Step 2: Check each option's order and command correctness
docker cp app.log webapp:/var/log/ correctly usesdocker cp app.log webapp:/var/log/. docker cp webapp:/var/log/ app.log reverses source and destination. docker copy app.log webapp:/var/log/ uses wrong commanddocker copy. docker cp /var/log/ app.log webapp has wrong order and extra arguments.Final Answer:
docker cp app.log webapp:/var/log/ -> Option AQuick Check:
docker cp source destination [OK]
- Swapping source and destination paths
- Using wrong command name like docker copy
- Missing container name or colon separator
db with a file /data/dump.sql, what will be the result of running docker cp db:/data/dump.sql ./backup.sql?Solution
Step 1: Analyze the command syntax
The command copies from containerdbpath/data/dump.sqlto local path./backup.sql. This means copying a single file from container to host with a new name.Step 2: Understand the effect on local filesystem
The filedump.sqlinside the container is copied and saved locally asbackup.sqlin the current directory.Final Answer:
Copies the file dump.sql from the container to the current directory as backup.sql -> Option BQuick Check:
docker cp container:file localfile [OK]
- Confusing source and destination order
- Assuming destination must be a directory
- Thinking it copies directories when given a file
docker cp mycontainer:/app/config.json ./config.json but get an error: no such file or directory. What is the most likely cause?Solution
Step 1: Understand the error message
The error "no such file or directory" usually means the source file inside the container does not exist at the specified path.Step 2: Evaluate other options
Container name or ID can be used interchangeably if correct. Destination path on host is usually valid if relative path is used. Running inside container shell is not needed fordocker cp.Final Answer:
The file /app/config.json does not exist inside the container -> Option CQuick Check:
Source file missing = error [OK]
- Assuming destination path causes error
- Thinking container name must be ID only
- Trying to run docker cp inside container shell
logs from your host to a running container appserver at path /var/app/. Which command correctly copies the entire folder including its contents?Solution
Step 1: Understand folder copy behavior
To copy a folder and its contents, specify the folder as source and the full destination path including the folder name if you want it placed inside the container.Step 2: Analyze each option
docker cp logs appserver:/var/app/logs copies the entire logs folder into /var/app/logs, preserving folder structure. docker cp logs/ appserver:/var/app/ with trailing slash copies contents of logs into /var/app/ but does not create logs folder inside container. docker cp logs appserver:/var/app/ copies contents of logs into /var/app/ without creating logs folder. docker cp logs/* appserver:/var/app/ copies only files inside logs, not subfolders.Final Answer:
docker cp logs appserver:/var/app/logs -> Option DQuick Check:
Include folder name in destination to copy folder [OK]
- Using trailing slash and losing folder structure
- Copying only files inside folder, not subfolders
- Not specifying full destination path including folder name
Ready to prove it?
Pass the test and this pattern moves unknown
