Copy from docker to local machine

Introduction

This article describes how to copy files and directories from the Docker container to the host, and vice versa.

These operations were complicated in older versions of Docker, but are now easily accomplished by using the docker cp command.

After explaining docker cp below, I will explain how to use the command with actual examples.

What is docker cp command?

The docker cp command is a command that allows you to transfer files and directories between the Docker host and the container in the same format as the Unix cp command.

Specifically, it has the following two functions.

  • Copy files and directories on the Docker host into the container
  • Copy files and directories in the container onto the Docker host

Let's look at a concrete example below.

Copy files from container to host

Basic usage when copying files from a container to a host

The basic syntax for copying files from a container to a host using the docker cp command is as follows: You can easily copy by just specifying the file path and output destination in the container you want to copy.

$ docker cp [OPTIONS] CONTAINER: SRC_PATH DEST_PATH

Specific examples are as follows. This example copies the files in the path / dir / file in the container testvm to the current directory on the Docker host.

$ docker cp testvm: / dir / file.

You can also copy with another name by specifying the file name as shown below. In the following example, the file in the path / dir / file in the container named testvm is renamed and copied to the current directory of the Docker host as file2.

$ docker cp testvm: / dir / file file2

Copy directory into container (recursive copy)

The same is true when copying a directory from a Docker container to a host instead of a file. Specify the directory, not the file, in the command. The following copies the dir directory in the testvm container to the current directory on the Docker host.

$ docker cp testvm: / dir.

Note that this is different from the Unix cp command. The docker cp command differs from the Unix cp command in that when copying directories, recursive file copying is done by default. In other words, not only the directory but the entire contents are copied.

Copy file from host to container

So what if you want to copy files from a Docker host to a container? In that case, specify the reverse of the specification when copying from the Docker container to the host. (It's easy!) The basic syntax for copying files from a host to a Docker container using the docker cp command is as follows.

docker cp [OPTIONS] SRC_PATH CONTAINER: DEST_PATH

Specific examples are as follows. In this example, the file on the host is copied to the directory/ dir /in the container named testvm.

$ docker cp file testvm: / dir /

If you want to change the file name, it is the same as the previous example. It can be changed by specifying the file name in the specification of the copy destination path. In this case, I renamed it from file to file2 and copied it to / dir in the testvm container.

$ docker cp file testvm: / dir / file2

Copy directory into host (recursive copy)

When copying a directory, specify the directory name as you would a file. The following is an example of copying the dir directory to the testvm container.

$ docker cp ./dir testvm: / dir

Again, the files in the directory are recursively copied from the host as well as the container.

Tips: behavior of docker cp

Finally, I will show you the specifications for mastering the docker cp command.

As mentioned above, using docker cp is similar to the general Unix utility cp. Specifically, it behaves as follows.

  • You can copy files even if the container is stopped.
  • If possible, recursively copy the directory along with the permissions.
  • File owner permissions specify the user and primary group at the end of the transfer. For example, if you copy the file to a container, the UID: GID will be created as the root user.
  • On the contrary, when copying the file to the local machine, the UID: GID of the user who executed the docker cp command is created.

Conclusion

I shown you how to copy files from a Docker host to a container and how to copy files from a Docker container to a host.

The docker cp command to achieve these points has much in common with the cp command, so it may be easier to operate intuitively. See the Docker documentation below for detailed command options and usage. https://docs.docker.com/engine/reference/commandline/cp/

(Posted At November 26, 2021)


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.

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.

To copy a file 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.

sudo docker cp :

For example,

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 -

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

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

sudo docker cp f4628571q5gc:/usr/src/app/file.txt . (Don’t forget the dot)

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 -

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

To copy a file 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.

sudo docker cp :

For example,

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 -

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

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

To conclude, after you have already built an image with a build context and you want to copy files and folders to and from the container, you can easily do that using the commands mentioned above.

Copy from docker to local machine

Updated on 01-Oct-2020 16:15:06

  • Related Questions & Answers
  • How to copy files from host to Docker container?
  • Copying files from Docker container to Host
  • From inside of a Docker container, how do I connect to the localhost of the machine
  • How to link jQuery from my local machine?
  • How is Docker different from a Virtual Machine?
  • Creating a MySQL Docker Container
  • Mounting a volume inside docker container
  • Working with Java inside Docker Container
  • How to get a Docker Container IP address?
  • How to backup and restore a Docker Container?
  • How to run a command inside Docker Container?
  • Installing Linux Packages Inside a Docker Container
  • Running a Docker image as a container
  • How to get a Docker container's IP address from the host?
  • Building a full-fledged data science Docker Container

How do I copy a file from Docker to local?

Obtain the name or id of the Docker container. Issue the docker cp command and reference the container name or id. The first parameter of the docker copy command is the path to the file inside the container. The second parameter of the docker copy command is the location to save the file on the host.

How Mount files from Docker container to physical server?

Follow the steps below:.
Stop running the Docker container using the following command: docker stop workbench..
Remove the existing container: docker rm workbench..
Copy a path to the folder that contains your data. ... .
Run the Docker container to mount the folder with your dataset using the following command:.

How do I store Docker images locally?

If you use the default storage driver overlay2, then your Docker images are stored in /var/lib/docker/overlay2 . There, you can find different files that represent read-only layers of a Docker image and a layer on top of it that contains your changes.

Can you use the Docker copy instruction to copy files from your local machine to a container?

The docker cp utility copies the contents of SRC_PATH to the DEST_PATH . You can copy from the container's file system to the local machine or the reverse, from the local filesystem to the container. If - is specified for either the SRC_PATH or DEST_PATH , you can also stream a tar archive from STDIN or to STDOUT .