0

I have a windows-based docker container, run it and run an app inside it. But sometimes I need to add a folder to the image and run another app. How can I add recursively

\folder
 folder\*.*
\subfolder1
 subfolder1\*.*
\subfolder2
 subfolder2\*.*

from my host machine to my image?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
ZedZip
  • 5,794
  • 15
  • 66
  • 119
  • Can you build a derived image `FROM` the original image, `COPY`ing in the new files? Or use a bind mount when you launch the container? – David Maze Aug 29 '21 at 11:31
  • same question once more https://stackoverflow.com/questions/29939419/docker-copying-file-from-host-to-container – The Fool Aug 29 '21 at 18:47
  • another once https://stackoverflow.com/questions/22049212/docker-copying-files-from-docker-container-to-host – The Fool Aug 29 '21 at 18:48

1 Answers1

2

You can use docker cp:

The docker cp utility copies the contents of SRC_PATH to the DEST_PATH. You can copy from the container’s file system to the local machine or the reverse, from the local filesystem to the container.

docker cp /path/to/dir mycontainer:/path/in/container/

The cp command behaves like the Unix cp -a command in that directories are copied recursively with permissions preserved if possible.

Furthermore, as stated in the docs, if this is not feasible, you can use some hacks with tar like below.

tar Ccf $(dirname SRC_PATH) - $(basename SRC_PATH) | docker exec -i CONTAINER tar Cxf DEST_PATH -
The Fool
  • 16,715
  • 5
  • 52
  • 86
  • Any changes you make this way will be lost as soon as the container exits. – David Maze Aug 29 '21 at 11:30
  • Not, if you use commit. – The Fool Aug 29 '21 at 19:18
  • `docker commit` creates an image that you can't recreate when there's a mandatory critical security update in a year. It's really not a best practice at all. – David Maze Aug 30 '21 at 10:52
  • Yes, that's right. But it's still possible. I personally wouldn't do it either. I still see use cases for it. Maybe to save something critical to the business, because someone didn't follow best practices in the first place. Maybe docker CP can also be useful for debugging and development. Normally I would use it to get default configs out of the container but, hey, you never know what people are up to. – The Fool Aug 30 '21 at 11:54
  • I run the docker, it is active, >docker ps shows it, then trying to run PS D:\> docker cp d:/mc myxxx:c:/app/mc/ Error response from daemon: filesystem operations against a running Hyper-V container are not supported – ZedZip Aug 30 '21 at 14:17