先总结,后面附有实际测试实例:
·DEST_PATH(目标目录)存在时,复制正常;
·DEST_PATH(目标目录)不存在时,要区分以下情况:
1、复制文件
DEST_PATH不存在会复制失败;
2、复制目录
这里先说明一下,网上有说一种错误说法,复制目录时需要看SRC_PATH(源目录)有没有/结尾,不同情况如下,但是实际测试并不是:
SRC_PATH不以/结尾。 (即:斜线后跟点) 将源目录复制到此目录
SRC_PATH以/结尾。 (即:斜线后跟点) 将源目录的内容复制到此目录
实际上SRC_PATH有没有/结尾都是复制整个目录到目标目录下。
2.1、单层目录不存在
将会直接复制为DEST_PATH(不是在DEST_PATH下创建多一层目录,可以用于直接复制创建新目录的场景)
2.2 多层目录不存在
DEST_PATH多层目录不存在会复制失败。
实例测试:
[root@centos-server ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0b51220058b9 nginx:v3 "/docker-entrypoint.…" 2 hours ago Up 2 hours 0.0.0.0:8081->80/tcp, :::8081->80/tcp
[root@centos-server ~]# pwd
/root
[root@centos-server ~]# cat H-test/123.txt
123
[root@centos-server ~]# docker exec -it 0b51220058b9 /bin/bash
root@0b51220058b9:/#
root@0b51220058b9:/# cat /root/docker-test
cat: /root/docker-test: No such file or directory
---测试一,SRC_PATH以/结尾(目标目录-单层不存在)
[root@centos-server ~]# docker cp /root/H-test/ 0b51220058b9:/root/docker-test
[root@centos-server ~]#
...
root@0b51220058b9:/# cat /root/docker-test/123.txt
123
---测试二,SRC_PATH不以/结尾(目标目录-单层不存在)
root@0b51220058b9:/# rm -rf /root/docker-test/
root@0b51220058b9:/#
root@0b51220058b9:/# ls /root/docker-test/
ls: cannot access '/root/docker-test/': No such file or directory
...
[root@centos-server ~]# docker cp /root/H-test 0b51220058b9:/root/docker-test
...
root@0b51220058b9:/# ls /root/docker-test/
123.txt
---测试三,DEST_PATH目标目录多层不存在
root@0b51220058b9:/# ls /root/docker-test/index/folder/
ls: cannot access '/root/docker-test/index/folder/': No such file or directory
...
[root@centos-server ~]# docker cp /root/H-test 0b51220058b9:/root/docker-test/index/folder/
Error: No such container:path: 0b51220058b9:/root/docker-test/index
[root@centos-server ~]# docker cp /root/H-test/ 0b51220058b9:/root/docker-test/index/folder/
Error: No such container:path: 0b51220058b9:/root/docker-test/index
[root@centos-server ~]# docker cp /root/H-test/123.txt 0b51220058b9:/root/docker-test/index/folder/
no such directory
...
root@0b51220058b9:/# ls /root/docker-test/index/folder/
ls: cannot access '/root/docker-test/index/folder/': No such file or directory
(结论:目标目录不存在,如果是单层不存在,则会创建,多重目录不存在,复制失败)
---测试四,DEST_PATH目标目录存在
root@0b51220058b9:/# mkdir -p /root/docker-test/index/folder/
root@0b51220058b9:/#
root@0b51220058b9:/# ls /root/docker-test/index/folder/
root@0b51220058b9:/#
...
[root@centos-server ~]# docker cp /root/H-test 0b51220058b9:/root/docker-test/index/folder/
[root@centos-server ~]#
...
root@0b51220058b9:/# ls /root/docker-test/index/folder/
H-test
root@0b51220058b9:/# cat /root/docker-test/index/folder/H-test/123.txt
123
# DEST_PATH存在且是一个目录,如果
# SRC_PATH不以/结尾。 (即:斜线后跟点) 将源目录复制到此目录
root@0b51220058b9:/# rm -rf /root/docker-test/index/folder/H-test/
root@0b51220058b9:/# ls /root/docker-test/index/folder/
root@0b51220058b9:/#
...
[root@centos-server ~]# docker cp /root/H-test/ 0b51220058b9:/root/docker-test/index/folder/
[root@centos-server ~]#
...
root@0b51220058b9:/# ls /root/docker-test/index/folder/
H-test
root@0b51220058b9:/#
root@0b51220058b9:/# cat /root/docker-test/index/folder/H-test/123.txt
123
# DEST_PATH存在且是一个目录,如果
# SRC_PATH以/结尾。 (即:斜线后跟点) 还是将源目录复制到此目录
---文本复制测试
root@0b51220058b9:/# rm -rf /root/docker-test/index/folder/H-test/
...
[root@centos-server ~]# docker cp /root/H-test/123.txt 0b51220058b9:/root/docker-test/index/folder/
[root@centos-server ~]#
...
root@0b51220058b9:/# ls /root/docker-test/index/folder/
123.txt
root@0b51220058b9:/# cat /root/docker-test/index/folder/123.txt
123
...
[root@centos-server ~]# docker cp /root/H-test/123.txt 0b51220058b9:/root/docker-test/456.txt
[root@centos-server ~]#
...
root@0b51220058b9:/# cat /root/docker-test/456.txt
123