欢迎来到赛兔子家园

删除docker镜像&镜像综合管理

查看docker镜像

先查看本机有哪些镜像,然后去删除对应的镜像。

查看本地镜像命令:docker images

[root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
redis        latest    02c7f2054405   2 days ago     105MB
nginx        latest    822b7ec2aaf2   2 days ago     133MB
ubuntu       latest    fb52e22af1b0   5 days ago     72.8MB
nginx        <none>    08b152afcfae   6 weeks ago    133MB
centos       latest    300e315adb2f   9 months ago   209MB

查看具体镜像命令:docker images contos

[root@localhost ~]# docker images centos
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
centos       latest    300e315adb2f   9 months ago   209MB

docker images nginx

[root@localhost ~]# docker images nginx
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
nginx        latest    822b7ec2aaf2   2 days ago    133MB
nginx        <none>    08b152afcfae   6 weeks ago   133MB

搜索dockerhub的镜像命令:docker search 镜像名

删除镜像

下载hello-world镜像

docker pull hello-world

docker images  查看本地镜像

root@localhost ~]# docker pull hello-world
Using default tag: latest
latest: Pulling from library/hello-world
b8dfde127a29: Pull complete 
Digest: sha256:7d91b69e04a9029b99f3585aaaccae2baa80bcf318f4a5d2165a9898cd2dc0a1
Status: Downloaded newer image for hello-world:latest
docker.io/library/hello-world:latest
[root@localhost ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
redis         latest    02c7f2054405   2 days ago     105MB
nginx         latest    822b7ec2aaf2   2 days ago     133MB
ubuntu        latest    fb52e22af1b0   5 days ago     72.8MB
nginx         <none>    08b152afcfae   6 weeks ago    133MB
hello-world   latest    d1165f221234   6 months ago   13.3kB
centos        latest    300e315adb2f   9 months ago   209MB

docker是分层存储,镜像是没有自己的内核,运行该镜像时在最顶层生成可写层,在可写层跑应用程序,依赖于当前宿主机的内核运行,因此镜像可大可小。

删除镜像

根据容器名:docker rmi hello-word

根据容器id:docker rmi  d1165f221234   # id也可以只用Id前三位:docker rmi  d11

[root@localhost ~]# docker images hello-world # 查看容器信息
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    d1165f221234   6 months ago   13.3kB
[root@localhost ~]# docker rmi hello-world # 根据容器名删除,失败提升有依赖
Error response from daemon: conflict: unable to remove repository reference "hello-world" (must force) - container c4be46bffa0d is using its referenced image d1165f221234
[root@localhost ~]# docker ps -a #查看运行过的所有容器 
CONTAINER ID   IMAGE          COMMAND                  CREATED              STATUS                          PORTS                               NAMES
c4be46bffa0d   hello-world    "/hello"                 About a minute ago   Exited (0) About a minute ago                                       lucid_davinci
afbb311d96fa   nginx          "/docker-entrypoint.…"   2 hours ago          Created                                                             infallible_mendel
97213a4c348b   ubuntu         "bash"                   3 hours ago          Exited (130) 3 hours ago                                            infallible_hamilton
0ced7b15918e   300e315adb2f   "bash"                   4 hours ago          Exited (0) 3 hours ago                                              recursing_goldstine
3bfef5025609   nginx          "/docker-entrypoint.…"   4 hours ago          Up 4 hours                      0.0.0.0:80->80/tcp, :::80->80/tcp   keen_wing
d33ab4d13298   nginx          "/docker-entrypoint.…"   20 hours ago         Exited (0) 19 hours ago                                             relaxed_stonebraker
c57196538b20   08b152afcfae   "/docker-entrypoint.…"   6 weeks ago          Exited (0) 6 weeks ago                                              heuristic_elbakyan

查看hello-word容器别ID=c4be46bffa0d依赖,需要先删除这个依赖。

删除:docker rm  c4be46bffa0d

然后再执行:docker rmi hello-world

[root@localhost ~]# docker rmi hello-world
Untagged: hello-world:latest
Untagged: hello-world@sha256:7d91b69e04a9029b99f3585aaaccae2baa80bcf318f4a5d2165a9898cd2dc0a1
Deleted: sha256:d1165f2212346b2bab48cb01c1e39ee8ad1be46b87873d9ca7a4e434980a7726
Deleted: sha256:f22b99068db93900abe17f7f5e09ec775c2826ecfe9db961fea68293744144bd
镜像综合管理

批量删除镜像(慎用)

docker rmi `docker images -aq`

批量删除容器

docker rm `docker ps -aq`

导出镜像

例如:默认的运行的centos镜像,不提供vim功能,运行该容器后,在容器内安装vim,然后提交该镜像,导出镜像为压缩文件,可以发给其他人用

例如:导出cnetos

命令:docker image save centos > /opt/centos.taz  # >后面导出后保存的名字和路径

[root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
redis        latest    02c7f2054405   2 days ago     105MB
nginx        latest    822b7ec2aaf2   2 days ago     133MB
ubuntu       latest    fb52e22af1b0   5 days ago     72.8MB
nginx        <none>    08b152afcfae   6 weeks ago    133MB
centos       latest    300e315adb2f   9 months ago   209MB
[root@localhost ~]# docker image save centos > /opt/centos.taz
[root@localhost ~]# ls /opt/ -lh
total 207M
-rw-r--r-- 1 root root 207M Sep  5 22:28 centos.taz
drwx--x--x 4 root root   28 Jul 24 07:02 containerd
[root@localhost ~]# 

导入镜像

批量删除依赖:docker rm `docker ps -aq`

[root@localhost ~]# docker rm `docker ps -aq`
b9a5d55b7c37
d8f383a83be2
63316c0f8eea
afbb311d96fa
97213a4c348b
0ced7b15918e
d33ab4d13298
c57196538b20

先删除centos

[root@localhost ~]# docker rmi 300e315adb2f
Untagged: centos:latest
Untagged: centos@sha256:5528e8b1b1719d34604c87e11dcd1c0a20bedf46e83b5632cdeac91b8c04efc1
Deleted: sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55
Deleted: sha256:2653d992f4ef2bfd27f94db643815aa567240c37732cae1405ad1c1309ee9859

导入命令:docker image load -i /opt/centos.taz   # -i 后面是路径和文件名

[root@localhost ~]# docker image load -i /opt/centos.taz 
2653d992f4ef: Loading layer [==================================================>]  216.5MB/216.5MB
Loaded image: centos:latest
[root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
redis        latest    02c7f2054405   2 days ago     105MB
nginx        latest    822b7ec2aaf2   2 days ago     133MB
ubuntu       latest    fb52e22af1b0   5 days ago     72.8MB
nginx        <none>    08b152afcfae   6 weeks ago    133MB
centos       latest    300e315adb2f   9 months ago   209MB

查看镜像详细详细

查看docker服务信息:docker images

[root@localhost ~]# docker info
Client:
 Context:    default
 Debug Mode: false
 Plugins:
  app: Docker App (Docker Inc., v0.9.1-beta3)
  buildx: Build with BuildKit (Docker Inc., v0.5.1-docker)
  scan: Docker Scan (Docker Inc., v0.8.0)

Server:
 Containers: 1
  Running: 1
  Paused: 0
  Stopped: 0
 Images: 5
 Server Version: 20.10.6
 Storage Driver: overlay2
  Backing Filesystem: xfs
  Supports d_type: true
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Cgroup Version: 1
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
 Swarm: inactive
 Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 7eba5930496d9bbe375fdf71603e610ad737d2b2
 runc version: v1.0.0-0-g84113ee
 init version: de40ad0
 Security Options:
  seccomp
   Profile: default
 Kernel Version: 3.10.0-1160.el7.x86_64
 Operating System: CentOS Linux 7 (Core)
 OSType: linux
 Architecture: x86_64
 CPUs: 1
 Total Memory: 3.682GiB
 Name: localhost.localdomain
 ID: NZ2I:JHCL:QBPJ:LWSM:ZNF5:UCIH:7NKG:LROY:YPEU:2ACZ:W7IF:AN3H
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Registry: https://index.docker.io/v1/
 Labels:
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Registry Mirrors:
  https://8xpk5wnt.mirror.aliyuncs.com/
 Live Restore Enabled: false

查看镜像详细详细

docker image inspect 镜像id 获取 镜像id前3位

查看:id=300e315adb2f 的centos镜像详细详细

命令:docker image inspect 300

 [root@localhost ~]# docker images
  REPOSITORY TAG IMAGE ID CREATED SIZE
  redis latest 02c7f2054405 2 days ago 105MB
  nginx latest 822b7ec2aaf2 2 days ago 133MB
  ubuntu latest fb52e22af1b0 5 days ago 72.8MB
  nginx <none> 08b152afcfae 6 weeks ago 133MB
  centos latest 300e315adb2f 9 months ago 209MB


[root@localhost ~]# docker image inspect 300 [ { "Id": "sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55", "RepoTags": [ "centos:latest" ], "RepoDigests": [], "Parent": "", "Comment": "", "Created": "2020-12-08T00:22:53.076477777Z", "Container": "395e0bfa7301f73bc994efe15099ea56b8836c608dd32614ac5ae279976d33e4", "ContainerConfig": { "Hostname": "395e0bfa7301", "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 [\"/bin/bash\"]" ], "Image": "sha256:6de05bdfbf9a9d403458d10de9e088b6d93d971dd5d48d18b4b6758f4554f451", "Volumes": null, "WorkingDir": "", "Entrypoint": null, "OnBuild": null, "Labels": { "org.label-schema.build-date": "20201204", "org.label-schema.license": "GPLv2", "org.label-schema.name": "CentOS Base Image", "org.label-schema.schema-version": "1.0", "org.label-schema.vendor": "CentOS" } }, "DockerVersion": "19.03.12", "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": [ "/bin/bash" ], "Image": "sha256:6de05bdfbf9a9d403458d10de9e088b6d93d971dd5d48d18b4b6758f4554f451", "Volumes": null, "WorkingDir": "", "Entrypoint": null, "OnBuild": null, "Labels": { "org.label-schema.build-date": "20201204", "org.label-schema.license": "GPLv2", "org.label-schema.name": "CentOS Base Image", "org.label-schema.schema-version": "1.0", "org.label-schema.vendor": "CentOS" } }, "Architecture": "amd64", "Os": "linux", "Size": 209348104, "VirtualSize": 209348104, "GraphDriver": { "Data": { "MergedDir": "/var/lib/docker/overlay2/feca59a0b79fb06342e35b6087d2c864f228621c2b6dc9692274bb264468ac0f/merged", "UpperDir": "/var/lib/docker/overlay2/feca59a0b79fb06342e35b6087d2c864f228621c2b6dc9692274bb264468ac0f/diff", "WorkDir": "/var/lib/docker/overlay2/feca59a0b79fb06342e35b6087d2c864f228621c2b6dc9692274bb264468ac0f/work" }, "Name": "overlay2" }, "RootFS": { "Type": "layers", "Layers": [ "sha256:2653d992f4ef2bfd27f94db643815aa567240c37732cae1405ad1c1309ee9859" ] }, "Metadata": { "LastTagTime": "0001-01-01T00:00:00Z" } } ]

 

posted on 2021-09-05 18:02  赛兔子  阅读(492)  评论(0编辑  收藏  举报

导航