Copy Files from Docker Container to Local Machine

If you are working on a project that requires frequent copying of files and folders either from container to your local machine or from the local machine to the container, Docker provides an easy and simple way to do that. If you have already built a Docker image which is of large size and contains a large number of files and in the midst of the project you want to copy files to and from the container, it's highly inefficient to put the files in the Docker build context and build images repeatedly. Instead, Docker allows easy copying of files to and from containers using the docker cp command.

In this article, we will learn about the commands that will allow us to do so. We will look for commands for copying the files from both local machine to container and vice versa.

Copy Files from Container to Local Machine

If you want to copy a file from your container to your local machine, you can use the following command syntax:

docker cp <Container ID>:<Path of file inside the container> <Path in the local machine>

Examples

If you have an Ubuntu container with ID f4628571q5gc and you want to copy a file from path /usr/src/app/file.txt in the container to /home/username/Desktop/folder in the local machine, you can use the command:

docker cp f4628571q5gc:/usr/src/app/file.txt /home/username/Desktop/folder/

If you want to paste the file in the current directory of your local machine, you can simply use:

docker cp f4628571q5gc:/usr/src/app/file.txt .

Note: The dot (.) represents the current directory. Don't forget to include it.

If you want to copy all the files from a particular folder in container to a folder in the local machine, use the following command:

docker cp f4628571q5gc:/usr/src/app/ /home/username/Desktop/folder/

Copy Files from Local Machine to Container

If you want to copy a file from your local machine to a container, you can use the following command syntax:

docker cp <Path in the local machine> <Container ID>:<Path inside the container>

Examples

If you have an Ubuntu container with ID f4628571q5gc and you want to copy a file from path /home/username/Desktop/folder/file.txt in the local machine to /usr/src/app/ in the container, you can use the command:

docker cp /home/username/Desktop/folder/file.txt f4628571q5gc:/usr/src/app/

If you want to copy all the files from a folder in your local machine to a folder in the container, use the following command:

docker cp /home/username/Desktop/folder f4628571q5gc:/usr/src/app/

Key Points

  • The docker cp command works with both running and stopped containers.

  • You can use either container ID or container name in the command.

  • When copying directories, the trailing slash matters for the destination path.

  • No need for sudo if your user is in the Docker group.

Conclusion

The docker cp command provides a convenient way to transfer files between Docker containers and the local filesystem without rebuilding images. This approach is particularly useful during development when you need to frequently move files in and out of containers while maintaining your existing container state.

Updated on: 2026-03-17T09:01:38+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements