Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

Docker commands Guide – docker cp with examples

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:

  1. Copy the file to the host: docker cp container1:/path/to/file ./file
  2. 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

CommandDescription
docker cp file.txt my_container:/path/to/file.txtCopy a file from host to container
docker cp my_container:/path/to/file.txt ./file.txtCopy a file from container to host
docker cp ./myfolder my_container:/path/in/containerCopy a directory from host to container
docker cp my_container:/path/in/container ./folderCopy a directory from container to host
docker cp stopped_container:/file ./fileCopy a file from a stopped container to host
docker cp ./file container:/new/path/newfile.txtCopy a file with a new name in the container
docker cp hidden/.env my_container:/app/.envCopy hidden files from host to container

Best Practices

  • Use absolute paths for clarity and to avoid mistakes.
  • Verify copied files with docker exec or docker 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

  1. “No such file or directory”
    → Ensure the file or directory exists on the host or in the container.
  2. Permission Denied
    → Check permissions of the destination directory and run Docker commands with the correct privileges (sudo if needed).
  3. 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? 😊

Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.

0
Would love your thoughts, please comment.x
()
x