"docker cp" all files from a folder to existing container folder

Docker

Docker Problem Overview


Am I missing something when I try to copy files from one folder to a existing container folder:

Doc

I want to copy files within the build(host) folder to html in my container:

docker cp ./src/build b081dbbb679b:/usr/share/nginx/html

To be clear; I need to copy from host to container.

But it copies the whole build folder and copies it to ..html/build

I just need the files(and sub folders) within the build folder to be copied into ..html.

Am I missing something or do I have to copy each file one by one?

Docker Solutions


Solution 1 - Docker

Here is the explanation from the doc on how to use the cp command in docker, which will fix your issue with /. at the end of SRC_PATH:

> * SRC_PATH does not end with /. (that is: slash followed by dot) > * the source directory is copied into this directory > > * SRC_PATH does end with /. (that is: slash followed by dot) > * the content of the source directory is copied into this directory

Solution 2 - Docker

Copy files/folders between a container and the local filesystem is like below formats:

  • Copy file to folder inside container:

     docker cp ./src/build/index.html ContainerName:/app/
    

above example shows index.html file is copying to app folder inside container

  • Copy all files to folder inside container:

     docker cp ./src/build/. ContainerName:/app/
    

above example shows all files inside build folder are copying to app folder inside container

Solution 3 - Docker

Just use "." (Dot - instead of "*" in Linux) after your file path, it will copy entire files from the folder

docker cp /file/path/* containerId:/filepath/nginx/html/

docker cp /file/path/. containerId:/filepath/nginx/html/

Solution 4 - Docker

In Windows, you can iterate through all txt files in a folder and copy them into the container with this command:

for /r %i in (*.txt) do docker cp %i containerId:/blabla/bla/

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChris G.View Question on Stackoverflow
Solution 1 - DockerBMWView Answer on Stackoverflow
Solution 2 - DockerSina LotfiView Answer on Stackoverflow
Solution 3 - DockerMuhammed FasilView Answer on Stackoverflow
Solution 4 - DockerPietro FioView Answer on Stackoverflow