Bird
Raised Fist0
Dockerdevops~10 mins

Copying files to and from containers in Docker - Step-by-Step Execution

Choose your learning style10 modes available
Process Flow - Copying files to and from containers
Start
↓
Choose source: Host or Container
↓
Choose destination: Container or Host
↓
Run docker cp command
↓
File copied successfully
↓
End
This flow shows how to copy files either from the host to a container or from a container to the host using the docker cp command.
Execution Sample
Docker
docker cp ./hostfile.txt mycontainer:/app/containerfile.txt

docker cp mycontainer:/app/containerfile.txt ./hostfile_copy.txt
Copies a file from the host to the container, then copies it back from the container to the host with a new name.
Process Table
StepCommandSourceDestinationActionResult
1docker cp ./hostfile.txt mycontainer:/app/containerfile.txtHost ./hostfile.txtContainer /app/containerfile.txtCopy file from host to containerFile copied inside container at /app/containerfile.txt
2docker cp mycontainer:/app/containerfile.txt ./hostfile_copy.txtContainer /app/containerfile.txtHost ./hostfile_copy.txtCopy file from container to hostFile copied on host as ./hostfile_copy.txt
3----Process complete, files copied successfully
💡 All copy commands executed successfully, no errors
Status Tracker
VariableStartAfter Step 1After Step 2Final
hostfile.txtExists on hostExists on hostExists on hostExists on host
containerfile.txtDoes not existExists in container at /app/containerfile.txtExists in container at /app/containerfile.txtExists in container
hostfile_copy.txtDoes not existDoes not existExists on hostExists on host
Key Moments - 3 Insights
Why do we specify container name and path with a colon in the docker cp command?
The colon separates the container name from the file path inside the container, telling docker where to copy the file. See execution_table step 1 and 2 for examples.
Can we copy directories with docker cp the same way as files?
Yes, docker cp supports copying directories recursively. The command syntax is the same, just specify the directory path instead of a file.
What happens if the destination file already exists?
The docker cp command overwrites the destination file without warning. This is shown in the execution_table where the file is copied back to the host with a new name to avoid overwriting.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the source in step 2?
AHost ./hostfile.txt
BContainer /app/containerfile.txt
CHost ./hostfile_copy.txt
DContainer /app/hostfile.txt
💡 Hint
Check the 'Source' column in execution_table row for step 2
At which step does the file first appear inside the container?
AStep 2
BStep 3
CStep 1
DIt never appears inside the container
💡 Hint
Look at the 'Result' column in execution_table for when the file is copied inside container
If you want to copy a directory instead of a file, what changes in the command?
ANo change, just specify directory path
BUse docker copy instead of docker cp
CAdd -r flag to docker cp
DUse docker cp with --dir option
💡 Hint
Refer to key_moments about copying directories
Concept Snapshot
docker cp <source> <destination>
- Copies files or directories between host and container
- Use containerName:/path for container paths
- Overwrites destination without warning
- Supports both directions: host->container and container->host
Full Transcript
This lesson shows how to copy files to and from Docker containers using the docker cp command. You specify the source and destination paths, using containerName:/path syntax for container files. The command works both ways: copying from host to container and from container to host. Files are overwritten if the destination exists. Directories can also be copied by specifying their paths. The execution table traces two commands: copying a file into the container, then copying it back out with a new name. Variables track file existence on host and container after each step. Key moments clarify the colon syntax, directory copying, and overwriting behavior. The quiz tests understanding of source/destination and command usage.

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