docker系列3:docker镜像基本命令

本文介绍了Docker的基本命令,包括显示Docker信息、版本、拉取与查看镜像、删除镜像以及搜索镜像。通过示例展示了如何使用dockerinfo、dockerversion、dockerpull、dockerimages、dockerrmi和dockersearch等命令。还提到了使用-f参数自定义输出格式和通过-dangling过滤器删除无用镜像。
摘要由CSDN通过智能技术生成

目录

传送门

docker的基本命令

镜像基本命令

显示docker信息

显示Docker版本信息

拉取镜像

查看镜像

删除镜像

搜索镜像


传送门

前面介绍了docker的安装:docker系列1:docker安装

还有docker镜像加速器:docker系列2:阿里云镜像加速器

工欲善其事必先利其器,现在环境已经安装好了,下面就来看看怎么操作docker!

docker的基本命令

镜像基本命令

在前面docker系列2:阿里云镜像加速器提到了,docker命令的官方指南:Comman-line reference

学习docker命令入门的一个比较好的方式,就是先通过官方文档来查看命令并练习,后期针对性的体系化研究,比如安装完docker之后,验证是否成功的命令docker version

显示docker信息

这个命令是最简单的,就是查看docker本身的一些信息:

docker info [OPTIONS]

Options

Name, shorthandDefaultDescription
--format , -fFormat the output using the given Go template

因为我服务器上面是用最新版本的docker,可以查看一下

显示Docker版本信息

docker version [OPTIONS]

Name, shorthandDefaultDescription
--format , -fFormat the output using the given Go template

version命令打印所有独立版本的Docker组件的当前版本号。使用--format选项可以自定义输出。

docker version

 输出docker相关的版本信息

从输出可以看到,“Client”部分显示了Docker CLI和客户端组件的信息,Server”部分包含Docker Engine和引擎使用的组件的信息。通过-f可选参数可以定制化输出,比如显示客户端版本信息:

docker version --format '{{.Client.APIVersion}}'

 输出docker客户端的版本信息,可以发现跟上面docker version输出的API version是一致的! 

同理,可以显示服务端版本:

docker version --format '{{.Server.Version}}'

或者go语言版本等:

docker version -f '{{.Server.GoVersion}}'

或者输出信息按json格式显示

docker version --format '{{json .}}'

{"Client":{"Platform":{"Name":"Docker Engine - Community"},"Version":"19.03.8","ApiVersion":"1.40","DefaultAPIVersion":"1.40","GitCommit":"afacb8b","GoVersion":"go1.12.17","Os":"darwin","Arch":"amd64","BuildTime":"Wed Mar 11 01:21:11 2020","Experimental":true},"Server":{"Platform":{"Name":"Docker Engine - Community"},"Components":[{"Name":"Engine","Version":"19.03.8","Details":{"ApiVersion":"1.40","Arch":"amd64","BuildTime":"Wed Mar 11 01:29:16 2020","Experimental":"true","GitCommit":"afacb8b","GoVersion":"go1.12.17","KernelVersion":"4.19.76-linuxkit","MinAPIVersion":"1.12","Os":"linux"}},{"Name":"containerd","Version":"v1.2.13","Details":{"GitCommit":"7ad184331fa3e55e52b890ea95e65ba581ae3429"}},{"Name":"runc","Version":"1.0.0-rc10","Details":{"GitCommit":"dc9208a3303feef5b3839f4323d9beb36df0a9dd"}},{"Name":"docker-init","Version":"0.18.0","Details":{"GitCommit":"fec3683"}}],"Version":"19.03.8","ApiVersion":"1.40","MinAPIVersion":"1.12","GitCommit":"afacb8b","GoVersion":"go1.12.17","Os":"linux","Arch":"amd64","KernelVersion":"4.19.76-linuxkit","Experimental":true,"BuildTime":"2020-03-11T01:29:16.000000000+00:00"}}

拉取镜像

在前面安装docker之后,为了验证docker是否安装成功,运行了hello-world镜像,因为是第一次运行,会主动去仓库拉取镜像:Pulling from library/hello-world

而如果要主动拉取镜像,命令如下:

docker pull [OPTIONS] NAME[:TAG|@DIGEST]

Options

Name, shorthandDefaultDescription
--all-tags , -aDownload all tagged images in the repository
--disable-content-trusttrueSkip image verification
--platformSet platform if server is multi-platform capable
--quiet , -qSuppress verbose output

