测试工程师Docker基础 (4)

从容器内拷贝到主机

#docker cp 容器id:容器内路径 主机目的路径 #测试 #进入容器内部 [root@ecs-x-large-2-linux-20200305213344 ~]# docker exec -it cacf7a960d29 /bin/bash root@cacf7a960d29:/# ls bin dev docker-entrypoint.sh home lib64 mnt proc run srv tmp var boot docker-entrypoint.d etc lib media opt root sbin sys usr #新建一个文件 root@cacf7a960d29:/# echo "hello" >test.java root@cacf7a960d29:/# ls bin dev docker-entrypoint.sh home lib64 mnt proc run srv test.java usr boot docker-entrypoint.d etc lib media opt root sbin sys tmp var #退出容器 exit #拷贝 docker cp cacf7a960d29:test.java /home/ #验证是否copy到主机 [root@ecs-x-large-2-linux-20200305213344 ~]# ls /home/ test.java

commit镜像

命令:docker commit -m=“描述信息” -a=“作者” 容器id 目标镜像名:[tag] 实战测试: #1、启动tomcat默认镜像文件 [root@ecs-x-large-2-linux-20200305213344 ~]# docker run -d -p 3344:8080 bf4709e77b18 7984fe235242c4164dcede5053663a44db96ae89e0035a13e351faab03634d17 #2、查看容器的webapp文件 [root@ecs-x-large-2-linux-20200305213344 ~]# docker exec -it 7984fe235242c4164dcede5053663a44db96ae89e0035a13e351faab03634d17 /bin/bash root@7984fe235242:/usr/local/tomcat# ls BUILDING.txt LICENSE README.md RUNNING.txt conf logs temp webapps.dist CONTRIBUTING.md NOTICE RELEASE-NOTES bin lib native-jni-lib webapps work root@7984fe235242:/usr/local/tomcat# cd webapps root@7984fe235242:/usr/local/tomcat/webapps# ls #3、拷贝webapps.dist下文件到webapps下 root@7984fe235242:/usr/local/tomcat# cp -r webapps.dist/* webapps/ root@7984fe235242:/usr/local/tomcat# ls webapps ROOT docs examples host-manager manager root@7984fe235242:/usr/local/tomcat# #4、将该容器提交为一个新的镜像文件 [root@ecs-x-large-2-linux-20200305213344 ~]# docker commit -m="梵响测试容器提交为一个新的镜像" -a="fanxiang" 7984fe235242c4164dcede5053 tomcat01:1.0 sha256:3622a772f2315dc2d22140698a45cc8c30a9c8b5e7fb961937429307669ced92 [root@ecs-x-large-2-linux-20200305213344 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE tomcat01 1.0 3622a772f231 12 seconds ago 672 MB docker.io/hello-world latest d1165f221234 2 days ago 13.3 kB docker.io/redis 5.0 d00afcde654e 5 days ago 98.4 MB docker.io/redis latest f877e80bb9ef 5 days ago 105 MB docker.io/nginx latest 35c43ace9216 2 weeks ago 133 MB docker.io/tomcat latest bf4709e77b18 3 weeks ago 667 MB docker.io/centos latest 300e315adb2f 3 months ago 209 MB

save镜像

[root@ecs-x-large-2-linux-20200305213344 ~]#docker save --help Usage: docker save [OPTIONS] IMAGE [IMAGE...] Save one or more images to a tar archive (streamed to STDOUT by default) Options: --help Print usage -o, --output string Write to a file, instead of STDOUT 实战: [root@ecs-x-large-2-linux-20200305213344 ~]# docker save -o /home/redis01.tar docker.io/redis:5.0 [root@ecs-x-large-2-linux-20200305213344 ~]# ls /home redis01.tar test.java tomcat02.tar

load镜像

[root@ecs-x-large-2-linux-20200305213344 ~]# docker load --help Usage: docker load [OPTIONS] Load an image from a tar archive or STDIN Options: --help Print usage -i, --input string Read from tar archive file, instead of STDIN -q, --quiet Suppress the load output 实战: [root@ecs-x-large-2-linux-20200305213344 ~]# docker load -i /home/redis01.tar 01b7eeecc774: Loading layer [==================================================>] 24.7 MB/24.7 MB f2df42e57d5e: Loading layer [==================================================>] 1.536 kB/1.536 kB b537eb7339bc: Loading layer [==================================================>] 3.584 kB/3.584 kB Loaded image: docker.io/redis:5.0 [root@ecs-x-large-2-linux-20200305213344 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE docker.io/hello-world latest d1165f221234 2 days ago 13.3 kB docker.io/redis 5.0 d00afcde654e 5 days ago 98.4 MB docker.io/redis latest f877e80bb9ef 5 days ago 105 MB docker.io/nginx latest 35c43ace9216 2 weeks ago 133 MB docker.io/tomcat latest bf4709e77b18 3 weeks ago 667 MB docker.io/centos latest 300e315adb2f 3 months ago 209 MB 4、命令总结

![image-20210306164709448](/Users/zhaoyang/Library/Application Support/typora-user-images/image-20210306164709448.png)

四、docker镜像 1、镜像是什么 镜像是一种轻量级、可执行的独立软件包,用来打包软件运行环境和基于运行环境开发的软件,它包含了运行某个软件所需的所有内容,包括代码、运行时库、环境变量和配置问价等。 将所有的应用和环境直接打包成镜像,就可以直接运行。 2、镜像分层原理

docker的镜像实际上由一层一层的文件系统组成,这种层级的文件系统UnionFS。平时我们安装进虚拟机的CentOS都是好几个G,为什么Docker这里才200M?

对于个精简的OS,rootfs可以很小,只需要包合最基本的命令,工具和程序库就可以了,因为底层直接用 Host的kernel,自己只需要提供rootfs就可以了。由此可见对于不同的Linux发行版, boots基本是一致 的, rootfs会有差別,因此不同的发行版可以公用bootfs. 虚拟机是分钟级别,容器是秒级! 3、分层理解 [root@ecs-x-large-2-linux-20200305213344 ~]# docker pull redis:5.0 Trying to pull repository docker.io/library/redis ... 5.0: Pulling from docker.io/library/redis 45b42c59be33: Already exists 5ce2e937bf62: Already exists 2a031498ff58: Already exists ec50b60c87ea: Pull complete 2bf0c804a5c0: Pull complete 6a3615492950: Pull complete Digest: sha256:6ba62effb31d8d74e6e2dec4b7ef9c8985e7fcc85c4f179e13f622f5785a4135 Status: Downloaded newer image for docker.io/redis:5.0

docker镜像为什么要采用这种分层的结构呢?

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zyffwy.html