代码之家  ›  专栏  ›  技术社区  ›  Benyamin Jafari

如何在docker cp上复制目录?

  •  0
  • Benyamin Jafari  · 技术社区  · 6 年前

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

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

    scp -r docker cp -r 我也是。

    3 回复  |  直到 6 年前
        1
  •  5
  •   jannis Anjali    6 年前

    引用 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
    
        2
  •  2
  •   destan40    6 年前

    您可以复制如下目录:

    docker cp /tmp/test mydocker:/tmp/test
    
        3
  •  1
  •   OK sure    6 年前

    不需要指定递归。