欢迎访问我的GitHub

https://github.com/zq2599/blog_demos

内容:原创文章分类汇总及配套源码,涉及Java、Docker、K8S、Devops等

在构建过Docker镜像的电脑上查看本地镜像列表,有可能看到下图红框中的镜像,在列表中展示为::

docker python3 镜像 docker镜像none_docker删除none镜像

这种镜像在Docker官方文档中被称作dangling images,指的是没有标签并且没有被容器使用的镜像。

官方解释

来自官方的解释如下图红框所示,地址是:https://docs.docker.com/config/pruning/

docker python3 镜像 docker镜像none_docker删除none镜像_02

怎么来的

结合第一幅图,梳理一下dangling images怎么产生的:

  1. 第一次构建镜像时生成的镜像ID为079dbd67f9f4,此镜像会被构建工具加上标签bolingcavalry/eureka-server:0.0.1-SNAPSHOT;
  2. 第二次构建镜像时生成的镜像ID为e40a97f764ef,此镜像会被构建工具加上标签bolingcavalry/eureka-server:0.0.1-SNAPSHOT,
  3. Docker会移除079dbd67f9f4的标签,此时079dbd67f9f4就变成了dangling images,在镜像列表中展示为:

准备实战

接下来通过实际操作来复现此问题,并做一些有趣的测试;

操作的主要内容是将一个maven工程构建成Docker镜像,以下是环境信息:

  1. 操作系统:Ubuntu 18.04.2 LTS
  2. Docker:18.06.1-ce
  3. Java:1.8.0_191
  4. Maven:3.6.0

实战源码下载

如果您不想写代码,也可以在Github下载整个maven工程,地址和链接信息如下表所示:

名称

链接

备注

项目主页

https://github.com/zq2599/blog_demos

该项目在GitHub上的主页

git仓库地址(https)

https://github.com/zq2599/blog_demos.git

该项目源码的仓库地址,https协议

git仓库地址(ssh)

git@github.com:zq2599/blog_demos.git

该项目源码的仓库地址,ssh协议

这个git项目中有多个文件夹,本章源码在springcloudscaledemo这个文件夹下,如下图红框所示:

docker python3 镜像 docker镜像none_docker删除none镜像_03

springcloudscaledemo文件夹内有三个工程,本次实战用到的是eureka-server,如下图:

docker python3 镜像 docker镜像none_spring_04

实战操作

接下来一起开发一个简单的java项目:

  1. 基于Maven创建一个springboot工程,pom.xml内容如下,主要注意plugins节点中的插件,该插件是用来构建镜像的:
<?xml version="1.0" encoding="UTF-8"?>	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">	4.0.0	com.bolingcavalry	eureka-server	0.0.1-SNAPSHOT	jar	eureka-server	Demo project for Spring Boot			org.springframework.boot		spring-boot-starter-parent		1.5.9.RELEASE						UTF-8		UTF-8		1.8		Edgware.SR1							org.springframework.cloud			spring-cloud-starter-eureka-server							org.springframework.boot			spring-boot-starter-test			test													org.springframework.cloud				spring-cloud-dependencies				${spring-cloud.version}				pom				import																org.springframework.boot				spring-boot-maven-plugin													com.spotify				docker-maven-plugin				0.4.12																		bolingcavalry/${project.artifactId}																${project.version}															java:8u111-jdk										["java", "-jar", "/${project.build.finalName}.jar"]																							/							${project.build.directory}							${project.build.finalName}.jar
  1. application.properties内容如下,可见该应用其实就是springcloud中的eureka身份:
server.port=8080eureka.instance.hostname=localhosteureka.client.register-with-eureka=falseeureka.client.fetch-registry=falseeureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
  1. 启动类是EurekaServerApplication,内容如下:
@SpringBootApplication@EnableEurekaServerpublic class EurekaServerApplication {	public static void main(String[] args) {		SpringApplication.run(EurekaServerApplication.class, args);	}}

现在工程已经创建好了,可以开始构建了;

构建镜像

  1. 在pom.xml所在目录执行以下命令即可构建镜像:
mvn clean package -U -DskipTests docker:dockerBuild
  1. 构建成功后,用docker images命令查看本地镜像列表如下,新增了ID为90b736eb388e的镜像:
[INFO] Built bolingcavalry/eureka-server[INFO] Tagging bolingcavalry/eureka-server with 0.0.1-SNAPSHOT[INFO] ------------------------------------------------------------------------[INFO] BUILD SUCCESS[INFO] ------------------------------------------------------------------------[INFO] Total time:  10.918 s[INFO] Finished at: 2019-06-01T08:36:59Z[INFO] ------------------------------------------------------------------------root@hedy:/home/willzhao/temp/201906/01/eureka-server# docker imagesREPOSITORY                    TAG                 IMAGE ID            CREATED             SIZEbolingcavalry/eureka-server   0.0.1-SNAPSHOT      90b736eb388e        5 seconds ago       683MBbolingcavalry/eureka-server   latest              90b736eb388e        5 seconds ago       683MBjava                          8u111-jdk           d23bdf5b1b1b        2 years ago         643MB
  1. 再次执行命令mvn clean package -U -DskipTests docker:build,也就是再构建一次;
  2. 构建完成后查看镜像列表,ID为90b736eb388e的镜像已经成为dangling images,标签bolingcavalry/eureka-server:0.0.1-SNAPSHOT已经被新镜像be262f101e2c占有:
