0
0
Dockerdevops~10 mins

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

Choose your learning style9 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.