Docker cp Command Usage with Examples - CentLinux

Latest

Friday, 10 April 2020

Docker cp Command Usage with Examples

Docker cp Command Usage with Examples

This article is for quick reference about the usage of Docker cp command. Docker cp command is used to copy files and directories from Docker host to a container or reverse.

 

This Article Provides:

     

    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 Docker Host (docker-01.centlinux.com) as an admin user by using a ssh tool.

    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.

    ahmer@docker-01:~$ 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.

    ahmer@docker-01:~$ 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 is available in Docker cp online documentation.

     

    Docker cp - Copy Files from Host to Container:

    To copy a file from Docker host to a container, we use Docker cp command as follows.

    ahmer@docker-01:~$ sudo docker cp test1 6b4f9e8fbfdd:/home/node

    Above command will copy the test1 file in container "6b4f9e8fbfdd" into the /home/node directory.

    To check the file is copied correctly, we can use following command.

    ahmer@docker-01:~$ sudo docker exec 6b4f9e8fbfdd cat /home/node/test1 test1-contents

     

    Docker cp - Copy Files from Container to Host:

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

    ahmer@docker-01:~$ 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.

    ahmer@docker-01:~$ cat passwd root:x:0:0:root:/root:/bin/ash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/mail:/sbin/nologin news:x:9:13:news:/usr/lib/news:/sbin/nologin uucp:x:10:14:uucp:/var/spool/uucppublic:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin man:x:13:15:man:/usr/man:/sbin/nologin postmaster:x:14:12:postmaster:/var/mail:/sbin/nologin cron:x:16:16:cron:/var/spool/cron:/sbin/nologin ftp:x:21:21::/var/lib/ftp:/sbin/nologin sshd:x:22:22:sshd:/dev/null:/sbin/nologin at:x:25:25:at:/var/spool/cron/atjobs:/sbin/nologin squid:x:31:31:Squid:/var/cache/squid:/sbin/nologin xfs:x:33:33:X Font Server:/etc/X11/fs:/sbin/nologin games:x:35:35:games:/usr/games:/sbin/nologin cyrus:x:85:12::/usr/cyrus:/sbin/nologin vpopmail:x:89:89::/var/vpopmail:/sbin/nologin ntp:x:123:123:NTP:/var/empty:/sbin/nologin smmsp:x:209:209:smmsp:/var/spool/mqueue:/sbin/nologin guest:x:405:100:guest:/dev/null:/sbin/nologin nobody:x:65534:65534:nobody:/:/sbin/nologin node:x:1000:1000:Linux User,,,:/home/node:/bin/sh

     

    Docker cp - Copy Directory from Host to Container:

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

    ahmer@docker-01:~$ 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, use the following command.

    ahmer@docker-01:~$ sudo docker exec 6b4f9e8fbfdd ls /home/node/testdir test2 test3 test4

     

    Docker cp - Copy Directory from Container to Host:

    The same Docker cp command is also useful, if we want to copy a complete directory (including contents) from a container to Docker host.

    Have a look at the following example.

    ahmer@docker-01:~$ 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.

    ahmer@docker-01:~$ 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.

    No comments:

    Post a comment