1、下载一个docker镜像:我们可以通过登陆docker网站搜索自己需要的镜像,可以选择自己所需要的版本,然后通过详情也可以看到:网址:https://hub.docker.com/2、我们也可以通过命令行搜索:[root@web130 ~]# docker search centos:7 #如果不带版本号默认搜到是最新版本的:latestNAME DESCRIPTION STARS OFFICIAL AUTOMATEDcentos The official build of CentOS. 4726 [OK] ansible/centos7-ansible Ansible on Centos7 118 [OK]*我们看输出内容可以很容易的理解,分别是:名称,描述,星(类似github上的stars),是否官方镜像,是否自动装配。*如下搜索自动创建,以及指定星级的镜像,星级默认为0,如果加--no-trunc输出信息不截断显示,默认是否--no-trunc=true|fales[root@web130 ~]# docker search --automated -s 3 nginxFlag --automated has been deprecated, use --filter=is-automated=true insteadFlag --stars has been deprecated, use --filter=stars=3 insteadNAME DESCRIPTION .....省略显示 ...
1、images列出镜像:关于images更多信息可以通过man docker-images来查看*此内容下显示:来自那个仓库、标签信息、镜像ID、创建时间、镜像大小[root@web130 ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZE2、下载一个镜像,如果后边不跟版本号,默认是最新非稳定版本:[root@web130 ~]# docker pull ubuntu #下载镜像*也可以通过上边搜索选择合适的版本号,如下:[root@web130 ~]# docker pull ubuntu:14.04[root@web130 ~]# docker pull centos:7.4.1708[root@web130 ~]# docker images #查看下载的镜像,可以看到刚刚下载的是三个镜像REPOSITORY TAG IMAGE ID CREATED SIZEubuntu 14.04 c32fae490809 2 weeks ago 188MBubuntu latest cd6d8154f1e1 2 weeks ago 84.1MBcentos 7.4.1708 d3949e34634c 6 weeks ago 197MB[root@web130 ~]# *我们也可以跟上仓库地址下在,非官网镜像源仓库下载指定镜像:[root@web130 ~]# docker pull 仓库地址/ubuntu:14.04 3、利用该镜像创建一个容器:[root@web130 ~]# docker run -it centos:7.4.1708 /bin/bash #后边详细说明参数,如果本地没有下载此镜像,自动先下载镜像并运行*如下由于没有指定标签,自动下载了最新版本的centos,并打开了一个shell:[root@web130 ~]# docker run -it centos /bin/bashUnable to find image 'centos:latest' locallylatest: Pulling from library/centos256b176beaff: Pull complete Digest: sha256:6f6d986d425aeabdc3a02cb61c02abb2e78e57357e92417d6d58332856024fafStatus: Downloaded newer image for centos:latest[root@3e876fdaaf89 /]# lsanaconda-post.log bin dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var[root@3e876fdaaf89 /]# 4、使用tag命令为镜像添加标签[root@web130 ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEubuntu 14.04 c32fae490809 2 weeks ago 188MBubuntu latest cd6d8154f1e1 2 weeks ago 84.1MBcentos 7.4.1708 d3949e34634c 6 weeks ago 197MBcentos latest 5182e96772bf 6 weeks ago 200MB[root@web130 ~]# docker tag centos:latest mycentos:latest #我们可以看到多出来一个镜像,tag的作用类似与添加链接的作用,他们指向同一个源镜像文件、[root@web130 ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEubuntu 14.04 c32fae490809 2 weeks ago 188MBubuntu latest cd6d8154f1e1 2 weeks ago 84.1MBcentos 7.4.1708 d3949e34634c 6 weeks ago 197MBcentos latest 5182e96772bf 6 weeks ago 200MBmycentos latest 5182e96772bf 6 weeks ago 200MB5、使用inspect命令查看详细信息:[root@web130 ~]# docker inspect mycentos[root@web130 ~]# 6、使用history命令查看镜像历史[root@web130 ~]# docker history mycentos #镜像使用多层组成,列出各层的创建信息,--no-trunc选项输出完整的命令IMAGE CREATED CREATED BY SIZE COMMENT5182e96772bf 6 weeks ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B <missing> 6 weeks ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B <missing> 6 weeks ago /bin/sh -c #(nop) ADD file:6340c690b08865d7e… 200MB [root@web130 ~]#
1、通过标签删除镜像[root@web130 ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEubuntu 14.04 c32fae490809 2 weeks ago 188MBubuntu latest cd6d8154f1e1 2 weeks ago 84.1MBcentos 7.4.1708 d3949e34634c 6 weeks ago 197MBcentos latest 5182e96772bf 6 weeks ago 200MBmycentos latest 5182e96772bf 6 weeks ago 200MB[root@web130 ~]# docker rmi mycentos:latestUntagged: mycentos:latest[root@web130 ~]# docker images 删除。REPOSITORY TAG IMAGE ID CREATED SIZEubuntu 14.04 c32fae490809 2 weeks ago 188MBubuntu latest cd6d8154f1e1 2 weeks ago 84.1MBcentos 7.4.1708 d3949e34634c 6 weeks ago 197MBcentos latest 5182e96772bf 6 weeks ago 200MB #我们可以看到mycentos标签已经被删除,我们可以看到源镜像centos依然存在,所以删除标签不影响镜像文件,但是仅剩一个标签的时候要小心,会将镜像文件2、通过镜像ID删除镜像[root@web130 ~]# docker rmi centos:latest Error response from daemon: conflict: unable to remove repository reference "centos:latest" (must force) - container 3e876fdaaf89 is using its referenced image 5182e96772bf[root@web130 ~]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES3e876fdaaf89 centos "/bin/bash" 40 minutes ago Exited (0) 35 minutes ago friendly_haibt[root@web130 ~]# #显示无法删除,因为有一个基于它的容器存在,我们也可以强制删除 -f:docker rmi -f centos,但是不建议这样删除,所有我们如下操作先删除依赖的容器[root@web130 ~]# docker rm 3e876fdaaf89 #通过容器id删除镜像3e876fdaaf89[root@web130 ~]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES[root@web130 ~]# docker rmi centos:latest #可以看到它删除了这个镜像文件的所有层Untagged: centos:latestUntagged: centos@sha256:6f6d986d425aeabdc3a02cb61c02abb2e78e57357e92417d6d58332856024fafDeleted: sha256:5182e96772bf11f4b912658e265dfe0db8bd314475443b6434ea708784192892Deleted: sha256:1d31b5806ba40b5f67bde96f18a181668348934a44c9253b420d5f04cfb4e37a[root@web130 ~]#
创建镜像的方法:*基于已有的镜像容器创建*基于本地模板导入*基于Dockerfile创建 (后文重点学习Dockerfile)
1、基于已有镜像的容器创建[root@web130 ~]# docker run -it ubuntu:14.04 /bin/bashroot@5b117c5dffe1:/# touch yangltroot@5b117c5dffe1:/# exitexit[root@web130 ~]# docker images;REPOSITORY TAG IMAGE ID CREATED SIZEubuntu 14.04 c32fae490809 2 weeks ago 188MBubuntu latest cd6d8154f1e1 2 weeks ago 84.1MBcentos 7.4.1708 d3949e34634c 6 weeks ago 197MB[root@web130 ~]# docker commit -m "a new file" -a "Yanglt" 5b117c5dffe1 test:0.1sha256:40925d6bc8025b8ca6ec68249f4cb371844896a39817f9e9bf80eda4119e8d6f[root@web130 ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEtest 0.1 40925d6bc802 9 seconds ago 188MBubuntu 14.04 c32fae490809 2 weeks ago 188MBubuntu latest cd6d8154f1e1 2 weeks ago 84.1MBcentos 7.4.1708 d3949e34634c 6 weeks ago 197MB#可以看到一个新的镜像test2、基于本地模板导入创建下载地址:https://download.openvz.org/template/precreated/选择要下载镜像模板:[root@web130 ~]# wget https://download.openvz.org/template/precreated/centos-7-x86_64-minimal.tar.gz[root@web130 ~]# cat centos-7-x86_64-minimal.tar.gz |docker import - centos:7[root@web130 ~]# cat centos-7-x86_64-minimal.tar.gz |docker import - centos:7sha256:3458fe08f84de52d5354a80793e5f806420a2330fc2c2425bb4878bcb8b0efd8[root@web130 ~]# docker images #可以看到已经出现新的镜像REPOSITORY TAG IMAGE ID CREATED SIZEcentos 7 3458fe08f84d 54 seconds ago 435MBtest 0.1 40925d6bc802 About an hour ago 188MBubuntu 14.04 c32fae490809 2 weeks ago 188MBubuntu latest cd6d8154f1e1 2 weeks ago 84.1MBcentos 7.4.1708 d3949e34634c 6 weeks ago 197MB[root@web130 ~]#
1、导出镜像 -o + 要导出最后的文件名 + 要导出的镜像的标签(名) 或id [root@localhost ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEtest 0.1 40925d6bc802 20 minutes ago 188MBubuntu 14.04 c32fae490809 2 weeks ago 188MBubuntu latest cd6d8154f1e1 2 weeks ago 84.1MBcentos 7.4.1708 d3949e34634c 6 weeks ago 197MB[root@localhost ~]# [root@localhost ~]# docker save -o myubuntu_14.04.tar ubuntu:14.04[root@localhost ~]# ll |grep my*-rw------- 1 root root 197191680 9月 20 20:03 myubuntu_14.04.tar[root@localhost ~]# 2、导入镜像(恢复原有镜像)*我们可以将导出的源镜像删除,然后在导入[root@localhost ~]# docker rmi ubuntu:14.04[root@localhost ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEtest 0.1 40925d6bc802 29 minutes ago 188MBubuntu latest cd6d8154f1e1 2 weeks ago 84.1MBcentos 7.4.1708 d3949e34634c 6 weeks ago 197MB[root@localhost ~]# docker load < myubuntu_14.04.tar Loaded image: ubuntu:14.04[root@localhost ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEtest 0.1 40925d6bc802 29 minutes ago 188MBubuntu 14.04 c32fae490809 2 weeks ago 188MBubuntu latest cd6d8154f1e1 2 weeks ago 84.1MBcentos 7.4.1708 d3949e34634c 6 weeks ago 197MB[root@localhost ~]#
#我们可以把自己的镜像传到dockerhub官网上,前提是注册一个用户[root@localhost ~]# docker push image_name
本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728