镜像image
是Docker架构的三个基本概念之一,就相当于是一个root
文件系统。比如官方镜像 ubuntu:16.04 就包含了完整的一套Ubuntu16.04最小系统的 root
文件系统。
可以在命令后面加--help
查看具体命令的详细使用方式:
docker COMMAND --help
docker images [OPTIONS] [REPOSITORY[:TAG]]
参数说明:
默认列出所有镜像
docker iamges REPOSITORY TAG IMAGE ID CREATED SIZE tomcat10 10.0.8 e5f9a8863052 14 minutes ago 3.17MB arvin88/tomcat_user 8 4b6a6e0cf6c1 43 hours ago 590MB arvin88/tomcat_user 8.1 4b6a6e0cf6c1 43 hours ago 590MB
各列说明:
列出指定镜像名列表
docker iamges arvin88/tomcat_user REPOSITORY TAG IMAGE ID CREATED SIZE arvin88/tomcat_user 8 4b6a6e0cf6c1 43 hours ago 590MB arvin88/tomcat_user 8.1 4b6a6e0cf6c1 43 hours ago 590MB
列出指定镜像名+标签列表
docker iamges arvin88/tomcat_user:8 REPOSITORY TAG IMAGE ID CREATED SIZE arvin88/tomcat_user 8 4b6a6e0cf6c1 43 hours ago 590MB
选项:-a,--all
docker iamges -a 或者 docker iamges --all REPOSITORY TAG IMAGE ID CREATED SIZE tomcat10 10.0.8 e5f9a8863052 14 minutes ago 3.17MB arvin88/tomcat_user 8 4b6a6e0cf6c1 43 hours ago 590MB arvin88/tomcat_user 8.1 4b6a6e0cf6c1 43 hours ago 590MB
结果与不加选项条件效果一样。
选项:--digests
docker images --digests REPOSITORY TAG DIGEST IMAGE ID CREATED SIZE tomcat10 10.0.8 <none> e5f9a8863052 20 minutes ago 3.17MB arvin88/tomcat_user 8 <none> 4b6a6e0cf6c1 43 hours ago 590MB arvin88/tomcat_user 8.1 <none> 4b6a6e0cf6c1 43 hours ago 590MB
结果输出多了DIGEST
列。
选项:-f, --filter filter
过滤标志(-f
或--filter
)的格式为key=value
,如果有多个过滤,则传多个标志(--filter "foo=bar" --filter "bif=baz"
)
目前支持的过滤器有:
docker images -f "dangling=true"
label=<key>
or label=<key>=<value>
) 根据构建的Dockerfile里面的LBAEL过滤docker images -f "label=com.example.version"
<image-name>[:<tag>]
, <image id>
or <image@digest>
) 显示给定的镜像之前创建的镜像docker images --filter "before=4b6a6e0cf6c1"
<image-name>[:<tag>]
, <image id>
or <image@digest>
) 显示给定的镜像之后创建的镜像docker images --filter "since=4b6a6e0cf6c1"
docker images --filter=reference='*/*'
选项:--format
格式化占位符如下:
.ID
:镜像ID.Repository
:镜像仓库源.Tag
:镜像名.Digest
:镜像摘要.CreatedSince
:镜像创建以来经过的时间.CreatedAt
:镜像创建时间.Size
:镜像大小docker images --format "{{.ID}}:{{.Repository}}:{{.Tag}}:{{.CreatedAt}}" e5f9a8863052:tomcat10:10.0.8:2021-07-29 04:06:54 -0400 EDT 4b6a6e0cf6c1:arvin88/tomcat_user:8:2021-07-27 09:46:49 -0400 EDT 4b6a6e0cf6c1:arvin88/tomcat_user:8.1:2021-07-27 09:46:49 -0400 EDT
选项:--no-trunc
docker images --no-trunc REPOSITORY TAG IMAGE ID CREATED SIZE tomcat10 10.0.8 sha256:e5f9a88630528d90a58873a8efa7249dbeb961a1405618a105ad4a8e283041e0 2 hours ago 3.17MB arvin88/tomcat_user 8 sha256:4b6a6e0cf6c14223b914fea42a6be8a49c2d1e9b4358a0f225967f0fafcfa1cb 44 hours ago 590MB arvin88/tomcat_user 8.1 sha256:4b6a6e0cf6c14223b914fea42a6be8a49c2d1e9b4358a0f225967f0fafcfa1cb 44 hours ago 590MB
可以看出,会将镜像ID全部输出
选项:-q, --quiet
docker images -q 或 docker images --quiet e5f9a8863052 4b6a6e0cf6c1 4b6a6e0cf6c1
选项也有可以多个配合使用,比如列出镜像ID并且不截断。
docker images --format='{{.ID}}' --no-trunc sha256:e5f9a88630528d90a58873a8efa7249dbeb961a1405618a105ad4a8e283041e0 sha256:4b6a6e0cf6c14223b914fea42a6be8a49c2d1e9b4358a0f225967f0fafcfa1cb sha256:4b6a6e0cf6c14223b914fea42a6be8a49c2d1e9b4358a0f225967f0fafcfa1cb
docker search [OPTIONS] TERM
参数说明:
参数的-f
、--format
、--no-trunc
与images
的用法类似。
docker search --limit 3 --no-trunc tomcat NAME DESCRIPTION STARS OFFICIAL AUTOMATED tomcat Apache Tomcat is an open source implementation of the Java Servlet and JavaServer Pages technologies 3081 [OK] rightctrl/tomcat CentOS , Oracle Java, tomcat application ssl https 6 [OK] jelastic/tomcat An image of the Tomcat Java application server maintained by Jelastic PaaS. 2
docker pull [OPTIONS] NAME[:TAG|@DIGEST]
参数说明:
拉取时不设置参数或指定TAG
,则默认获取latest
镜像。
docker pull ykaj9758/tomcat9 Using default tag: latest latest: Pulling from ykaj9758/tomcat9 469cfcc7a4b3: Pull complete dd66912d30dc: Pull complete b9e106a1d03e: Pull complete 6d41cb630740: Pull complete Digest: sha256:e70bc08b133dc30eb3274e1cfd0628a612d35a1296f1a493ecead4f5ec8c2ee4 Status: Downloaded newer image for ykaj9758/tomcat9:latest docker.io/ykaj9758/tomcat9:latest
参数用法就不测试了,有兴趣自行测试。
docker push [OPTIONS] NAME[:TAG]
参数说明:
docker push -q arvin88/tomcat10:v1 --推送成功,由于这里加了-q,因此没有输出详细推送信息 docker.io/arvin88/tomcat10:v1
docker rmi [OPTIONS] IMAGE [IMAGE...]
参数说明:
根据镜像仓库删除。
docker rmi ykaj9758/tomcat9 Untagged: ykaj9758/tomcat9:latest Untagged: ykaj9758/tomcat9@sha256:e70bc08b133dc30eb3274e1cfd0628a612d35a1296f1a493ecead4f5ec8c2ee4 Deleted: sha256:4fe6b09b4e594b5ccc512445f6edad3c8c07774523ee664ef9d010bec992d76a Deleted: sha256:6ee9982c8811fbf5a54ad29723c6756abf979595cc663dd0eea187a371cd26dd Deleted: sha256:b35eeacb163e86113cc1f5f6578e4d31a08c185da53415d3b6b0389ba3091409 Deleted: sha256:470497dbb86a60aceb8c48b8ee802cc7a2e42b6973dfc36c8f2c111cb6af9f63 Deleted: sha256:43e653f84b79ba52711b0f726ff5a7fd1162ae9df4be76ca1de8370b8bbf9bb0
也可以根据镜像ID删除。如果需要将本地镜像全部删除,可以在命令后面追加列出所有镜像命令
docker rmi $(docker images) 如果镜像列表中镜像ID有重复,则不能删除,可以加上-f强制删除 docker rmi -f $(docker images)
docker save [OPTIONS] IMAGE [IMAGE...]
参数说明:
docker save -o tomcat10.tar arvin88/tomcat10 --结果 tomcat10.tar 不使用参数情况下,可以这样: docker save arvin88/tomcat10 > tomcat10.tar
docker load [OPTIONS]
参数说明:
上一步将镜像保存为本地文件,现在将保存后的文件还原为镜像。
docker load -i tomcat10.tar 12295e8f45f4: Loading layer [==================================================>] 3.185MB/3.185MB Loaded image: arvin88/tomcat10:v1 [root@localhost docker]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE arvin88/tomcat10 v1 e5f9a8863052 4 hours ago 3.17MB
docker history [OPTIONS] IMAGE
参数说明:
以上参数除了-H,--human
,其他与search
命令参数用法一样。
docker history arvin88/tomcat10:v1 IMAGE CREATED CREATED BY SIZE COMMENT e5f9a8863052 4 hours ago 3.17MB Imported from https://mirror-hk.koddos.net/apache/tomcat/tomcat-10/v10.0.8/bin/apache-tomcat-10.0.8-deployer.tar.gz 加上-H=false结果 IMAGE CREATED AT CREATED BY SIZE COMMENT e5f9a8863052 2021-07-29T04:06:54-04:00 3165364 Imported from https://mirror-hk.koddos.net/apache/tomcat/tomcat-10/v10.0.8/bin/apache-tomcat-10.0.8-deployer.tar.gz
docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
SOURCE_IMAGE[:TAG]:原镜像
TARGET_IMAGE[:TAG]:打标记后的镜像
docker tag arvin88/tomcat10:v1 arvin88/tomcat10:v2 REPOSITORY TAG IMAGE ID CREATED SIZE arvin88/tomcat10 v1 e5f9a8863052 4 hours ago 3.17MB arvin88/tomcat10 v2 e5f9a8863052 4 hours ago 3.17MB
docker import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]
参数说明:
将tomcat9从官网拉取下来。
docker import -m="首次拉取Tomcat9" https://mirror-hk.koddos.net/apache/tomcat/tomcat-9/v9.0.50/bin/apache-tomcat-9.0.50-fulldocs.tar.gz arvin88/tomcat9:v1 Downloading from https://mirror-hk.koddos.net/apache/tomcat/tomcat-9/v9.0.50/bin/apache-tomcat-9.0.50-fulldocs.tar.gz Importing [==================================================>] 6.538MB/6.538MB sha256:9bbb4fc3b91617c09c82322f35c518791ca31e0ca4effdeb87652145ef54d792 REPOSITORY TAG IMAGE ID CREATED SIZE arvin88/tomcat9 v1 9bbb4fc3b916 28 seconds ago 73.4MB
如果不指定存储名称,则默认<none>
docker import -m="首次拉取Tomcat9" https://mirror-hk.koddos.net/apache/tomcat/tomcat-9/v9.0.50/bin/apache-tomcat-9.0.50-fulldocs.tar.gz Downloading from https://mirror-hk.koddos.net/apache/tomcat/tomcat-9/v9.0.50/bin/apache-tomcat-9.0.50-fulldocs.tar.gz Importing [==================================================>] 6.538MB/6.538MB sha256:12b66d873fb4aca882f106e7c4616cb08d243779657950a8fdced7cba12593ec REPOSITORY TAG IMAGE ID CREATED SIZE <none> <none> 12b66d873fb4 33 seconds ago 73.4MB
然后可以使用tag
命令,设置名称及标签
docker tag 12b66d873fb4 tomcat9:v1 REPOSITORY TAG IMAGE ID CREATED SIZE tomcat9 v1 12b66d873fb4 About a minute ago 73.4MB
docker inspect [OPTIONS] NAME|ID [NAME|ID...] docker inspect 12b66d873fb4 [ { "Id": "sha256:12b66d873fb4aca882f106e7c4616cb08d243779657950a8fdced7cba12593ec", "RepoTags": [ "tomcat9:v1" ], "RepoDigests": [], "Parent": "", "Comment": "首次拉取Tomcat9", "Created": "2021-07-29T11:58:24.888558636Z", "Container": "", "ContainerConfig": { "Hostname": "", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "Tty": false, "OpenStdin": false, "StdinOnce": false, "Env": null, "Cmd": null, "Image": "", "Volumes": null, "WorkingDir": "", "Entrypoint": null, "OnBuild": null, "Labels": null }, "DockerVersion": "20.10.7", "Author": "", "Config": { "Hostname": "", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "Tty": false, "OpenStdin": false, "StdinOnce": false, "Env": null, "Cmd": null, "Image": "", "Volumes": null, "WorkingDir": "", "Entrypoint": null, "OnBuild": null, "Labels": null }, "Architecture": "amd64", "Os": "linux", "Size": 73362010, "VirtualSize": 73362010, "GraphDriver": { "Data": { "MergedDir": "/var/lib/docker/overlay2/b65458a9b24e69d75b82ad423b42bc36a2969ecaf20687f574c0d26718a67b06/merged", "UpperDir": "/var/lib/docker/overlay2/b65458a9b24e69d75b82ad423b42bc36a2969ecaf20687f574c0d26718a67b06/diff", "WorkDir": "/var/lib/docker/overlay2/b65458a9b24e69d75b82ad423b42bc36a2969ecaf20687f574c0d26718a67b06/work" }, "Name": "overlay2" }, "RootFS": { "Type": "layers", "Layers": [ "sha256:07c25b8b9cae522b03fcde4ad413f0ad1fab9d4b099fcb36108044f4f1351950" ] }, "Metadata": { "LastTagTime": "2021-07-29T07:59:59.783788199-04:00" } } ]
以上命令是对整个本地镜像进行操作,如果需要对单个镜像操作,可以看下docker image --help
具体使用。