github.com/pwn-term/docker@v0.0.0-20210616085119-6e977cce2565/cli/man/src/container/cp.md (about) 1 The `docker container cp` utility copies the contents of `SRC_PATH` to the `DEST_PATH`. 2 You can copy from the container's file system to the local machine or the 3 reverse, from the local filesystem to the container. If `-` is specified for 4 either the `SRC_PATH` or `DEST_PATH`, you can also stream a tar archive from 5 `STDIN` or to `STDOUT`. The `CONTAINER` can be a running or stopped container. 6 The `SRC_PATH` or `DEST_PATH` can be a file or directory. 7 8 The `docker container cp` command assumes container paths are relative to the container's 9 `/` (root) directory. This means supplying the initial forward slash is optional; 10 The command sees `compassionate_darwin:/tmp/foo/myfile.txt` and 11 `compassionate_darwin:tmp/foo/myfile.txt` as identical. Local machine paths can 12 be an absolute or relative value. The command interprets a local machine's 13 relative paths as relative to the current working directory where `docker container cp` is 14 run. 15 16 The `cp` command behaves like the Unix `cp -a` command in that directories are 17 copied recursively with permissions preserved if possible. Ownership is set to 18 the user and primary group at the destination. For example, files copied to a 19 container are created with `UID:GID` of the root user. Files copied to the local 20 machine are created with the `UID:GID` of the user which invoked the `docker container cp` 21 command. If you specify the `-L` option, `docker container cp` follows any symbolic link 22 in the `SRC_PATH`. `docker container cp` does *not* create parent directories for 23 `DEST_PATH` if they do not exist. 24 25 Assuming a path separator of `/`, a first argument of `SRC_PATH` and second 26 argument of `DEST_PATH`, the behavior is as follows: 27 28 - `SRC_PATH` specifies a file 29 - `DEST_PATH` does not exist 30 - the file is saved to a file created at `DEST_PATH` 31 - `DEST_PATH` does not exist and ends with `/` 32 - Error condition: the destination directory must exist. 33 - `DEST_PATH` exists and is a file 34 - the destination is overwritten with the source file's contents 35 - `DEST_PATH` exists and is a directory 36 - the file is copied into this directory using the basename from 37 `SRC_PATH` 38 - `SRC_PATH` specifies a directory 39 - `DEST_PATH` does not exist 40 - `DEST_PATH` is created as a directory and the *contents* of the source 41 directory are copied into this directory 42 - `DEST_PATH` exists and is a file 43 - Error condition: cannot copy a directory to a file 44 - `DEST_PATH` exists and is a directory 45 - `SRC_PATH` does not end with `/.` (that is: _slash_ followed by _dot_) 46 - the source directory is copied into this directory 47 - `SRC_PATH` does end with `/.` (that is: _slash_ followed by _dot_) 48 - the *content* of the source directory is copied into this 49 directory 50 51 The command requires `SRC_PATH` and `DEST_PATH` to exist according to the above 52 rules. If `SRC_PATH` is local and is a symbolic link, the symbolic link, not 53 the target, is copied by default. To copy the link target and not the link, 54 specify the `-L` option. 55 56 A colon (`:`) is used as a delimiter between `CONTAINER` and its path. You can 57 also use `:` when specifying paths to a `SRC_PATH` or `DEST_PATH` on a local 58 machine, for example `file:name.txt`. If you use a `:` in a local machine path, 59 you must be explicit with a relative or absolute path, for example: 60 61 `/path/to/file:name.txt` or `./file:name.txt` 62 63 It is not possible to copy certain system files such as resources under 64 `/proc`, `/sys`, `/dev`, tmpfs, and mounts created by the user in the container. 65 However, you can still copy such files by manually running `tar` in `docker exec`. 66 For example (consider `SRC_PATH` and `DEST_PATH` are directories): 67 68 $ docker exec foo tar Ccf $(dirname SRC_PATH) - $(basename SRC_PATH) | tar Cxf DEST_PATH - 69 70 or 71 72 $ tar Ccf $(dirname SRC_PATH) - $(basename SRC_PATH) | docker exec -i foo tar Cxf DEST_PATH - 73 74 75 Using `-` as the `SRC_PATH` streams the contents of `STDIN` as a tar archive. 76 The command extracts the content of the tar to the `DEST_PATH` in container's 77 filesystem. In this case, `DEST_PATH` must specify a directory. Using `-` as 78 the `DEST_PATH` streams the contents of the resource as a tar archive to `STDOUT`. 79 80 # EXAMPLES 81 82 Suppose a container has finished producing some output as a file it saves 83 to somewhere in its filesystem. This could be the output of a build job or 84 some other computation. You can copy these outputs from the container to a 85 location on your local host. 86 87 If you want to copy the `/tmp/foo` directory from a container to the 88 existing `/tmp` directory on your host. If you run `docker container cp` in your `~` 89 (home) directory on the local host: 90 91 $ docker container cp compassionate_darwin:tmp/foo /tmp 92 93 Docker creates a `/tmp/foo` directory on your host. Alternatively, you can omit 94 the leading slash in the command. If you execute this command from your home 95 directory: 96 97 $ docker container cp compassionate_darwin:tmp/foo tmp 98 99 If `~/tmp` does not exist, Docker will create it and copy the contents of 100 `/tmp/foo` from the container into this new directory. If `~/tmp` already 101 exists as a directory, then Docker will copy the contents of `/tmp/foo` from 102 the container into a directory at `~/tmp/foo`. 103 104 When copying a single file to an existing `LOCALPATH`, the `docker container cp` command 105 will either overwrite the contents of `LOCALPATH` if it is a file or place it 106 into `LOCALPATH` if it is a directory, overwriting an existing file of the same 107 name if one exists. For example, this command: 108 109 $ docker container cp sharp_ptolemy:/tmp/foo/myfile.txt /test 110 111 If `/test` does not exist on the local machine, it will be created as a file 112 with the contents of `/tmp/foo/myfile.txt` from the container. If `/test` 113 exists as a file, it will be overwritten. Lastly, if `/test` exists as a 114 directory, the file will be copied to `/test/myfile.txt`. 115 116 Next, suppose you want to copy a file or folder into a container. For example, 117 this could be a configuration file or some other input to a long running 118 computation that you would like to place into a created container before it 119 starts. This is useful because it does not require the configuration file or 120 other input to exist in the container image. 121 122 If you have a file, `config.yml`, in the current directory on your local host 123 and wish to copy it to an existing directory at `/etc/my-app.d` in a container, 124 this command can be used: 125 126 $ docker container cp config.yml myappcontainer:/etc/my-app.d 127 128 If you have several files in a local directory `/config` which you need to copy 129 to a directory `/etc/my-app.d` in a container: 130 131 $ docker container cp /config/. myappcontainer:/etc/my-app.d 132 133 The above command will copy the contents of the local `/config` directory into 134 the directory `/etc/my-app.d` in the container. 135 136 Finally, if you want to copy a symbolic link into a container, you typically 137 want to copy the linked target and not the link itself. To copy the target, use 138 the `-L` option, for example: 139 140 $ ln -s /tmp/somefile /tmp/somefile.ln 141 $ docker container cp -L /tmp/somefile.ln myappcontainer:/tmp/ 142 143 This command copies content of the local `/tmp/somefile` into the file 144 `/tmp/somefile.ln` in the container. Without `-L` option, the `/tmp/somefile.ln` 145 preserves its symbolic link but not its content.