Here’s a complete tutorial on the docker cp
command, including how it works, its use cases, and a comprehensive list of examples.
What is docker cp
?
docker cp
is a Docker command used to copy files or directories between your local machine (host) and a Docker container. It’s similar to the Linux cp
command but operates between the host system and Docker containers.
Key Features of docker cp
:
- Copy files from the host to a container.
- Copy files from a container to the host.
- Works with absolute and relative paths.
- Supports copying entire directories.
Basic Syntax
docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH
docker cp [OPTIONS] SRC_PATH CONTAINER:DEST_PATH
Arguments:
CONTAINER
: The name or ID of the container.SRC_PATH
: The source file or directory path (inside the container or on the host).DEST_PATH
: The destination file or directory path (inside the container or on the host).
Examples of docker cp
1. Copy a File from Host to Container
docker cp myfile.txt my_container:/tmp/myfile.txt
This command copies myfile.txt
from the host to the /tmp
directory of my_container
.
2. Copy a File from Container to Host
docker cp my_container:/etc/hostname ./hostname.txt
This command copies the /etc/hostname
file from the container to the current directory on the host and names it hostname.txt
.
3. Copy a Directory from Host to Container
docker cp ./myfolder my_container:/var/myfolder
This copies the entire myfolder
directory from the host into /var
in the container.
4. Copy a Directory from Container to Host
docker cp my_container:/usr/share/nginx/html ./nginx-html
This copies the /usr/share/nginx/html
directory from the container to the current directory on the host.
5. Copy a File and Overwrite in the Container
docker cp updated-config.conf my_container:/etc/myapp/config.conf
The existing config.conf
in the container is replaced with updated-config.conf
from the host.
6. Copy a File with a New Name
docker cp my_container:/var/log/nginx/access.log ./nginx_access_backup.log
This copies the access.log
from the container and saves it on the host as nginx_access_backup.log
.
7. Copy Files Between Two Containers (Indirectly)
Docker does not allow direct copying between containers using docker cp
. However, you can do it in two steps:
- Copy the file to the host:
docker cp container1:/path/to/file ./file
- Copy the file to the second container:
docker cp ./file container2:/path/to/file
8. Using Absolute Paths
docker cp /home/user/myfile.txt my_container:/tmp/
This uses an absolute path to copy myfile.txt
from the host to the /tmp
directory in the container.
9. Copying Hidden Files
To copy hidden files (like .env
), use:
docker cp ./myfolder/.env my_container:/app/.env
10. Copy Logs from Container to Host
If you want to copy logs for analysis:
docker cp my_container:/var/log/app.log ./app.log
11. Copy a File from a Stopped Container
You can still use docker cp
on stopped containers:
docker cp stopped_container:/data/backup.tar.gz ./backup.tar.gz
12. Copy Large Directories
To copy large directories (e.g., database backups or logs), ensure you have enough space on your host:
docker cp my_container:/var/lib/mysql ./mysql_backup
13. Verify File Copy
After copying, you can verify it with:
docker exec my_container ls -l /path/in/container
List of Common docker cp
Commands
Command | Description |
---|---|
docker cp file.txt my_container:/path/to/file.txt | Copy a file from host to container |
docker cp my_container:/path/to/file.txt ./file.txt | Copy a file from container to host |
docker cp ./myfolder my_container:/path/in/container | Copy a directory from host to container |
docker cp my_container:/path/in/container ./folder | Copy a directory from container to host |
docker cp stopped_container:/file ./file | Copy a file from a stopped container to host |
docker cp ./file container:/new/path/newfile.txt | Copy a file with a new name in the container |
docker cp hidden/.env my_container:/app/.env | Copy hidden files from host to container |
Best Practices
- Use absolute paths for clarity and to avoid mistakes.
- Verify copied files with
docker exec
ordocker logs
. - Avoid overwriting critical files inside the container unless necessary.
- Backup large directories regularly using
docker cp
to avoid data loss.
Common Errors and Solutions
- “No such file or directory”
→ Ensure the file or directory exists on the host or in the container. - Permission Denied
→ Check permissions of the destination directory and run Docker commands with the correct privileges (sudo
if needed). - Container Not Found
→ Ensure the container is running or use the correct name/ID.
Would you like examples for specific file types, like config files, web assets, or database backups? 😊
I’m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I am working at Cotocus. I blog tech insights at DevOps School, travel stories at Holiday Landmark, stock market tips at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at I reviewed , and SEO strategies at Wizbrand.
Please find my social handles as below;
Rajesh Kumar Personal Website
Rajesh Kumar at YOUTUBE
Rajesh Kumar at INSTAGRAM
Rajesh Kumar at X
Rajesh Kumar at FACEBOOK
Rajesh Kumar at LINKEDIN
Rajesh Kumar at PINTEREST
Rajesh Kumar at QUORA
Rajesh Kumar at WIZBRAND