2

Suppose I have the following directory structure

parent-dir
+ child-dir
  + grandchild-file-a
  + grandchild-file-b
uncle-dir
  .
  ..

Now on unix - if I run the command

parent-dir> cp child-dir ../uncle-dir

Then I expect to get

uncle-dir
+ child-dir
  + grandchild-file-a
  + grandchild-file-b

But if I repeat this with docker cp then I get

uncle-dir
+ grandchild-file-a
+ grandchild-file-b

Which might be reasonable on unix if I had done:

parent-dir> cp child-dir/* ../uncle-dir

My question is: What are the reasons Docker cp works differently to Unix cp?

hawkeye
  • 34,745
  • 30
  • 150
  • 304
  • suppose it could be because of this behavior http://stackoverflow.com/questions/37455454/docker-cp-the-content-of-a-folder/37457401#37457401 – Zeromus May 27 '16 at 12:24
  • @Zeromus That's not "a behavior" and it doesn't answer the question. – Etan Reisner May 27 '16 at 12:40
  • 2
    You appear to have an alias like `cp=cp -r` set. By default, `cp` by itself won't copy a directory. – chepner May 27 '16 at 12:43
  • the question in the link is about copying all content of a folder in another but docker cp doesnt accept * wildcard. that's a behavior and if what @hawkeye described is true then that resolve the problem – Zeromus May 27 '16 at 12:45
  • @Zeromus But that question did link, eventually, to the docker docs which did explain the problem. – Etan Reisner May 27 '16 at 12:45
  • @Zeromus No, it gives a workaround for avoiding the `*` (that docker apparently doesn't like) it doesn't explain *why* `docker cp` is doing what it is doing. – Etan Reisner May 27 '16 at 12:46

1 Answers1

3

The docker docs on cp explain this:

  • SRC_PATH specifies a directory
    • DEST_PATH does not exist
      • DEST_PATH is created as a directory and the contents of the source directory are copied into this directory
    • DEST_PATH exists and is a file
      • Error condition: cannot copy a directory to a file
    • DEST_PATH exists and is a directory
      • SRC_PATH does not end with /.
        • the source directory is copied into this directory
      • SRC_PATH does end with /.
        • the content of the source directory is copied into this directory

So either DEST_PATH does not exist or it does and your docker cp command is actually docker cp child-dir/ uncle-dir (with the trailing slash).

Etan Reisner
  • 77,877
  • 8
  • 106
  • 148
  • NOTE: 1. As of 2023/07/21 the behavior and documentation has been updated 2. `/.` slash followed by dot is important https://docs.docker.com/engine/reference/commandline/cp/#description – yue Jul 21 '23 at 06:21