github.com/zhuohuang-hust/src-cbuild@v0.0.0-20230105071821-c7aab3e7c840/man/docker-cp.1.md (about)

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