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
Code language: CSS (css)
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
Code language: JavaScript (javascript)
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
Code language: JavaScript (javascript)
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
Code language: JavaScript (javascript)
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
Code language: JavaScript (javascript)
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
Code language: JavaScript (javascript)
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
Code language: JavaScript (javascript)
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/
Code language: JavaScript (javascript)
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
Code language: JavaScript (javascript)
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
Code language: JavaScript (javascript)
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
Code language: JavaScript (javascript)
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
Code language: JavaScript (javascript)
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 execordocker logs. - Avoid overwriting critical files inside the container unless necessary.
- Backup large directories regularly using
docker cpto 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 (sudoif 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 Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms.
I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.
Find Trusted Cardiac Hospitals
Compare heart hospitals by city and services โ all in one place.
Explore Hospitals