Best Docker cp Command Examples with Usage

Share on Social Media

This article is for quick reference about the Docker cp command examples and their usage. Docker cp command is used to copy files and directories from Docker host to a container or reverse. #centlinux #linux #docker

Environment Specification:

We are using a Ubuntu based Docker host with following specification.

  • CPU – 3.4 Ghz (2 cores)
  • Memory – 2 GB
  • Storage – 20 GB
  • Operating System – Ubuntu Server 18.04 LTS
  • Hostname – docker-01.centlinux.com
  • IP Address – 192.168.116.219 /24

Get Help on Docker cp command:

Connect to your Docker Host (docker-01.centlinux.com) as an admin user by using a ssh client.

Docker cp command is a very simple command and work exactly same like the Linux cp command. Whereas, its syntax is quiet similar to Linux scp command.

We can get help on Docker cp command at Linux shell in many ways. To see the syntax of Docker cp, we can use following command.

$ docker cp help
"docker cp" requires exactly 2 arguments.
See 'docker cp --help'.

Usage:  docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
        docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH

Copy files/folders between a container and the local filesystem

For a complete desciption of the Docker cp command, switches and arguments, we can check the manual pages of the docker command.

$ man docker

Manual pages for the docker command is as follows.

DOCKER(1)                         APRIL 2014                         DOCKER(1)

NAME
       docker - Docker image and container command line interface

SYNOPSIS
       docker [OPTIONS] COMMAND [ARG...]

       docker [--help|-v|--version]

DESCRIPTION
       docker is a client for interacting with the daemon (see dockerd(8))
       through the CLI.

       The Docker CLI has over 30 commands. The commands are listed below and
       each has its own man page which explain usage and arguments.

       To see the man page for a command run man docker <command>.

OPTIONS
       --help
         Print usage statement

 Manual page docker(1) line 1 (press h for help or q to quit)

Besides that, advanced and up-to-date description about the Docker cp command and examples are available in Docker cp online documentation.

Copy Files from Docker Host to Container:

To copy a file from host machine to a container, have a look at the folowing Docker cp command example.

$ sudo docker cp test1 6b4f9e8fbfdd:/home/node

where “6b4f9e8fbfdd” is the target container ID.

Above command will copy the test1 file to container “6b4f9e8fbfdd” into the /home/node directory.

To check the file is copied correctly, we can execute following command at Linux bash prompt.

$ sudo docker exec 6b4f9e8fbfdd cat /home/node/test1
test1-contents

Copy Files from Docker Container to Host:

Similarly, we can copy the files from a container to the host machine, by using the same Docker cp command example with a minor variation.

$ sudo docker cp 6b4f9e8fbfdd:/etc/passwd .

Above command will copy the /etc/passwd file from container “6b4f9e8fbfdd” to Docker host in current directory.

To confirm the file copy, please check the contents of the copied file.

$ cat passwd

Copy Directory from Docker Host to Container:

Just as we use Docker cp command example to copy files from Docker host to a container, we can also use the same command to copy a complete directory as well.

$ sudo docker cp testdir 6b4f9e8fbfdd:/home/node

The above command will copy the testdir directory including its contents to the container “6b4f9e8fbfdd” into the /home/node directory.

To verify that the directory is copied correctly, execute the following command at Linux bash prompt.

$ sudo docker exec 6b4f9e8fbfdd ls /home/node/testdir
test2
test3
test4

Copy Directory from Docker Container to Host:

A variation of the same Docker cp command example is also useful, if we want to copy a complete directory (including contents) from a container to host machine.

$ sudo docker cp 6b4f9e8fbfdd:/etc/profile.d .

Above command will copy the profile.d directory from container “6b4f9e8fbfdd” to current directory of Docker host.

We can verify the copy process as follows.

$ ls -al profile.d
total 16
drwxr-xr-x  2 root  root  4096 Mar 23 20:12 .
drwxr-xr-x 12 ahmer ahmer 4096 Apr  9 21:28 ..
-rw-r--r--  1 root  root   295 Nov 29 14:51 color_prompt
-rw-r--r--  1 root  root    40 Nov 29 14:51 locale

Docker cp – Use of Wild Cards:

Current releases of Docker cp does not support of wild cards in source and destination paths.

As a workaround, one can copy all the required files in a directory and then copy the directory to/from the container with the help of Docker cp command.

Conclusion – Docker cp Command Examples:

In this article, you have learned about Docker cp command examples and their usage scenarios.

Scroll to Top