docker cp

Command Name: 

docker cp

Description:

Copy files/folders between a container and the local filesystem. You can copy files or folders from your local file systems to the docker container.

This is useful when you want to copy any file or folder to the container. 

Best example is suppose you are developing some docker image and you want to override the setting file. That case for testing purposes you want to override the setting file of container from your setting file which is present on your local computer docker cp command helps in that case.


Sometimes your docker containers are running on a server and you need to copy your local computer setting file to the docker container.

In that case you first need to copy your local setting file to the server using putty or winscp and then on the server you need to use docker cp command.

 

Syntax:

easywhatis$ 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

Use '-' as the source to read a tar archive from stdin
and extract it to a directory destination in a container.
Use '-' as the destination to stream a tar archive of a
container source to stdout.

Options:
  -a, --archive       Archive mode (copy all uid/gid information)
  -L, --follow-link   Always follow symbol link in SRC_PATH
easywhatis$

Example:

Copy file from local file system to container

Steps:

  1. Download ubuntu image from docker hub
  2. Run ubuntu container
  3. Check file is present or not in ubuntu home directory
  4. Copy file test.txt from local file system to ubuntu
easywhatis$ docker run -it --rm -d ubuntu
21e5523f3cd0f1e9f7fafaf66c4af57959ea38c259217eb5445ead67d2005fc2
easywhatis$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
21e5523f3cd0        ubuntu              "/bin/bash"         2 seconds ago       Up 1 second                             pedantic_edison
easywhatis$ docker exec -it 21e5523f3cd0 /bin/bash
root@21e5523f3cd0:/# pwd
/
root@21e5523f3cd0:/# exit
exit
easywhatis$ ls -lt
total 8
-rw-r--r--   1 easywhatis  staff    23 Apr 12 10:52 test.txt
drwx------+  9 easywhatis  staff   288 Apr 12 10:50 Movies
drwx------+ 43 easywhatis  staff  1376 Apr 11 18:15 Downloads
drwx------+ 13 easywhatis  staff   416 Apr  7 09:48 Desktop
drwx------+  5 easywhatis  staff   160 Apr  5 11:10 Documents
drwx------@ 60 easywhatis  staff  1920 Mar 29 19:49 Library
drwx------@  3 easywhatis  staff    96 Mar 28 10:16 Applications
drwx------+  3 easywhatis  staff    96 Mar 28 10:09 Music
drwx------+  3 easywhatis  staff    96 Mar 28 10:09 Pictures
drwxr-xr-x+  4 easywhatis  staff   128 Mar 28 10:09 Public
easywhatis$ docker cp test.txt 21e5523f3cd0/
must specify at least one container source
easywhatis$ docker cp test.txt 21e5523f3cd0:/
easywhatis$ docker exec -it 21e5523f3cd0 /bin/bash
root@21e5523f3cd0:/# ls -lt
total 68
drwx------   1 root root    4096 Apr 12 05:31 root
drwxr-xr-x   5 root root     360 Apr 12 05:31 dev
dr-xr-xr-x  13 root root       0 Apr 12 05:31 sys
dr-xr-xr-x 161 root root       0 Apr 12 05:31 proc
drwxr-xr-x   1 root root    4096 Apr 12 05:31 etc
-rw-r--r--   1  502 dialout   23 Apr 12 05:22 test.txt
drwxr-xr-x   1 root root    4096 Mar 20 19:20 run
drwxr-xr-x   1 root root    4096 Mar 20 19:20 sbin
drwxrwxrwt   2 root root    4096 Mar 11 21:05 tmp
drwxr-xr-x   2 root root    4096 Mar 11 21:05 bin
drwxr-xr-x   1 root root    4096 Mar 11 21:05 var
drwxr-xr-x   2 root root    4096 Mar 11 21:03 lib64
drwxr-xr-x   2 root root    4096 Mar 11 21:03 media
drwxr-xr-x   2 root root    4096 Mar 11 21:03 mnt
drwxr-xr-x   2 root root    4096 Mar 11 21:03 opt
drwxr-xr-x   2 root root    4096 Mar 11 21:03 srv
drwxr-xr-x   1 root root    4096 Mar 11 21:03 usr
drwxr-xr-x   2 root root    4096 Apr 24  2018 boot
drwxr-xr-x   2 root root    4096 Apr 24  2018 home
drwxr-xr-x   8 root root    4096 May 23  2017 lib
root@21e5523f3cd0:/#

