docker容器與宿主交互數據


1、查看容器

[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                         NAMES
cd6957191c52        nginx               "nginx -g 'daemon ..."   8 hours ago         Up 7 hours          192.168.51.227:9999->80/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 ..."   8 hours ago         Up 7 hours          192.168.51.227:9999->80/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 ..."   8 hours ago         Up 7 hours          192.168.51.227:9999->80/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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM