{"id":331099,"date":"2021-08-27T14:30:56","date_gmt":"2021-08-27T11:30:56","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-use-docker-cp-to-copy-files-between-host-and-containers-cloudsavvy-it\/"},"modified":"2021-08-27T14:30:56","modified_gmt":"2021-08-27T11:30:56","slug":"how-to-use-docker-cp-to-copy-files-between-host-and-containers-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-use-docker-cp-to-copy-files-between-host-and-containers-cloudsavvy-it\/","title":{"rendered":"#How to Use Docker Cp to Copy Files Between Host and Containers \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a3be3c2b5080\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a3be3c2b5080\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-docker-cp-to-copy-files-between-host-and-containers-cloudsavvy-it\/#Basic_Syntax\" >Basic Syntax<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-docker-cp-to-copy-files-between-host-and-containers-cloudsavvy-it\/#Copying_Entire_Directories\" >Copying Entire Directories<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-docker-cp-to-copy-files-between-host-and-containers-cloudsavvy-it\/#Copy_Behavior\" >Copy Behavior<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-docker-cp-to-copy-files-between-host-and-containers-cloudsavvy-it\/#Command_Limitations\" >Command Limitations<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-docker-cp-to-copy-files-between-host-and-containers-cloudsavvy-it\/#Using_Bind_Mounts_to_Copy_Files\" >Using Bind Mounts to Copy Files<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-docker-cp-to-copy-files-between-host-and-containers-cloudsavvy-it\/#What_About_COPY_in_Dockerfiles\" >What About COPY in Dockerfiles?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-docker-cp-to-copy-files-between-host-and-containers-cloudsavvy-it\/#When_to_Copy_Files_With_Docker\" >When to Copy Files With Docker?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-docker-cp-to-copy-files-between-host-and-containers-cloudsavvy-it\/#Summary\" >Summary<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#How to Use Docker Cp to Copy Files Between Host and Containers \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage aligncenter size-full wp-image-9034\" srcset=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/01\/6dc7b5a0.jpeg?width=398&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 400w, https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/01\/6dc7b5a0.jpeg?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 1200w\" sizes=\"auto, 400w, 1200w\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/01\/6dc7b5a0.jpeg?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"1602\" height=\"902\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Need to get some files into or out of a Docker container? The <code>docker cp<\/code> command lets you copy between host and container filesystems so you can add config details, create backups, and restore existing data.<\/p>\n<h2 id=\"basic-syntax\"><span class=\"ez-toc-section\" id=\"Basic_Syntax\"><\/span>Basic Syntax<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><code>docker cp<\/code> accepts source and destination paths as its two arguments:<\/p>\n<pre>docker cp example.txt my-container:\/example.txt<\/pre>\n<p>Here <code>example.txt<\/code> is being copied from your working directory to <code>\/example.txt<\/code> in the <code>my-container<\/code> container. You could reverse the two arguments to copy <code>\/example.txt<\/code> out of the container and into your working directory.<\/p>\n<p>The argument referencing the container path needs to be prefixed with a container ID or name followed by a colon (<code>:<\/code>). You can find the ID or name of a running container with <code>docker ps<\/code>.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-13959\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/08\/21179279.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"1136\" height=\"353\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Each <code>docker cp<\/code> command needs one local filesystem path and one container path \u2013 you can\u2019t directly copy between two containers. Use a multi-step procedure if you need to do this, copying first from the source container to your filesystem, then from the new local path into the target container.<\/p>\n<h2 id=\"copying-entire-directories\"><span class=\"ez-toc-section\" id=\"Copying_Entire_Directories\"><\/span>Copying Entire Directories<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><code>docker cp<\/code> can recursively copy directories too:<\/p>\n<pre>docker cp \/home\/demo\/website apache-container:\/var\/www\/html\/.<\/pre>\n<p>Docker will copy everything in <code>\/home\/demo\/website<\/code> and transfer it into <code>\/var\/www\/html<\/code>.<\/p>\n<h2 id=\"copy-behavior\"><span class=\"ez-toc-section\" id=\"Copy_Behavior\"><\/span>Copy Behavior<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>When you\u2019re copying a file, Docker creates a new file at the destination if it doesn\u2019t already exist. Existing files are overwritten with the new content. When the destination\u2019s a directory, the file gets copied into it using the source filename. An exception is when the specified destination ends with a <code>\/<\/code>, denoting a directory, but the path does not already exist. In this scenario an error will be raised.<\/p>\n<p>The process is a little more complicated for directory copies. A new directory will be created at the destination with the contents of the source directory, if the destination path doesn\u2019t already exist. When it does exist, the behavior differs depending on whether you\u2019ve included a trailing <code>\/.<\/code> component in the path.<\/p>\n<ul>\n<li><strong><code>\/.<\/code><\/strong> is present \u2013 The source <em>directory<\/em> is copied into the existing destination directory.<\/li>\n<li><strong><code>\/.<\/code><\/strong> is not present \u2013 The <em>content<\/em> of the source directory is copied into the destination.<\/li>\n<\/ul>\n<p>The subtle distinction dictates whether a new subdirectory is created inside the destination.<\/p>\n<h2 id=\"command-limitations\"><span class=\"ez-toc-section\" id=\"Command_Limitations\"><\/span>Command Limitations<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Despite its name, <code>docker cp<\/code> is not a complete implementation of the <code>cp<\/code> shell command. The <code>cp<\/code> flags are not supported, except for <code>-a<\/code> and <code>-L<\/code>:<\/p>\n<ul>\n<li><strong><code>-a<\/code><\/strong> \u2013 Archival mode, which preserves user and group details on copied files.<\/li>\n<li><strong><code>-L<\/code><\/strong> \u2013 Follow symlinks in the source directory to copy the contents of link targets, rather than the links themselves.<\/li>\n<\/ul>\n<p>For more advanced use cases where selective copying is required, you\u2019ll need to fallback to using a different <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">app<\/a>roach.<\/p>\n<h2 id=\"using-bind-mounts-to-copy-files\"><span class=\"ez-toc-section\" id=\"Using_Bind_Mounts_to_Copy_Files\"><\/span>Using Bind Mounts to Copy Files<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Docker volumes provide another way of moving files between containers and your host. Bind mounting a local directory into a container lets you access its contents from your host filesystem, removing the need to use <code>docker cp<\/code>.<\/p>\n<pre>docker run -v \/example\/host\/directory:\/container\/path my-image:latest<\/pre>\n<p>The contents of the <code>\/example\/host\/directory<\/code> path are mounted into the container\u2019s filesystem at <code>\/container\/path<\/code>. You can interact with these files outside of Docker using familiar tools such as <code>cp<\/code>, <code>rsync<\/code>, and your graphical file browser.<\/p>\n<p>This technique is only useful when you\u2019re working with a single container directory. It doesn\u2019t work well when you\u2019re copying from arbitrary locations as you need to know the paths you\u2019ll be using ahead of time, when the container is created.<\/p>\n<p>You should also be wary of filesystem permissions: files created within the container will usually be owned by <code>root<\/code>. This can create awkward scenarios on the host where you\u2019re unable to edit or delete files inside the bound directory. Use the <code>chown<\/code> command on the host and inside the container to switch the ownership depending on environment if necessary.<\/p>\n<h2 id=\"what-about-copy-in-dockerfiles\"><span class=\"ez-toc-section\" id=\"What_About_COPY_in_Dockerfiles\"><\/span>What About <code>COPY<\/code> in Dockerfiles?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><code>docker cp<\/code> can sometimes be confused with the <code>COPY<\/code> instruction in Dockerfiles. It\u2019s important to recognize that these two features serve very different use cases.<\/p>\n<p><code>COPY<\/code> can\u2019t be used to move files between your host and a running container. It\u2019s for getting files into <em>images<\/em> during the build process:<\/p>\n<pre>COPY \/home\/me\/my-website \/var\/www\/html\/.<\/pre>\n<p>Here website source code gets copied into an image as part of a build. This is a one-time process. Every container started from the image would include the website source as it was at the time you ran <code>docker build<\/code>.<\/p>\n<p><code>docker cp<\/code> lets you replace that source code with a newer version once a container is running. <code>COPY<\/code> instructions are for making files part of a static image; <code>cp<\/code> commands interact with live containers.<\/p>\n<h2 id=\"when-to-copy-files-with-docker\"><span class=\"ez-toc-section\" id=\"When_to_Copy_Files_With_Docker\"><\/span>When to Copy Files With Docker?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Manually copying files from your host to a Docker container, or vice versa, should be a relatively rare occurrence. Images are meant to be self-sufficient so they should come with everything you need to start an instance. Configuration is usually handled via environment variables.<\/p>\n<p>Containers which need to store data persistently should be using Docker volumes. Volumes allow data to outlive any single container so you don\u2019t need to manually <code>docker cp<\/code> before replacing an instance. When you\u2019re making backups, copy the volumes from your host, instead of pulling files out of containers.<\/p>\n<p><code>docker cp<\/code> is most useful when debugging containers or working in a development environment. Sometimes you need to manually inject a temporary config file or pull out a buried log. Using <code>docker cp<\/code> is quicker and more convenient than rebuilding the entire image each time you make a code change.<\/p>\n<p>Always remember that files copied <em>into<\/em> containers will only persist as long as the container lives. Starting another container from the same image will give you a clean slate, without the files you added with <code>docker cp<\/code>.<\/p>\n<h2 id=\"summary\"><span class=\"ez-toc-section\" id=\"Summary\"><\/span>Summary<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><code>docker cp<\/code> lets you move files between your host and your Docker containers. It works with files and directories but lacks most of the advanced functionality in the shell-based <code>cp<\/code> command.<\/p>\n<p>Regular use of <code>docker cp<\/code> indicates a potential departure from container best practices. It\u2019s wise to treat it as a convenience tool for development use, rather than an integral part of working with containers. Long-term file persistence should be implemented with volumes as these are first-class components in the Docker ecosystem.\n<\/div>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/13987\/how-to-use-docker-cp-to-copy-files-between-host-and-containers\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#How to Use Docker Cp to Copy Files Between Host and Containers \u2013 CloudSavvy IT&#8221; Need to get some files into or out of a Docker container? The docker cp command lets you copy between host and container filesystems so you can add config details, create backups, and restore existing data. Basic Syntax docker cp&#8230;<\/p>\n","protected":false},"author":1,"featured_media":331100,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/01\/6dc7b5a0.jpeg","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-331099","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/331099","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=331099"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/331099\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/331100"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=331099"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=331099"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=331099"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}