docker 系列4 搜索镜像和删除清理镜像

一.搜索镜像

  主要介绍Docker镜像的search子命令,使用docker search命令可以搜索docker hub官方仓库的镜像,语法为: docker search [option] keyword。支持的命令选项主要包括:

    -f ,  --filter    过滤输出内容

    --format   string :格式化输出内容;

    --limit   int :限制输出结果个数,默认为25个;

    --no-trunc :不截断输出结果

  文档:docker search | Docker Docs

  例如,搜索官方提供的带nginx关键词的镜像,如下所示:

[root@VM_0_12_centos ~]# docker search nginx
NAME                               DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
nginx                              Official build of Nginx.                        13088               [OK]                
jwilder/nginx-proxy                Automated Nginx reverse proxy for docker con…   1786                                    [OK]
richarvey/nginx-php-fpm            Container running Nginx + PHP-FPM capable of…   771                                     [OK]
linuxserver/nginx                  An Nginx container, brought to you by LinuxS…   107                                     
bitnami/nginx                      Bitnami nginx Docker Image                      83                                      [OK]
tiangolo/nginx-rtmp                Docker image with Nginx using the nginx-rtmp…   70                                      [OK]
alfg/nginx-rtmp                    NGINX, nginx-rtmp-module and FFmpeg from sou…   57                                      [OK]
jc21/nginx-proxy-manager           Docker container for managing Nginx proxy ho…   54                                      
nginxdemos/hello                   NGINX webserver that serves a simple page co…   48                                      [OK]
jlesage/nginx-proxy-manager        Docker container for Nginx Proxy Manager        39                                      [OK]
nginx/nginx-ingress                NGINX Ingress Controller for Kubernetes         29                                      
privatebin/nginx-fpm-alpine        PrivateBin running on an Nginx, php-fpm & Al…   24                                      [OK]
schmunk42/nginx-redirect           A very simple container to redirect HTTP tra…   18                                      [OK]
nginxinc/nginx-unprivileged        Unprivileged NGINX Dockerfiles                  14                                      
centos/nginx-112-centos7           Platform for running nginx 1.12 or building …   13                                      
centos/nginx-18-centos7            Platform for running nginx 1.8 or building n…   13                                      
blacklabelops/nginx                Dockerized Nginx Reverse Proxy Server.          13                                      [OK]
raulr/nginx-wordpress              Nginx front-end for the official wordpress:f…   12                                      [OK]
nginx/nginx-prometheus-exporter    NGINX Prometheus Exporter                       11                                      
sophos/nginx-vts-exporter          Simple server that scrapes Nginx vts stats a…   7                                       [OK]
mailu/nginx                        Mailu nginx frontend                            6                                       [OK]
bitnami/nginx-ingress-controller   Bitnami Docker Image for NGINX Ingress Contr…   5                                       [OK]
ansibleplaybookbundle/nginx-apb    An APB to deploy NGINX                          1                                       [OK]
wodby/nginx                        Generic nginx                                   1                                       [OK]

  可以看到返回了很多包含关键字的镜像,其中包括镜像名字,描述,收藏数(表示受欢迎的程度),是否官方创建,是否自动创建。

二.删除和清理镜像

  主要介绍docker镜像的rm和prune子命令

  1.使用标签删除镜像

    使用docker rmi或docker image rm命令可以删除镜像,命令格式为 docker rmi image[image ...], 其中image可以为标签或ID。支持的选项包括:

    -f , -force :强制删除镜像,即使有容器依赖它

    -no-prune  :不要清理未带标签的父镜像

    文档:docker rmi | Docker Docs

    例如:要删除 myhello-world镜像,可以使用如下命令(再查看已删除):

[root@VM_0_12_centos ~]# docker rmi myhello-world:latest
Untagged: 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
[root@VM_0_12_centos ~]# 

    上面只是删除了一个副本,当只剩一个标签的时候就要小心了,此时再使用docker rmi命令会彻底删除镜像

  2.使用镜像ID来删除镜像

    当使用rmi命令,并且后面跟上镜像的ID时,会先尝试删除所有指向该镜像的标签,然后删除该镜像文件本身。注意当有该镜像创建的容器存在时,镜像文件默认是无法被删除的,例如:先利用hello-world镜像创建一个简单的容器来输出一段话:

[root@VM_0_12_centos ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                   PORTS               NAMES
6a8fc2b94a89        hello-world         "/hello"            3 hours ago         Exited (0) 3 hours ago                       naughty_chatelet
7bc638ee2478        hello-world         "bash"              3 hours ago         Created                                      heuristic_bassi
[root@VM_0_12_centos ~]# docker run hello-world

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

[root@VM_0_12_centos ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
e709d22a467d        hello-world         "/hello"            4 seconds ago       Exited (0) 4 seconds ago                       distracted_lalande
6a8fc2b94a89        hello-world         "/hello"            3 hours ago         Exited (0) 3 hours ago                         naughty_chatelet
7bc638ee2478        hello-world         "bash"              3 hours ago         Created                                        heuristic_bassi

   第一次查看容器docker ps -a有二个容器,通过docker run hello-world 命令又创建了一个容器。可以看到,后台存在一个刚退出状态的容器 ,退出的4秒(e709d22a467d)。

  如果删除该镜像,bf756fb1ae65为hello-world的镜像ID (该镜像没有了副本后),docker会提示有容器正在运行,无法删除

[root@VM_0_12_centos ~]# docker rmi  bf756fb1ae65 
Error response from daemon: conflict: unable to delete bf756fb1ae65 (must be forced) - image is being used by stopped container 7bc638ee2478

  如果想强行删除镜像,可以使用-f参数命令如下: docker rmi  -f   bf756fb1ae65

  但通常并不推荐使用-f参数来强制删除一个存在容器依赖的镜像,正确的做法是先删除依赖该镜像的所有容器,再来删除镜像。

  首先删除容器7bc638ee2478,删除后在查看容器列表

[root@VM_0_12_centos ~]# docker rm 7bc638ee2478
7bc638ee2478
[root@VM_0_12_centos ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
e709d22a467d        bf756fb1ae65        "/hello"            15 minutes ago      Exited (0) 15 minutes ago                       distracted_lalande
6a8fc2b94a89        bf756fb1ae65        "/hello"            3 hours ago         Exited (0) 3 hours ago                          naughty_chatelet
[root@VM_0_12_centos ~]# 

  3. 清理镜像

    使用docker一段时间后,系统中可能会遗留一些临时的镜像文件,以及一些没有被使用的镜像,可以通过docker image prune命令来进行清理,支持选项包括:

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

    -filter  只清理符合给定过滤器的镜像

    -f, -force 强制删除镜像,而不进行提示确认

    文档:docker image prune | Docker Docs

   如下所示:删除所有无用镜像,没有删除该镜像,是因为该镜像关联了容器 

[root@VM_0_12_centos ~]# 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
Total reclaimed space: 0B
[root@VM_0_12_centos ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              bf756fb1ae65        3 months ago        13.3kB
[root@VM_0_12_centos ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                         PORTS               NAMES
e709d22a467d        bf756fb1ae65        "/hello"            About an hour ago   Exited (0) About an hour ago                       distracted_lalande
6a8fc2b94a89        bf756fb1ae65        "/hello"            4 hours ago         Exited (0) 4 hours ago                             naughty_chatelet
[root@VM_0_12_centos ~]# 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值