linux - 如何通过 docker cp 复制目录?

标签 linux docker shell ubuntu docker-copy

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

$ docker cp -r <a-dir> <a-container-ID>:/destination/path
unknown shorthand flag: 'r' in -r
scp -r shell 命令我可以做到,所以我希望与 docker cp -r 相同.
如何在不使用压缩的情况下克服它 .tar文件和提取?

最佳答案

引用 docs :

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

-- 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

关于linux - 如何通过 docker cp 复制目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52737690/

相关文章:

linux - while 循环测试 bash 中是否存在具有给定模式的文件

php - require_once(../../path/to/script.php) 无法打开流权限被拒绝

linux - 用于内存和 CPU 使用的 bash 脚本

bash - 映射 id 的有效方法

linux - shell 脚本 : Check if file exists in directory from a list file

linux - 从控制台计算特定类型文件的数量

python - 在python中使用os.system()来执行linux命令

node.js - 在 Docker GitHub 操作中缓存 npm 包

docker - convoy-glusterfs相对于纯glusterfs有什么好处

bash - 如何在 Docker Postgres 的脚本中创建用户/数据库