docker cp 的作用和linux 的cp是一样的,用来在宿主机和容器之间复制文件
[[email protected] ~]# docker cp --help Usage: docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|- docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH Copy files/folders between a container and the local filesystem Options: -a, --archive Archive mode (copy all uid/gid information) -L, --follow-link Always follow symbol link in SRC_PATH
-a复制所有的信息包括用户名和用户组
使用方法上面写得很清楚了
docker cp 选项 源路径 目标路径,如果是容器,需要写 容器名:绝对路径,容器名可以用id来代替
示范:刚才已创建了ubuntu容器,不会的看前面的日志
[[email protected] ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7103a04f97d3 ubuntu:14.04 "/bin/bash" 2 hours ago Up 2 hours ubuntu [[email protected] ~]# docker exec -it ubuntu bash [email protected]:/# pwd / [email protected]:/# cd home/ [email protected]:/home# pwd /home
在容器的/home文件夹下面随便写点什么东西 [email protected]:/home# echo "It is a docker space" > test.txt [email protected]:/home# ls test.txt [email protected]:/home# cat test.txt It is a docker space
退出容器,别忘了,要用ctrl+p,然后ctlr+q
[email protected]:/home# read escape sequence [[email protected] ~]#
从容器里面复制文件到本地
[[email protected] ~]# docker cp ubuntu:/home/test.txt ./test1.txt [[email protected] ~]# ls anaconda-ks.cfg initial-setup-ks.cfg test1.txt 公共 模板 视频 图片 文档 下载 音乐 桌面 [[email protected] ~]# cat test1.txt It is a docker space
容器名可以用id号来代替 [[email protected] ~]# docker cp 7103:/home/test.txt ./test2.txt [[email protected] ~]# ls anaconda-ks.cfg initial-setup-ks.cfg test1.txt test2.txt 公共 模板 视频 图片 文档 下载 音乐 桌面 [[email protected] ~]# cat test2.txt It is a docker space
现在反过来,从真机复制文件到容器
[[email protected] ~]# echo "It is a real space" > rtest.txt [[email protected] ~]# cat rtest.txt It is a real space [[email protected] ~]# docker cp rtest.txt ubuntu:/home [[email protected] ~]# docker cp rtest.txt 7103a:/home/rtest2.txt [[email protected] ~]# [[email protected] ~]# docker exec -it ubuntu bash [email protected]:/# cd home/ [email protected]:/home# ls rtest.txt rtest2.txt test.txt [email protected]:/home# cat rtest.txt rtest2.txt It is a real space It is a real space [email protected]:/home#
其实真机到容器的方法太多了,可以挂载数据卷,也可以用cp,还可以直接echo,这些后面再演示