docker镜像的获取删除和查看镜像信息历史记录清理残余镜像

前面已经介绍镜像的好处, 这里就不多解释, 直入主题, 上例子

上一篇已经介绍拉取第一个项目 hello-world,并运行, 也实践了, 这里继续学习镜像拉取,

拉取第一个服务nginx, 拉取并运行如下操作

第一个 Docker 实例

当前只是演示拉取服务并跑起来, 参数稍后会一一详解

[root@chensir ~] # docker run -d --rm --name nginx -p 80:80 nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
bb79b6b2107f: Pull complete
5a9f1c0027a7: Downloading [==============> ] 7.457MB/26.49MB
5a9f1c0027a7: Downloading [======================> ] 12.17MB/26.49MB
166a2418f7e8: Download complete
1966ea362d23: Download complete
使用 Docker 镜像

docker 镜像是 docker 中三大概念之一,其主要作用是作为启动容器的模板。

[root@chensir ~] # docker run 镜像|镜像ID
获取镜像

镜像是运行容器的模板,官方 Docker Hub 仓库已经提供了许多镜像共开发者使用。如果我们需要获取某 个镜像则可以去 docker 仓库下载所需的镜像。

下载镜像的格式:docker pull [image_name][:image_version]

下载镜像实例:

[root@chensir ~]# docker pull nginx:1.17

下载的时候,我们可以看到有若干层组成,像 afb6ec6fdc1c 这样的字符串是层的唯一 ID(实际上,完整的 ID 包括 256 比特, 64 个十六进制字符组成)。使用 docker pull 命令下载中会获取并输出镜像的各层信息。当不 同的镜像包括相同的层的时候,本地仅存一份内容,减小存储空间.

查看镜像信息

镜像主要包括镜像文件、镜像 tag 以及镜像详细信息等等。

[root@chensir ~]# docker images
镜像列表

使用 docker images 或 docker images ls 命令可以列举本地主机上已有镜像的基本信息。使用同样的命令

