MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

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

Related Posts

Debug for Docker Container

To debug a Docker container, check its state and logs first, then its network configuration, then test connectivity from another container on the same network. This page…

Read More

Install Jenkins with Docker: Persistent Setup, Port Mapping, and Logs

Learn how to run Jenkins in Docker with correct port mapping, persistent storage, and operational commands. This updated guide keeps the original workflow but adds modern, production-minded practices.

Read More

How to Access a Running Docker Container Shell: docker exec vs docker attach

Need a shell inside a running container that is already in detached mode? This updated guide shows the correct workflow, compares docker exec and docker attach, and explains when SSH does and does not make sense.

Read More

Install Atlassian Jira with Docker: Quick Start, Persistent Volumes, and Deployment Checks

This guide shows how to run Jira in Docker using the original one-line command, then improve it with persistent storage and repeatable deployment steps. It also includes a workflow diagram, command examples, and practical validation checks.

Read More

Install Sonatype Nexus with Docker: RM2/RM3 Commands, Validation Steps, and Important Caveats

A structured, production-minded rewrite of the original 2016 Docker setup for Sonatype Nexus Repository Manager 2 and 3. Includes command examples, a workflow diagram, and clear warnings about outdated tags/endpoints.

Read More

Top 5 DevOps Tools To Learn Before Writing Your Dissertation

Writing a technical dissertation is rarely just an exercise in reading sources and formatting chapters. On paper, that sounds like the job. In reality, students in computer…

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