Copy file from container to local file system

Steps:

  1. Download ubuntu image from docker hub
  2. Run ubuntu container
  3. Check file is present or not in local file system home directory
  4. Copy file test.txt from ubuntu to local file system
easywhatis$ pwd
/Users/easywhatis
easywhatis$ ls -lt
total 0
drwx------+  9 easywhatis  staff   288 Apr 12 10:50 Movies
drwx------+ 43 easywhatis  staff  1376 Apr 11 18:15 Downloads
drwx------+ 13 easywhatis  staff   416 Apr  7 09:48 Desktop
drwx------+  5 easywhatis  staff   160 Apr  5 11:10 Documents
drwx------@ 60 easywhatis  staff  1920 Mar 29 19:49 Library
drwx------@  3 easywhatis  staff    96 Mar 28 10:16 Applications
drwx------+  3 easywhatis  staff    96 Mar 28 10:09 Music
drwx------+  3 easywhatis  staff    96 Mar 28 10:09 Pictures
drwxr-xr-x+  4 easywhatis  staff   128 Mar 28 10:09 Public
easywhatis$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
21e5523f3cd0        ubuntu              "/bin/bash"         6 minutes ago       Up 6 minutes                            pedantic_edison
easywhatis$ docker exec -it 21e5523f3cd0 /bin/bash
root@21e5523f3cd0:/# ls -lt
total 68
dr-xr-xr-x  13 root root       0 Apr 12 05:33 sys
drwx------   1 root root    4096 Apr 12 05:31 root
drwxr-xr-x   5 root root     360 Apr 12 05:31 dev
dr-xr-xr-x 159 root root       0 Apr 12 05:31 proc
drwxr-xr-x   1 root root    4096 Apr 12 05:31 etc
-rw-r--r--   1  502 dialout   23 Apr 12 05:22 test.txt
drwxr-xr-x   1 root root    4096 Mar 20 19:20 run
drwxr-xr-x   1 root root    4096 Mar 20 19:20 sbin
drwxrwxrwt   2 root root    4096 Mar 11 21:05 tmp
drwxr-xr-x   2 root root    4096 Mar 11 21:05 bin
drwxr-xr-x   1 root root    4096 Mar 11 21:05 var
drwxr-xr-x   2 root root    4096 Mar 11 21:03 lib64
drwxr-xr-x   2 root root    4096 Mar 11 21:03 media
drwxr-xr-x   2 root root    4096 Mar 11 21:03 mnt
drwxr-xr-x   2 root root    4096 Mar 11 21:03 opt
drwxr-xr-x   2 root root    4096 Mar 11 21:03 srv
drwxr-xr-x   1 root root    4096 Mar 11 21:03 usr
drwxr-xr-x   2 root root    4096 Apr 24  2018 boot
drwxr-xr-x   2 root root    4096 Apr 24  2018 home
drwxr-xr-x   8 root root    4096 May 23  2017 lib
root@21e5523f3cd0:/# exit
exit
easywhatis$ docker cp 21e5523f3cd0:/test.txt /
open /test.txt: permission denied
easywhatis$ docker cp 21e5523f3cd0:/test.txt /Users/easywhatis/
easywhatis$ pwd
/Users/easywhatis
easywhatis$ ls -lt
total 8
-rw-r--r--   1 easywhatis  staff    23 Apr 12 10:52 test.txt
drwx------+  9 easywhatis  staff   288 Apr 12 10:50 Movies
drwx------+ 43 easywhatis  staff  1376 Apr 11 18:15 Downloads
drwx------+ 13 easywhatis  staff   416 Apr  7 09:48 Desktop
drwx------+  5 easywhatis  staff   160 Apr  5 11:10 Documents
drwx------@ 60 easywhatis  staff  1920 Mar 29 19:49 Library
drwx------@  3 easywhatis  staff    96 Mar 28 10:16 Applications
drwx------+  3 easywhatis  staff    96 Mar 28 10:09 Music
drwx------+  3 easywhatis  staff    96 Mar 28 10:09 Pictures
drwxr-xr-x+  4 easywhatis  staff   128 Mar 28 10:09 Public
easywhatis$