比如下载一个nginx,不加任务参数,直接docker pull 目标,表示下载最新版本的

docker pull nginx

 也可以通过tag来拉取指定版本的镜像nginx,比如tag为

docker pull nginx:stable-alpine-perl

 如果要查询所有的tag,可以通过docker hub:Dockerhttps://hub.docker.com/ 去查询镜像信息来得到

也可以通过-q启动安静模式,在安装的过程中不会输出任何过程信息

查看镜像

查看镜像的命令已经出现过很多次了,这个命令就是

docker images [OPTIONS] [REPOSITORY[:TAG]]

Options

Name, shorthandDefaultDescription
--all , -aShow all images (default hides intermediate images)
--digestsShow digests
--filter , -fFilter output based on conditions provided
--formatFormat output using a custom template: ‘table’: Print output in table format with column headers (default) ‘table TEMPLATE’: Print output in table format using the given Go template ‘json’: Print in JSON format ‘TEMPLATE’: Print output using the given Go template. Refer to https://docs.docker.com/go/formatting/ for more information about formatting output with templates
--no-truncDon’t truncate output
--quiet , -qOnly show image IDs

docker images会展示所有的镜像,包括名称,TAG,镜像ID,创建时间和大小。这里就有刚才拉取的nginx的几个镜像,以及安装时验证的hello-world:

 docker images默认是展示所有的镜像,相当于加了于-a参数。而-q参数表示只显示镜像ID:

 docker images可以通过仓库或者TAG来查询,比如只看nginx的镜像:

 docker images --no-trunc,表示不截断输出,主要就是IMAGE_ID那一列,因为它是sha256摘要比较长,默认没有全部显示

 docker images --filter可以对镜像进行过滤,比如按时创建时间显示:

  •  before,创建时间在指定镜像之前的
  • since,创建时间在指定镜像之后的

删除镜像

在前面配置阿里云镜像加速器的时候,通过安装tomcat来验证加速效果,出现了删除镜像,命令就是:

docker rmi [OPTIONS] IMAGE [IMAGE...]

Options

Name, shorthandDefaultDescription
--force , -fForce removal of the image
--no-pruneDo not delete untagged parents

docker rmi 镜像名称,表示直接删除镜像,比如删除刚才的nginx

删除完成之后,再docker images nginx查看一下nginx的镜像,发现还有,这是因为如果不指定删除条件,不是会全部删除的,只会删除最近的一条latest。

 如果要删除全部的nginx镜像,可以指定镜像名称,一条条删除

 或者是镜像ID: 

除了这样一条条删除之外,还可以利用上面的查询来联合删除:docker images -qa查询出所有的镜像,传递给rmi循环删除!

docker rmi $(docker images -aq)

 而其中的-f,表示强制删除:如果有镜像正在被运行,是不能删除的;想强制删除就得加-f

尝试强制删除tomcat,再查看之后会发现一个比较奇怪的现象,有条tomcat的关联不上了,这是为什么呢?

官方文档给出了解释:

This will display untagged images that are the leaves of the images tree (not intermediary layers). These images occur when a new build of an image takes the repo:tag away from the image ID, leaving it as <none>:<none> or untagged. A warning will be issued if trying to remove an image when a container is presently using it. By having this flag it allows for batch cleanup.

这将显示未标记的图像,这些图像是图像树的叶子(而不是中间层)。当图像的新构建将repo:标记从图像ID中移除,将其保留为<none>:<none>或未标记时,就会出现这些图像。当容器当前正在使用图像时,如果试图删除图像,将发出警告。通过使用此标志,可以进行批量清理。

所以对于这种数据,可以结合docker images --filter "dangling=true"来处理("dangling=true"表示 悬空的节点)

docker rmi $(docker images -f "dangling=true" -q)

搜索镜像

搜索镜像命令也用过很多次了,就是

docker search [OPTIONS] TERM

Options

Name, shorthandDefaultDescription
--filter , -fFilter output based on conditions provided
--formatPretty-print search using a Go template
--limitMax number of search results
--no-truncDon’t truncate output

比如上面搜索nginx,tomcat:

 这里还要说的是,除了在宿主机上面命令行搜索,还可以通过docker hub可视化搜索,比如搜索mysql,如果是要查看详细信息,界面会方便一些

 除了这些常用的之外,比如docker build及docker commi等跟docker file文件有关的,在后面再研究。下一篇会介绍一下常用的容器命令!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值