📖 Scenario: You are working with Docker containers and need to move files between your local machine and a running container. This is a common task when you want to add configuration files or retrieve logs from containers.
🎯 Goal: Learn how to copy files into a running Docker container and copy files out from it using Docker commands.
📋 What You'll Learn
Create a simple Docker container running in the background
Copy a file from the local machine into the container
Copy a file from the container back to the local machine
Verify the copied files exist in the correct locations
💡 Why This Matters
🌍 Real World
Copying files to and from containers is essential when you need to add configuration files, scripts, or retrieve logs and data from running containers.
💼 Career
DevOps engineers and developers often need to manage files inside containers for debugging, configuration, and deployment tasks.
Progress0 / 4 steps
1
Start a running container
Run a Docker container named mycontainer in detached mode using the alpine image and keep it running with the command sleep 300.
Docker
Hint
Use docker run -d --name mycontainer alpine sleep 300 to start the container.
2
Copy a file into the container
Create a file named hello.txt locally with the content Hello from local! and then copy this file into the container mycontainer at path /root/hello.txt using the Docker copy command.
Docker
Hint
Use echo to create the file and docker cp to copy it into the container.
3
Copy a file from the container to local
Inside the container mycontainer, create a file /root/containerfile.txt with content Hello from container!. Then copy this file from the container to your local machine as containerfile.txt using the Docker copy command.
Docker
Hint
Use docker exec to create the file inside the container and docker cp to copy it out.
4
Verify copied files
Use cat commands to display the contents of the local files hello.txt and containerfile.txt to verify the copy operations worked correctly.
Docker
Hint
Use cat hello.txt and cat containerfile.txt to see the file contents.
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
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.
Step 2: Differentiate from other Docker commands
Other options relate to images, environment variables, or logs, which are not handled by docker cp.
Final Answer:
Copies files or folders between your computer and a Docker container -> Option A
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
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.
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.
Final Answer:
docker cp app.log webapp:/var/log/ -> Option A
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
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.
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.
Final Answer:
Copies the file dump.sql from the container to the current directory as backup.sql -> Option B
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
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 for docker cp.
Final Answer:
The file /app/config.json does not exist inside the container -> Option C
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
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 D
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 unknownstrongwhile earning +1 Grip toward your certificate.