[root@chensir ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               1.17                9beeba249f3e        6 months ago        127MB
hello-world         latest              bf756fb1ae65        11 months ago       13.3kB

列举信息中,可以看到几个字段信息:

  • 镜像来源:来自哪个仓库,默认来自:hub.docker.com
  • 镜像标签:比喻 1.17, latest(最新版)
  • 镜像 ID:例如 9beeba249f3e
  • 镜像创建时间:例如:6 months ago
  • 镜像大小:127MB

其中镜像的 ID 信息十分重要,它唯一标识了镜像。在使用镜像 ID 的时候,一般可以使用该 ID 的前若干个 字符组成的可区分串来替代完整的 ID。

TAG 信息用于标记来自同一个仓库的不同镜像。TAG 在同一个仓库中是唯一的。

镜像大小信息只是表示了该镜像的逻辑体积大小,实际上由于相同的镜像层本地只会存储一份,物理上占用 的存储空间会小于各镜像逻辑体积之和。

image 子命令主要支持如下选项:

-a : 列出所有(包括临时文件)镜像文件

–digests=true|false:列出镜像的数字摘要值

-q : 仅显示 ID 信息

自行测试
[root@chensir ~]# docker images -q
9beeba249f3e
bf756fb1ae65
为镜像添加 tag

为了方便后续工作中使用特定的镜像,还可以使用 docker tag 命令来为本地的镜像添加标签

现有镜像docker tag [镜像名称]:[镜像版本] [镜像新名称]:[版本], 还有参数, 自行研究, 后续会介绍一点

[root@chensir ~]# docker images -a
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               1.17                9beeba249f3e        6 months ago        127MB
hello-world         latest              bf756fb1ae65        11 months ago       13.3kB

[root@chensir ~]# docker tag nginx:1.17 mynginx:v1
[root@chensir ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mynginx             v1                  9beeba249f3e        6 months ago        127MB
nginx               1.17                9beeba249f3e        6 months ago        127MB
hello-world         latest              bf756fb1ae65        11 months ago       13.3kB
使用 inspect 命令查看详细信息

详解 docker inspect [镜像名|镜像ID], 可以是镜像名, 也可以是镜像ID

[root@chensir ~]# docker inspect 9beeba249f3e   我这里用镜像id
[
    {
        "Id": "sha256:9beeba249f3ee158d3e495a6ac25c5667ae2de8a43ac2a8bfd2bf687a58c06c9",
        "RepoTags": [
            "mynginx:v1",
            "nginx:1.17"
        ],
        "RepoDigests": [
            "nginx@sha256:6fff55753e3b34e36e24e37039ee9eae1fe38a6420d8ae16ef37c92d1eb26699"
        ],
.....
使用 history 命令查看镜像历史
[root@chensir ~]# docker history 9beeba249f3e
IMAGE               CREATED             CREATED BY                                      SIZE  COMMENT
9beeba249f3e        6 months ago        /bin/sh -c #(nop)  CMD ["nginx" "-g" "daemon…   0B                  
<missing>           6 months ago        /bin/sh -c #(nop)  STOPSIGNAL SIGTERM           0B                  
<missing>           6 months ago        /bin/sh -c #(nop)  EXPOSE 80                    0B                  
<missing>           6 months ago        /bin/sh -c ln -sf /dev/stdout /var/log/nginx…   22B                 
<missing>           6 months ago        /bin/sh -c set -x     && addgroup --system -57.6MB              
<missing>           6 months ago        /bin/sh -c #(nop)  ENV PKG_RELEASE=1~buster     0B                  
<missing>           6 months ago        /bin/sh -c #(nop)  ENV NJS_VERSION=0.3.9        0B                  
<missing>           6 months ago        /bin/sh -c #(nop)  ENV NGINX_VERSION=1.17.10    0B                  
<missing>           6 months ago        /bin/sh -c #(nop)  LABEL maintainer=NGINX Do…   0B                  
<missing>           6 months ago        /bin/sh -c #(nop)  CMD ["bash"]                 0B                  
<missing>           6 months ago        /bin/sh -c #(nop) ADD file:7780c81c33e6cc5b6…   69.2MB 

注意:有些构建信息过长,可以使用–no-trunc 选项来输出完整信息

搜索镜像

在docker中搜索镜像主要使用Search子命令,默认只搜索Docker Hub官方镜像仓库中的镜像。其语法为

docker search [option] keyword。支持的命令选项主要包括:

-f : 过滤输出内容

搜索官方提供的带有 Redis 关键字的镜像

[root@chensir ~]# docker search -f is-official=true redis
NAME                DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
redis               Redis is an open source key-value store that…   8792                [OK]

搜索被收藏超过 8000 个的并且关键词包括 mysql 的镜像

[root@chensir ~]# docker search -f stars=8000 mysql
NAME                DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mysql               MySQL is a widely used, open-source relation…   10202               [OK]

–limit: 限制输出结果

[root@chensir ~]# docker search --limit=2 mysql
NAME                              DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mysql                             MySQL is a widely used, open-source relation…   10202               [OK]                
mariadb                           MariaDB is a community-developed fork of MyS…   3753                [OK]                
mysql/mysql-server                Optimized MySQL Server Docker images. Create…   746

–no-trunc: 不截断输出结果, 结果会非常长,

[root@chensir ~]# docker search -f stars=8 --limit 3 --no-trunc mysql
NAME                 DESCRIPTION                                                                                           STARS               OFFICIAL            AUTOMATED
mysql                MySQL is a widely used, open-source relational database management system (RDBMS).                    10202               [OK]                
mysql/mysql-server   Optimized MySQL Server Docker images. Created, maintained and supported by the MySQL team at Oracle   746                                     [OK]
bitnami/mysql        Bitnami MySQL Docker Image                                                                            47                                      [OK]

输出参数释义
NAME     DESCRIPTION      STARS      OFFICIAL     AUTOMATED
  • NAME: 镜像仓库源的名称
  • DESCRIPTION: 镜像的描述
  • OFFICIAL: 是否 docker 官方发布
  • stars: 类似 Github 里面的 star,标星,表示点赞
  • AUTOMATED: 自动构建。
删除和清理镜像

在 docker 中,删除镜像主要使用 rmi 子命令,清理镜像主要使用 prune 子命令。

使用标签删除镜像

使用 docker rmidocker image rm 命令可以删除镜像,命令格式为 docker rmi image

[root@chensir ~]# docker rmi hello-world:latest

使用参数 -f : 强制删除镜像。

[root@chensir ~]# docker rmi -f bf756fb1ae65
Untagged: hello-world:latest
Untagged: hello-world@sha256:e7c70bb24b462baa86c102610182e3efcb12a04854e8c582838d92970a09f323
Deleted: sha256:bf756fb1ae65adf866bd8c456593cd24beb6a0a061dedf42b26a993176745f6b
清理镜像

使用一段时间之后,docker 会产生很多临时文件,以及一些没有被使用的镜像,我们可以通过 docker image prune 命令来进行清理。删除没有被使用的镜像, 使用的不会删除

参数:

-a :删除所有无用的镜像,不光是临时镜像。

先查看所有镜像
[root@chensir ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mysql               5.7                 ae0658fdbad5        10 days ago         449MB
redis               latest              74d107221092        13 days ago         104MB
centos              latest              0d120b6ccaa8        3 months ago        215MB
mynginx             v1                  9beeba249f3e        6 months ago        127MB
nginx               1.17                9beeba249f3e        6 months ago        127MB

查看运行的容器
[root@chensir ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
2bf02516069b        redis               "docker-entrypoint.s…"   5 seconds ago       Up 3 seconds        6379/tcp            stupefied_bouman
43b257df88ee        centos              "bash"                   32 seconds ago      Up 31 seconds                           naughty_einstein


只有两个被使用到, 其他的是多余的, 需要使用prune删除掉
[root@chensir ~]# docker image prune -a
[root@chensir ~]# docker image prune -a
WARNING! This will remove all images without at least one container associated to them.
Are you sure you want to continue? [y/N] y  输入y确认删除
Deleted Images:
untagged: mynginx:v1
untagged: nginx:1.17
untagged: nginx@sha256:6fff55753e3b34e36e24e37039ee9eae1fe38a6420d8ae16ef37c92d1eb26699
deleted: sha256:9beeba249f3ee158d3e495a6ac25c5667ae2de8a43ac2a8bfd2bf687a58c06c9
...
untagged: mysql:5.7
untagged: mysql@sha256:8e2004f9fe43df06c3030090f593021a5f283d028b5ed5765cc24236c2c4d88e
deleted: sha256:ae0658fdbad5fb1c9413c998d8a573eeb5d16713463992005029c591e6400d02
...

Total reclaimed space: 506MB

再查看只有运行的镜像保留下来

-f :强制删除镜像,而不进行提示。也就是没有 y确认项, 自行测试

下一篇讲构建镜像和保存, 期待哦!

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值