github.com/pwn-term/docker@v0.0.0-20210616085119-6e977cce2565/cli/docs/reference/commandline/cp.md (about)

     1  ---
     2  title: "cp"
     3  description: "The cp command description and usage"
     4  keywords: "copy, container, files, folders"
     5  ---
     6  
     7  # cp
     8  
     9  ```markdown
    10  Usage:  docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
    11          docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH
    12  
    13  Copy files/folders between a container and the local filesystem
    14  
    15  Use '-' as the source to read a tar archive from stdin
    16  and extract it to a directory destination in a container.
    17  Use '-' as the destination to stream a tar archive of a
    18  container source to stdout.
    19  
    20  Options:
    21    -L, --follow-link   Always follow symbol link in SRC_PATH
    22    -a, --archive       Archive mode (copy all uid/gid information)
    23        --help          Print usage
    24  ```
    25  
    26  ## Description
    27  
    28  The `docker cp` utility copies the contents of `SRC_PATH` to the `DEST_PATH`.
    29  You can copy from the container's file system to the local machine or the
    30  reverse, from the local filesystem to the container. If `-` is specified for
    31  either the `SRC_PATH` or `DEST_PATH`, you can also stream a tar archive from
    32  `STDIN` or to `STDOUT`. The `CONTAINER` can be a running or stopped container.
    33  The `SRC_PATH` or `DEST_PATH` can be a file or directory.
    34  
    35  The `docker cp` command assumes container paths are relative to the container's
    36  `/` (root) directory. This means supplying the initial forward slash is optional;
    37  The command sees `compassionate_darwin:/tmp/foo/myfile.txt` and
    38  `compassionate_darwin:tmp/foo/myfile.txt` as identical. Local machine paths can
    39  be an absolute or relative value. The command interprets a local machine's
    40  relative paths as relative to the current working directory where `docker cp` is
    41  run.
    42  
    43  The `cp` command behaves like the Unix `cp -a` command in that directories are
    44  copied recursively with permissions preserved if possible. Ownership is set to
    45  the user and primary group at the destination. For example, files copied to a
    46  container are created with `UID:GID` of the root user. Files copied to the local
    47  machine are created with the `UID:GID` of the user which invoked the `docker cp`
    48  command. However, if you specify the `-a` option, `docker cp` sets the ownership
    49  to the user and primary group at the source.
    50  If you specify the `-L` option, `docker cp` follows any symbolic link
    51  in the `SRC_PATH`.  `docker cp` does *not* create parent directories for
    52  `DEST_PATH` if they do not exist.
    53  
    54  Assuming a path separator of `/`, a first argument of `SRC_PATH` and second
    55  argument of `DEST_PATH`, the behavior is as follows:
    56  
    57  - `SRC_PATH` specifies a file
    58      - `DEST_PATH` does not exist
    59          - the file is saved to a file created at `DEST_PATH`
    60      - `DEST_PATH` does not exist and ends with `/`
    61          - Error condition: the destination directory must exist.
    62      - `DEST_PATH` exists and is a file
    63          - the destination is overwritten with the source file's contents
    64      - `DEST_PATH` exists and is a directory
    65          - the file is copied into this directory using the basename from
    66            `SRC_PATH`
    67  - `SRC_PATH` specifies a directory
    68      - `DEST_PATH` does not exist
    69          - `DEST_PATH` is created as a directory and the *contents* of the source
    70             directory are copied into this directory
    71      - `DEST_PATH` exists and is a file
    72          - Error condition: cannot copy a directory to a file
    73      - `DEST_PATH` exists and is a directory
    74          - `SRC_PATH` does not end with `/.` (that is: _slash_ followed by _dot_)
    75              - the source directory is copied into this directory
    76          - `SRC_PATH` does end with `/.` (that is: _slash_ followed by _dot_)
    77              - the *content* of the source directory is copied into this
    78                directory
    79  
    80  The command requires `SRC_PATH` and `DEST_PATH` to exist according to the above
    81  rules. If `SRC_PATH` is local and is a symbolic link, the symbolic link, not
    82  the target, is copied by default. To copy the link target and not the link, specify
    83  the `-L` option.
    84  
    85  A colon (`:`) is used as a delimiter between `CONTAINER` and its path. You can
    86  also use `:` when specifying paths to a `SRC_PATH` or `DEST_PATH` on a local
    87  machine, for example  `file:name.txt`. If you use a `:` in a local machine path,
    88  you must be explicit with a relative or absolute path, for example:
    89  
    90      `/path/to/file:name.txt` or `./file:name.txt`
    91  
    92  It is not possible to copy certain system files such as resources under
    93  `/proc`, `/sys`, `/dev`, [tmpfs](run.md#mount-tmpfs---tmpfs), and mounts created by
    94  the user in the container. However, you can still copy such files by manually
    95  running `tar` in `docker exec`. Both of the following examples do the same thing
    96  in different ways (consider `SRC_PATH` and `DEST_PATH` are directories):
    97  
    98  ```bash
    99  $ docker exec CONTAINER tar Ccf $(dirname SRC_PATH) - $(basename SRC_PATH) | tar Cxf DEST_PATH -
   100  ```
   101  
   102  ```bash
   103  $ tar Ccf $(dirname SRC_PATH) - $(basename SRC_PATH) | docker exec -i CONTAINER tar Cxf DEST_PATH -
   104  ```
   105  
   106  Using `-` as the `SRC_PATH` streams the contents of `STDIN` as a tar archive.
   107  The command extracts the content of the tar to the `DEST_PATH` in container's
   108  filesystem. In this case, `DEST_PATH` must specify a directory. Using `-` as
   109  the `DEST_PATH` streams the contents of the resource as a tar archive to `STDOUT`.