【问题标题】:How to copy a directory over the docker cp?如何通过 docker cp 复制目录?
【发布时间】:2019-03-15 05:10:27
【问题描述】:

我想将目录从主机复制到容器,但我无法使用以下命令:

$ docker cp -r <a-dir> <a-container-ID>:/destination/path
unknown shorthand flag: 'r' in -r

使用scp -r shell 命令我可以做到,所以我希望docker cp -r 也一样。

如何在不使用压缩.tar 文件和提取的情况下克服它?

【问题讨论】:

    标签: linux docker shell ubuntu docker-copy


    【解决方案1】:

    引用docs

    cp 命令的行为类似于 Unix 的 cp -a 命令,因为目录被递归复制并尽可能保留权限。

    -- https://docs.docker.com/engine/reference/commandline/#extended-description

    所以docker cp 没有也不需要-r 标志。只是省略它。像这样:

    # Create a directory and put two files in it
    $ mkdir testdir && touch testdir/aaa testdir/bbb
    
    # Make sure files are there
    $ ls -la testdir
    total 384
    drwxr-xr-x 1 jannisbaratheon 197121 0 paź 10 15:15 ./
    drwxr-xr-x 1 jannisbaratheon 197121 0 paź 10 15:15 ../
    -rw-r--r-- 1 jannisbaratheon 197121 0 paź 10 15:15 aaa
    -rw-r--r-- 1 jannisbaratheon 197121 0 paź 10 15:15 bbb
    
    # Create container and fetch its ID
    $ export CONTAINER_ID=`docker run -di alpine:3.8 sh`
    
    # Make sure the target directory does not exist
    $ docker exec "$CONTAINER_ID" sh -c 'ls -la /containertestdir'
    ls: /containertestdir: No such file or directory
    
    # Copy the files with 'docker cp'
    $ docker cp testdir "$CONTAINER_ID":/containertestdir
    
    # Verify the files were copied by running 'ls -la' in the container
    $ docker exec "$CONTAINER_ID" sh -c 'ls -la /containertestdir'
    total 8
    drwxr-xr-x    2 root     root          4096 Oct 10 13:15 .
    drwxr-xr-x    1 root     root          4096 Oct 10 13:15 ..
    -rwxr-xr-x    1 root     root             0 Oct 10 13:15 aaa
    -rwxr-xr-x    1 root     root             0 Oct 10 13:15 bbb
    

    【讨论】:

    • 感谢您的完整解释+1
    【解决方案2】:

    默认情况下它是递归的。它会将您指向的命令复制到容器上的任何目标位置。

    无需指定递归。

    【讨论】:

      【解决方案3】:

      你可以像这样复制一个目录:

      docker cp /tmp/test mydocker:/tmp/test
      

      【讨论】:

        猜你喜欢
        • 2019-05-26
        • 1970-01-01
        • 1970-01-01
        • 2017-11-28
        • 2020-12-06
        • 1970-01-01
        • 1970-01-01
        • 2022-01-06
        • 1970-01-01
        相关资源
        最近更新 更多