
1、查看容器
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cd6957191c52 nginx "nginx -g 'daemon ..." hours ago Up hours 192.168.51.227:->/tcp webserver
2、使用容器名字webserver进行文件复制
3、从宿主机复制到容器,命令:docker cp ceshi.txt webserver:/home/
4、进入容器,查看文件:docker exec -it webserver bash
[root@localhost docker]# docker exec -it webserver bash
root@cd6957191c52:/# cat /home/ceshi.txt
hello,bob!
nice to meet you!
hello jack!
nice to meet you too!
5、修改容器中的文件,并复制到宿主机,命令:docker cp webserver:/home/ceshi.txt ./
root@cd6957191c52:/# cd /home/
root@cd6957191c52:/home# echo "this is my bike" >> ceshi.txt
root@cd6957191c52:/home# cat ceshi.txt
hello,bob!
nice to meet you!
hello jack!
nice to meet you too!
this is my bike
root@cd6957191c52:/home# exit
exit
[root@localhost docker]# docker cp webserver:/home/ceshi.txt ./
[root@localhost docker]# cat ceshi.txt
hello,bob!
nice to meet you!
hello jack!
nice to meet you too!
this is my bike
或者,把容器的名字改为容器的ID,获取容器ID的方法:
方法1、
[root@localhost docker]# docker inspect -f '{{.Id}}' webserver
cd6957191c52b25d29319b8ad450313931f2a8c730e4f1052704be957f8c573d
方法2、
[root@localhost docker]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cd6957191c52 nginx "nginx -g 'daemon ..." hours ago Up hours 192.168.51.227:->/tcp webserver
[root@localhost docker]# docker inspect -f '{{.Id}}' cd6957191c52
cd6957191c52b25d29319b8ad450313931f2a8c730e4f1052704be957f8c573d
用容器ID复制,如下:
[root@localhost docker]# docker inspect -f '{{.Id}}' webserver
cd6957191c52b25d29319b8ad450313931f2a8c730e4f1052704be957f8c573d
[root@localhost docker]# echo "abc">>ceshi.txt
[root@localhost docker]# cat ceshi.txt
hello,bob!
nice to meet you!
hello jack!
nice to meet you too!
this is my bike
abc
[root@localhost docker]# docker cp ceshi.txt cd6957191c52b25d29319b8ad450313931f2a8c730e4f1052704be957f8c573d:/home/
[root@localhost docker]# docker exec -it webserver bash
root@cd6957191c52:/# cd /home/
root@cd6957191c52:/home# cat ceshi.txt
hello,bob!
nice to meet you!
hello jack!
nice to meet you too!
this is my bike
abc
不用容器全ID也可以
[root@localhost docker]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cd6957191c52 nginx "nginx -g 'daemon ..." hours ago Up hours 192.168.51.227:->/tcp webserver
[root@localhost docker]# docker cp cd6957191c52:/home/ceshi.txt ./abc.txt
[root@localhost docker]# cat abc.txt
hello,bob!
nice to meet you!
hello jack!
nice to meet you too!
this is my bike
abc