Bird
Raised Fist0
Dockerdevops~20 mins

Copying files to and from containers in Docker - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Docker File Copy Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Copying a file from host to container
You run the command docker cp ./app/config.yaml mycontainer:/app/config.yaml. What is the expected result?
AThe command fails because the container name must be specified with a leading slash.
BThe command copies the entire app directory from the host to the container.
CThe file config.yaml is copied from the container's /app directory to the host's current directory.
DThe file config.yaml is copied from the host's current directory to /app inside the container named mycontainer.
Attempts:
2 left
💡 Hint
Remember the syntax is docker cp source_path container:destination_path to copy files into a container.
💻 Command Output
intermediate
2:00remaining
Copying a directory from container to host
What happens when you run docker cp mycontainer:/var/logs ./logs_backup?
AThe entire /var/logs directory from the container is copied to a new directory logs_backup on the host.
BOnly the files inside /var/logs are copied, but not the directory itself.
CThe command fails because the destination path on the host must be absolute.
DThe command copies the /var/logs directory from the host to the container.
Attempts:
2 left
💡 Hint
Check if the source is container:path and destination is host path.
❓ Troubleshoot
advanced
2:00remaining
Why does this docker cp command fail?
You run docker cp /home/user/data.txt mycontainer:/data/ but get an error: Error: No such container:path: mycontainer:/data/. What is the most likely cause?
AThe docker cp command cannot copy files into directories inside containers.
BThe source file /home/user/data.txt does not exist on the host.
CThe container named mycontainer is not running or does not exist.
DThe destination path inside the container must not end with a slash.
Attempts:
2 left
💡 Hint
Check if the container name is correct and the container is running.
✅ Best Practice
advanced
2:00remaining
Best practice for copying large files into containers
Which approach is best when you need to copy a large file repeatedly into a container during development?
AMount the host directory containing the file as a volume inside the container.
BUse docker cp every time you need to copy the file.
CRebuild the container image with the file included each time.
DUse scp to copy the file into the container's IP address.
Attempts:
2 left
💡 Hint
Think about avoiding repeated copying and making changes instantly visible.
🔀 Workflow
expert
3:00remaining
Order the steps to copy a file from host to a running container and verify it
Arrange these steps in the correct order to copy a file named settings.json from the host to a running container named webapp and verify the copy.
A1,4,2,3
B4,1,2,3
C4,2,1,3
D2,4,1,3
Attempts:
2 left
💡 Hint
You must confirm the container is running before copying files.

Practice

(1/5)
1. What does the docker cp command do?
easy
A. Copies files or folders between your computer and a Docker container
B. Copies Docker images between registries
C. Copies environment variables from a container
D. Copies container logs to a file

Solution

  1. Step 1: Understand the purpose of docker cp

    The docker cp command is designed to transfer files or folders between the host machine and a Docker container.
  2. Step 2: Differentiate from other Docker commands

    Other options relate to images, environment variables, or logs, which are not handled by docker cp.
  3. Final Answer:

    Copies files or folders between your computer and a Docker container -> Option A
  4. Quick Check:

    docker cp = copy files/folders [OK]
Hint: Remember: cp means copy files between host and container [OK]
Common Mistakes:
  • Confusing docker cp with image transfer commands
  • Thinking it copies logs or environment variables
  • Assuming it copies container settings
2. Which of the following is the correct syntax to copy a file named app.log from your computer to a container named webapp at path /var/log/?
easy
A. docker cp app.log webapp:/var/log/
B. docker cp webapp:/var/log/ app.log
C. docker copy app.log webapp:/var/log/
D. docker cp /var/log/ app.log webapp

Solution

  1. Step 1: Identify source and destination in docker cp

    The syntax is docker cp [source] [destination]. To copy from host to container, source is local file, destination is container:path.
  2. Step 2: Check each option's order and command correctness

    docker cp app.log webapp:/var/log/ correctly uses docker 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 command docker copy. docker cp /var/log/ app.log webapp has wrong order and extra arguments.
  3. Final Answer:

    docker cp app.log webapp:/var/log/ -> Option A
  4. Quick Check:

    docker cp source destination [OK]
Hint: Source first, then destination with container:path [OK]
Common Mistakes:
  • Swapping source and destination paths
  • Using wrong command name like docker copy
  • Missing container name or colon separator
3. Given a container named db with a file /data/dump.sql, what will be the result of running docker cp db:/data/dump.sql ./backup.sql?
medium
A. Throws an error because the destination must be a directory
B. Copies the file dump.sql from the container to the current directory as backup.sql
C. Copies the file backup.sql from the current directory to the container at /data/dump.sql
D. Copies the entire /data directory from the container to ./backup.sql

Solution

  1. Step 1: Analyze the command syntax

    The command copies from container db path /data/dump.sql to local path ./backup.sql. This means copying a single file from container to host with a new name.
  2. Step 2: Understand the effect on local filesystem

    The file dump.sql inside the container is copied and saved locally as backup.sql in the current directory.
  3. Final Answer:

    Copies the file dump.sql from the container to the current directory as backup.sql -> Option B
  4. Quick Check:

    docker cp container:file localfile [OK]
Hint: Container source first, local destination second [OK]
Common Mistakes:
  • Confusing source and destination order
  • Assuming destination must be a directory
  • Thinking it copies directories when given a file
4. You run docker cp mycontainer:/app/config.json ./config.json but get an error: no such file or directory. What is the most likely cause?
medium
A. You forgot to specify the container ID instead of name
B. The destination path ./config.json is invalid on your host
C. The file /app/config.json does not exist inside the container
D. You need to run the command inside the container shell

Solution

  1. 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.
  2. 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 for docker cp.
  3. Final Answer:

    The file /app/config.json does not exist inside the container -> Option C
  4. Quick Check:

    Source file missing = error [OK]
Hint: Check source file path inside container first [OK]
Common Mistakes:
  • Assuming destination path causes error
  • Thinking container name must be ID only
  • Trying to run docker cp inside container shell
5. You want to copy a folder named logs from your host to a running container appserver at path /var/app/. Which command correctly copies the entire folder including its contents?
hard
A. docker cp logs/* appserver:/var/app/
B. docker cp logs/ appserver:/var/app/
C. docker cp logs appserver:/var/app/
D. docker cp logs appserver:/var/app/logs

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    docker cp logs appserver:/var/app/logs -> Option D
  4. Quick Check:

    Include folder name in destination to copy folder [OK]
Hint: Add folder name in destination path to copy folder [OK]
Common Mistakes:
  • Using trailing slash and losing folder structure
  • Copying only files inside folder, not subfolders
  • Not specifying full destination path including folder name
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard