docker 系列3 获取和查看镜像
一.获取镜像
镜像是docker三大核心概念中最重要的,dokcer运行容器前需要本地存在对应的镜像,如果镜像不存在,docker会尝试先从默认镜像仓库下载(docker hub),用户也可以通过配置,使用自定义的镜像仓库。
镜像是运行容器的前提,官方的docker hub网站已经提供了数十万个镜像供大家开放下载。可以使用docker [image] pull命令直接从docker hub镜像源来下载镜像,该命令格式为
docker [image] pull Name[:TAG]
其中Name是镜像仓库名称(用来区分镜像),TAG是镜像的标签(往往用来表示版本信息),通常情况下,描述一个镜像需要包括 "名称+标签"信息。
文档:https://docs.docker.com/engine/reference/commandline/image_pull/
例如:获取一个hello-world的镜像(docker hub上可以找到):
[root@VM_0_12_centos ~]# docker pull hello-world:latest latest: Pulling from library/hello-world Digest: sha256:8e3114318a995a1ee497790535e7b88365222a21771ae7e53687ad76563e8e76 Status: Image is up to date for hello-world:latest
如果不指定TAG,默认会选择Latest标签。
docker pull hello-world 命令相当于docker pull registry.hub.docker.com/hello-world命令,如果多个仓库,镜像名称相同,需要指定仓库地址,假设从网易的镜像源来下载hello-world,可以使用如下命令: docker pull hub.c.163.com/public/hello-world
pull 子命令支持的选择主要包括:
-a, --all-tags=true|false :是否获取仓库中的所有镜像,默认为否;
--disable-content-trust :取消镜像的内容校验,默认为真
另外,有时需要使用镜像代理服务来加速docker镜像获取过程,可以在docker服务启动配置中增加 --registry-mirror=proxy_url 来指定镜像代理服务地址(如官方中国加速器https://registry.docker-cn.com)
最后下载到本地后,就可以使用镜像了。
[root@VM_0_12_centos ~]# docker run -it hello-world
Hello from Docker!
二.查看镜像
主要介绍docker镜像的 ls,tag,inspect子命令
1.使用images命令列出镜像
使用docker images或docker image ls命令可以列出本地主机上已有镜像的基本信息:
[root@VM_0_12_centos ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest bf756fb1ae65 3 months ago 13.3kB hello-world latest bf756fb1ae65 3 months ago 13.3kB
列出信息中,可以看出如下信息:
来自于哪个仓库
镜像的标签信息
镜像的ID,如果两个镜像的ID相同,说明它们实际上指向了同一个镜像,相同的镜像层本地只会存储一份。
创建时间,说明镜像最后的更新时间
镜像大小,优秀的镜像往往体积都比较小
2.使用tag命令添加镜像标签
文档:https://docs.docker.com/engine/reference/commandline/image_tag/
为了方便在后续工作中使用特定镜像,还可以用docker tag命令来为本地镜像任意添加新的标签,例如,添加一个新的myhello-world:latest镜像标签,添加后再次查看imges本机,多出一个。之后就可以直接使用myhello-world:latest来表示这个镜像了。
[root@VM_0_12_centos ~]# docker tag hello-world:latest myhello-world:latest [root@VM_0_12_centos ~]# docker images --all REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest bf756fb1ae65 3 months ago 13.3kB hello-world latest bf756fb1ae65 3 months ago 13.3kB myhello-world latest bf756fb1ae65 3 months ago 13.3kB
注意这里的myhello-world:latest镜像的ID跟hello-world:latest是完全一致的,它们实际上指向同一个镜像文件,只是别名不同而已,docker tag命令添加的标签实际上起到了类似链接的作用。
3.使用inspect命令查看详细信息
文档:https://docs.docker.com/engine/reference/commandline/image_inspect/
使用docker [image] inspect命令可以获取该镜像的详细信息,包括作者,适应架构,各层的数字摘要等:
[root@VM_0_12_centos ~]# docker inspect hello-world [ { "Id": "sha256:bf756fb1ae65adf866bd8c456593cd24beb6a0a061dedf42b26a993176745f6b", "RepoTags": [ "hello-world:latest", "hello-world:latest", "myhello-world:latest" ], "RepoDigests": [ "hello-world@sha256:8e3114318a995a1ee497790535e7b88365222a21771ae7e53687ad76563e8e76", "hello-world@sha256:8e3114318a995a1ee497790535e7b88365222a21771ae7e53687ad76563e8e76" ], "Parent": "", "Comment": "", "Created": "2020-01-03T01:21:37.263809283Z", "Container": "71237a2659e6419aee44fc0b51ffbd12859d1a50ba202e02c2586ed999def583", "ContainerConfig": { "Hostname": "71237a2659e6", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "Tty": false, "OpenStdin": false, "StdinOnce": false, "Env": [ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" ], "Cmd": [ "/bin/sh", "-c", "#(nop) ", "CMD [\"/hello\"]" ], "ArgsEscaped": true, "Image": "sha256:eb850c6a1aedb3d5c62c3a484ff01b6b4aade130b950e3bf3e9c016f17f70c34", "Volumes": null, "WorkingDir": "", "Entrypoint": null, "OnBuild": null, "Labels": {} }, "DockerVersion": "18.06.1-ce", "Author": "", "Config": { "Hostname": "", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "Tty": false, "OpenStdin": false, "StdinOnce": false, "Env": [ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" ], "Cmd": [ "/hello" ], "ArgsEscaped": true, "Image": "sha256:eb850c6a1aedb3d5c62c3a484ff01b6b4aade130b950e3bf3e9c016f17f70c34", "Volumes": null, "WorkingDir": "", "Entrypoint": null, "OnBuild": null, "Labels": null }, "Architecture": "amd64", "Os": "linux", "Size": 13336, "VirtualSize": 13336, "GraphDriver": { "Data": { "MergedDir": "/var/lib/docker/overlay2/bb03c3373078f475b3aafcc0dcda7167a877b3db3a05f3192321bebeae5b0999/merged", "UpperDir": "/var/lib/docker/overlay2/bb03c3373078f475b3aafcc0dcda7167a877b3db3a05f3192321bebeae5b0999/diff", "WorkDir": "/var/lib/docker/overlay2/bb03c3373078f475b3aafcc0dcda7167a877b3db3a05f3192321bebeae5b0999/work" }, "Name": "overlay2" }, "RootFS": { "Type": "layers", "Layers": [ "sha256:9c27e219663c25e0f28493790cc0b88bc973ba3b1686355f221c38a36978ac63" ] }, "Metadata": { "LastTagTime": "2020-04-30T15:41:26.712469853+08:00" } } ]
4.使用history命令查看镜像历史
文档:https://docs.docker.com/engine/reference/commandline/image_history/
由于镜像文件由多个层组成,那么怎么知道各个层的内容具体是什么呢?这时候可以使用history子命令,该命令列出各层的创建信息:
[root@VM_0_12_centos ~]# docker history hello-world IMAGE CREATED CREATED BY SIZE COMMENT bf756fb1ae65 3 months ago /bin/sh -c #(nop) CMD ["/hello"] 0B <missing> 3 months ago /bin/sh -c #(nop) COPY file:7bf12aab75c3867a… 13.3kB
image id过长自动截断了,可以使用前面提到的--no-trunc选项来输出完整命令:
[root@VM_0_12_centos ~]# docker history --no-trunc hello-world IMAGE CREATED CREATED BY SIZE COMMENT sha256:bf756fb1ae65adf866bd8c456593cd24beb6a0a061dedf42b26a993176745f6b 3 months ago /bin/sh -c #(nop) CMD ["/hello"] 0B <missing> 3 months ago /bin/sh -c #(nop) COPY file:7bf12aab75c3867a023fe3b8bd6d113d43a4fcc415f3cc27cbcf0fff37b65a02 in / 13.3kB
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 一文读懂知识蒸馏
· 终于写完轮子一部分:tcp代理 了,记录一下