Find the Best Cosmetic Hospitals

Explore trusted cosmetic hospitals and make a confident choice for your transformation.

โ€œInvest in yourself โ€” your confidence is always worth it.โ€

Explore Cosmetic Hospitals

Start your journey today โ€” compare options in one place.

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
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:

  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/
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

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? ๐Ÿ˜Š

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services โ€” all in one place.

Explore Hospitals
Iโ€™m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I have worked at <a href="https://www.cotocus.com/">Cotocus</a>. I share tech blog at <a href="https://www.devopsschool.com/">DevOps School</a>, travel stories at <a href="https://www.holidaylandmark.com/">Holiday Landmark</a>, stock market tips at <a href="https://www.stocksmantra.in/">Stocks Mantra</a>, health and fitness guidance at <a href="https://www.mymedicplus.com/">My Medic Plus</a>, product reviews at <a href="https://www.truereviewnow.com/">TrueReviewNow</a> , and SEO strategies at <a href="https://www.wizbrand.com/">Wizbrand.</a> Do you want to learn <a href="https://www.quantumuting.com/">Quantum Computing</a>? <strong>Please find my social handles as below;</strong> <a href="https://www.rajeshkumar.xyz/">Rajesh Kumar Personal Website</a> <a href="https://www.youtube.com/TheDevOpsSchool">Rajesh Kumar at YOUTUBE</a> <a href="https://www.instagram.com/rajeshkumarin">Rajesh Kumar at INSTAGRAM</a> <a href="https://x.com/RajeshKumarIn">Rajesh Kumar at X</a> <a href="https://www.facebook.com/RajeshKumarLog">Rajesh Kumar at FACEBOOK</a> <a href="https://www.linkedin.com/in/rajeshkumarin/">Rajesh Kumar at LINKEDIN</a> <a href="https://www.wizbrand.com/rajeshkumar">Rajesh Kumar at WIZBRAND</a> <a href="https://www.rajeshkumar.xyz/dailylogs">Rajesh Kumar DailyLogs</a>

Related Posts

Docker Tutorials: Docker Image – Understanding Dockerfiles instructions & options

Hereโ€™s a step-by-step tutorial for Dockerfile, including explanations and examples for each major command. Dockerfile Tutorial A Dockerfile is a text file containing instructions to build a…

Read More

Docker Tutorials: Docker Image – Example and Sample Programs of Dockerfile

Reference Rajesh Kumar Iโ€™m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I have worked at Cotocus. I share tech blog at DevOps School, travel stories…

Read More

Docker Tutorials: Installation and Configurations

Docker Installation in Centos/RHEL Method -1: How to install Docker Community Edition via YUM? Step 1 – Install required packages. yum-utils provides the yum-config-manager utility, and device-mapper-persistent-data…

Read More

Docker Tutorials: How to Install Docker in Ubuntu?

Install Docker Engine in Ubuntu NOTE – All commands you must run as root user or add a current user into a linux group name called “docker”…

Read More

Docker Lab, Excercise & Assignment – 7 – Docker Volume

Below is a very detailed tutorial and lab manual for learning Docker Volumes, using the Ubuntu image for practical, hands-on labs. This covers all major types of…

Read More

Docker Lab, Excercise & Assignment – 4 – Docker Networking

Hereโ€™s an in-depth, step-by-step tutorial and lab manual for Docker Networkingโ€”starting from basics, covering all core concepts, and providing a hands-on guide to every feature and command….

Read More
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x