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