[INFO] Built bolingcavalry/eureka-server[INFO] Tagging bolingcavalry/eureka-server with 0.0.1-SNAPSHOT[INFO] ------------------------------------------------------------------------[INFO] BUILD SUCCESS[INFO] ------------------------------------------------------------------------[INFO] Total time:  10.985 s[INFO] Finished at: 2019-06-01T08:44:49Z[INFO] ------------------------------------------------------------------------root@hedy:/home/willzhao/temp/201906/01/eureka-server# docker imagesREPOSITORY                    TAG                 IMAGE ID            CREATED              SIZEbolingcavalry/eureka-server   0.0.1-SNAPSHOT      be262f101e2c        About a minute ago   683MBbolingcavalry/eureka-server   latest              be262f101e2c        About a minute ago   683MB              90b736eb388e        9 minutes ago        683MBjava                          8u111-jdk           d23bdf5b1b1b        2 years ago          643MB

此时,如果您的镜像列表出现:,您也能分析出此现象的来源了,接下来试试如何清理dangling images。

清理dangling images

  1. 如下所示,执行命令docker image prune即可删除dangling images:
root@hedy:/home/willzhao/temp/201906/01/eureka-server# docker imagesREPOSITORY                    TAG                 IMAGE ID            CREATED              SIZEbolingcavalry/eureka-server   0.0.1-SNAPSHOT      be262f101e2c        About a minute ago   683MBbolingcavalry/eureka-server   latest              be262f101e2c        About a minute ago   683MB              90b736eb388e        9 minutes ago        683MBjava                          8u111-jdk           d23bdf5b1b1b        2 years ago          643MBroot@hedy:/home/willzhao/temp/201906/01/eureka-server# docker image pruneWARNING! This will remove all dangling images.Are you sure you want to continue? [y/N] yDeleted Images:deleted: sha256:90b736eb388e42df2e4bc2ab3c8770a9a5f7563bb5af2493b88428c610f14f6bdeleted: sha256:db3657b5e27b7bf6c780e3280d6f2e24ffc26592a14b4efa651000130294b429deleted: sha256:afd10ec003e3132ea5e1c489ba5e51f53b0759351bdc175184ba82daaac178d1Total reclaimed space: 39.94MB
  1. 再次查看,发现90b736eb388e已经不在列表中:
root@hedy:/home/willzhao/temp/201906/01/eureka-server# docker imagesREPOSITORY                    TAG                 IMAGE ID            CREATED             SIZEbolingcavalry/eureka-server   0.0.1-SNAPSHOT      be262f101e2c        39 minutes ago      683MBbolingcavalry/eureka-server   latest              be262f101e2c        39 minutes ago      683MBjava                          8u111-jdk           d23bdf5b1b1b        2 years ago         643MB

一点疑问

再来看看官方对dangling images的解释,如下所示,有两个条件:没有标签、并且不再被容器使用:

docker python3 镜像 docker镜像none_docker_05

我的疑问:如果没有标签,但是正在被容器使用的镜像,应该不算dangling images吧,此时如果执行命令docker image prune会怎么样呢?

我的猜测:docker image prune是用来清理dangling images的,如果镜像正在被使用那就不算dangling images,那就不会被清理掉;

还是动手来试试吧:

  1. 执行以下命令会用镜像bolingcavalry/eureka-server:0.0.1-SNAPSHOT创建一个容器:
docker run -idt bolingcavalry/eureka-server:0.0.1-SNAPSHOT
  1. 再次执行命令mvn clean package -U -DskipTests docker:build,也就是再构建一次;
  2. 查看镜像信息如下,此时be262f101e2c在列表中已经显示成了::
root@hedy:/home/willzhao/temp/201906/01/eureka-server# docker imagesREPOSITORY                    TAG                 IMAGE ID            CREATED             SIZEbolingcavalry/eureka-server   0.0.1-SNAPSHOT      f83762738051        4 seconds ago       683MBbolingcavalry/eureka-server   latest              f83762738051        4 seconds ago       683MB              be262f101e2c        About an hour ago   683MBjava                          8u111-jdk           d23bdf5b1b1b        2 years ago         643MB
  1. 执行命令docker ps查看容器,发现IMAGE字段已经变成了ID(之前是bolingcavalry/eureka-server:0.0.1-SNAPSHOT):
root@hedy:/home/willzhao/temp/201906/01/eureka-server# docker psCONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMESf4e9b7aa6d25        be262f101e2c        "java -jar /eureka-s…"   7 minutes ago       Up 7 minutes                            gallant_bell
  1. 执行命令docker image prune,再查看镜像列表,如下,可见be262f101e2c依旧在列表中,没有被清理掉,符合之前的猜测:被容器用到的镜像不是dangling images,用命令docker image prune无法清除:
root@hedy:/home/willzhao/temp/201906/01/eureka-server# docker image pruneWARNING! This will remove all dangling images.Are you sure you want to continue? [y/N] yTotal reclaimed space: 0Broot@hedy:/home/willzhao/temp/201906/01/eureka-server# docker imagesREPOSITORY                    TAG                 IMAGE ID            CREATED             SIZEbolingcavalry/eureka-server   0.0.1-SNAPSHOT      f83762738051        5 minutes ago       683MBbolingcavalry/eureka-server   latest              f83762738051        5 minutes ago       683MB              be262f101e2c        About an hour ago   683MBjava                          8u111-jdk           d23bdf5b1b1b        2 years ago         643MB

至此,对Docker镜像列表中的none:none已经了解,希望在您有类似疑惑时本文可以提供一些参考。