summaryrefslogtreecommitdiff
path: root/devdocs/docker/engine%2Freference%2Fcommandline%2Fcp%2Findex.html
blob: a23ca6bbfe3aa6b397c0606be4244e8bd58c437a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<h1>docker cp</h1>  <p><br></p> <p>Copy files/folders between a container and the local filesystem</p> <h2 id="usage">Usage</h2> <div class="highlight"><pre class="highlight" data-language="">$ docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH
</pre></div> <p>Refer to the <a href="#options">options section</a> for an overview of available <a href="#options"><code class="language-plaintext highlighter-rouge">OPTIONS</code></a> for this command.</p> <h2 id="description">Description</h2> <p name="extended-description">The <code class="language-plaintext highlighter-rouge">docker cp</code> utility copies the contents of <code class="language-plaintext highlighter-rouge">SRC_PATH</code> to the <code class="language-plaintext highlighter-rouge">DEST_PATH</code>. You can copy from the container’s file system to the local machine or the reverse, from the local filesystem to the container. If <code class="language-plaintext highlighter-rouge">-</code> is specified for either the <code class="language-plaintext highlighter-rouge">SRC_PATH</code> or <code class="language-plaintext highlighter-rouge">DEST_PATH</code>, you can also stream a tar archive from <code class="language-plaintext highlighter-rouge">STDIN</code> or to <code class="language-plaintext highlighter-rouge">STDOUT</code>. The <code class="language-plaintext highlighter-rouge">CONTAINER</code> can be a running or stopped container. The <code class="language-plaintext highlighter-rouge">SRC_PATH</code> or <code class="language-plaintext highlighter-rouge">DEST_PATH</code> can be a file or directory.</p> <p>The <code class="language-plaintext highlighter-rouge">docker cp</code> command assumes container paths are relative to the container’s <code class="language-plaintext highlighter-rouge">/</code> (root) directory. This means supplying the initial forward slash is optional; The command sees <code class="language-plaintext highlighter-rouge">compassionate_darwin:/tmp/foo/myfile.txt</code> and <code class="language-plaintext highlighter-rouge">compassionate_darwin:tmp/foo/myfile.txt</code> as identical. Local machine paths can be an absolute or relative value. The command interprets a local machine’s relative paths as relative to the current working directory where <code class="language-plaintext highlighter-rouge">docker cp</code> is run.</p> <p>The <code class="language-plaintext highlighter-rouge">cp</code> command behaves like the Unix <code class="language-plaintext highlighter-rouge">cp -a</code> command in that directories are copied recursively with permissions preserved if possible. Ownership is set to the user and primary group at the destination. For example, files copied to a container are created with <code class="language-plaintext highlighter-rouge">UID:GID</code> of the root user. Files copied to the local machine are created with the <code class="language-plaintext highlighter-rouge">UID:GID</code> of the user which invoked the <code class="language-plaintext highlighter-rouge">docker cp</code> command. However, if you specify the <code class="language-plaintext highlighter-rouge">-a</code> option, <code class="language-plaintext highlighter-rouge">docker cp</code> sets the ownership to the user and primary group at the source. If you specify the <code class="language-plaintext highlighter-rouge">-L</code> option, <code class="language-plaintext highlighter-rouge">docker cp</code> follows any symbolic link in the <code class="language-plaintext highlighter-rouge">SRC_PATH</code>. <code class="language-plaintext highlighter-rouge">docker cp</code> does <em>not</em> create parent directories for <code class="language-plaintext highlighter-rouge">DEST_PATH</code> if they do not exist.</p> <p>Assuming a path separator of <code class="language-plaintext highlighter-rouge">/</code>, a first argument of <code class="language-plaintext highlighter-rouge">SRC_PATH</code> and second argument of <code class="language-plaintext highlighter-rouge">DEST_PATH</code>, the behavior is as follows:</p> <ul> <li>
<code class="language-plaintext highlighter-rouge">SRC_PATH</code> specifies a file <ul> <li>
<code class="language-plaintext highlighter-rouge">DEST_PATH</code> does not exist <ul> <li>the file is saved to a file created at <code class="language-plaintext highlighter-rouge">DEST_PATH</code>
</li> </ul> </li> <li>
<code class="language-plaintext highlighter-rouge">DEST_PATH</code> does not exist and ends with <code class="language-plaintext highlighter-rouge">/</code> <ul> <li>Error condition: the destination directory must exist.</li> </ul> </li> <li>
<code class="language-plaintext highlighter-rouge">DEST_PATH</code> exists and is a file <ul> <li>the destination is overwritten with the source file’s contents</li> </ul> </li> <li>
<code class="language-plaintext highlighter-rouge">DEST_PATH</code> exists and is a directory <ul> <li>the file is copied into this directory using the basename from <code class="language-plaintext highlighter-rouge">SRC_PATH</code>
</li> </ul> </li> </ul> </li> <li>
<code class="language-plaintext highlighter-rouge">SRC_PATH</code> specifies a directory <ul> <li>
<code class="language-plaintext highlighter-rouge">DEST_PATH</code> does not exist <ul> <li>
<code class="language-plaintext highlighter-rouge">DEST_PATH</code> is created as a directory and the <em>contents</em> of the source directory are copied into this directory</li> </ul> </li> <li>
<code class="language-plaintext highlighter-rouge">DEST_PATH</code> exists and is a file <ul> <li>Error condition: cannot copy a directory to a file</li> </ul> </li> <li>
<code class="language-plaintext highlighter-rouge">DEST_PATH</code> exists and is a directory <ul> <li>
<code class="language-plaintext highlighter-rouge">SRC_PATH</code> does not end with <code class="language-plaintext highlighter-rouge">/.</code> (that is: <em>slash</em> followed by <em>dot</em>) <ul> <li>the source directory is copied into this directory</li> </ul> </li> <li>
<code class="language-plaintext highlighter-rouge">SRC_PATH</code> does end with <code class="language-plaintext highlighter-rouge">/.</code> (that is: <em>slash</em> followed by <em>dot</em>) <ul> <li>the <em>content</em> of the source directory is copied into this directory</li> </ul> </li> </ul> </li> </ul> </li> </ul> <p>The command requires <code class="language-plaintext highlighter-rouge">SRC_PATH</code> and <code class="language-plaintext highlighter-rouge">DEST_PATH</code> to exist according to the above rules. If <code class="language-plaintext highlighter-rouge">SRC_PATH</code> is local and is a symbolic link, the symbolic link, not the target, is copied by default. To copy the link target and not the link, specify the <code class="language-plaintext highlighter-rouge">-L</code> option.</p> <p>A colon (<code class="language-plaintext highlighter-rouge">:</code>) is used as a delimiter between <code class="language-plaintext highlighter-rouge">CONTAINER</code> and its path. You can also use <code class="language-plaintext highlighter-rouge">:</code> when specifying paths to a <code class="language-plaintext highlighter-rouge">SRC_PATH</code> or <code class="language-plaintext highlighter-rouge">DEST_PATH</code> on a local machine, for example <code class="language-plaintext highlighter-rouge">file:name.txt</code>. If you use a <code class="language-plaintext highlighter-rouge">:</code> in a local machine path, you must be explicit with a relative or absolute path, for example:</p> <div class="highlight"><pre class="highlight" data-language="">`/path/to/file:name.txt` or `./file:name.txt`
</pre></div> <p>For example uses of this command, refer to the <a href="#examples">examples section</a> below.</p> <h2 id="options">Options</h2> <table> <thead> <tr> <td>Name, shorthand</td> <td>Default</td> <td>Description</td> </tr> </thead> <tbody> <tr> <td>
<code class="language-plaintext highlighter-rouge">--archive</code> , <code class="language-plaintext highlighter-rouge">-a</code>
</td> <td></td> <td>Archive mode (copy all uid/gid information)</td> </tr> <tr> <td>
<code class="language-plaintext highlighter-rouge">--follow-link</code> , <code class="language-plaintext highlighter-rouge">-L</code>
</td> <td></td> <td>Always follow symbol link in SRC_PATH</td> </tr>  </tbody> </table>  <h2 id="examples">Examples</h2> <p>Copy a local file into container</p> <div class="highlight"><pre class="highlight" data-language="">$ docker cp ./some_file CONTAINER:/work
</pre></div> <p>Copy files from container to local path</p> <div class="highlight"><pre class="highlight" data-language="">$ docker cp CONTAINER:/var/logs/ /tmp/app_logs
</pre></div> <p>Copy a file from container to stdout. Please note <code class="language-plaintext highlighter-rouge">cp</code> command produces a tar stream</p> <div class="highlight"><pre class="highlight" data-language="">$ docker cp CONTAINER:/var/logs/app.log - | tar x -O | grep "ERROR"
</pre></div> <h3 id="corner-cases">Corner cases</h3> <p>It is not possible to copy certain system files such as resources under <code class="language-plaintext highlighter-rouge">/proc</code>, <code class="language-plaintext highlighter-rouge">/sys</code>, <code class="language-plaintext highlighter-rouge">/dev</code>, <a href="../run/index#mount-tmpfs---tmpfs">tmpfs</a>, and mounts created by the user in the container. However, you can still copy such files by manually running <code class="language-plaintext highlighter-rouge">tar</code> in <code class="language-plaintext highlighter-rouge">docker exec</code>. Both of the following examples do the same thing in different ways (consider <code class="language-plaintext highlighter-rouge">SRC_PATH</code> and <code class="language-plaintext highlighter-rouge">DEST_PATH</code> are directories):</p> <div class="highlight"><pre class="highlight" data-language="">$ docker exec CONTAINER tar Ccf $(dirname SRC_PATH) - $(basename SRC_PATH) | tar Cxf DEST_PATH -
</pre></div> <div class="highlight"><pre class="highlight" data-language="">$ tar Ccf $(dirname SRC_PATH) - $(basename SRC_PATH) | docker exec -i CONTAINER tar Cxf DEST_PATH -
</pre></div> <p>Using <code class="language-plaintext highlighter-rouge">-</code> as the <code class="language-plaintext highlighter-rouge">SRC_PATH</code> streams the contents of <code class="language-plaintext highlighter-rouge">STDIN</code> as a tar archive. The command extracts the content of the tar to the <code class="language-plaintext highlighter-rouge">DEST_PATH</code> in container’s filesystem. In this case, <code class="language-plaintext highlighter-rouge">DEST_PATH</code> must specify a directory. Using <code class="language-plaintext highlighter-rouge">-</code> as the <code class="language-plaintext highlighter-rouge">DEST_PATH</code> streams the contents of the resource as a tar archive to <code class="language-plaintext highlighter-rouge">STDOUT</code>.</p> <div class="_attribution">
  <p class="_attribution-p">
    &copy; 2019 Docker, Inc.<br>Licensed under the Apache License, Version 2.0.<br>Docker and the Docker logo are trademarks or registered trademarks of Docker, Inc. in the United States and/or other countries.<br>Docker, Inc. and other parties may also have trademark rights in other terms used herein.<br>
    <a href="https://docs.docker.com/engine/reference/commandline/cp/" class="_attribution-link">https://docs.docker.com/engine/reference/commandline/cp/</a>
  </p>
</div>