崔志伟博客

Docker镜像列表中的none:none是什么

在构建过Docker镜像的电脑上查看本地镜像列表,有可能看到下图红框中的镜像,在列表中展示为<none>:<none>
在这里插入图片描述
这种镜像在Docker官方文档中被称作dangling images,指的是没有标签并且没有被容器使用的镜像。

官方解释

来自官方的解释如下图红框所示,地址是:https://docs.docker.com/config/pruning/
在这里插入图片描述

怎么来的

结合第一幅图,梳理一下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,在镜像列表中展示为<none>:<none>

准备实战

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

操作的主要内容是将一个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这个文件夹下,如下图红框所示:
在这里插入图片描述
springcloudscaledemo文件夹内有三个工程,本次实战用到的是eureka-server,如下图:
在这里插入图片描述

实战操作

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

  1. 基于Maven创建一个springboot工程,pom.xml内容如下,主要注意plugins节点中的插件,该插件是用来构建镜像的:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.bolingcavalry</groupId>
	<artifactId>eureka-server</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>eureka-server</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.9.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
		<spring-cloud.version>Edgware.SR1</spring-cloud.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka-server</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>${spring-cloud.version}</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>

			<!--新增的docker maven插件-->
			<plugin>
				<groupId>com.spotify</groupId>
				<artifactId>docker-maven-plugin</artifactId>
				<version>0.4.12</version>
				<!--docker镜像相关的配置信息-->
				<configuration>
					<!--镜像名,这里用工程名-->
					<imageName>bolingcavalry/${project.artifactId}</imageName>
					<!--TAG,这里用工程版本号-->
					<imageTags>
						<imageTag>${project.version}</imageTag>
					</imageTags>
					<!--镜像的FROM,使用java官方镜像-->
					<baseImage>java:8u111-jdk</baseImage>
					<!--该镜像的容器启动后,直接运行spring boot工程-->
					<entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>
					<!--构建镜像的配置信息-->
					<resources>
						<resource>
							<targetPath>/</targetPath>
							<directory>${project.build.directory}</directory>
							<include>${project.build.finalName}.jar</include>
						</resource>
					</resources>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>
  1. application.properties内容如下,可见该应用其实就是springcloud中的eureka身份:
server.port=8080

eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
  1. 启动类是EurekaServerApplication,内容如下:
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

	public static void main(String[] args) {
		SpringApplication.run(EurekaServerApplication.class, args);
	}
}

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

构建镜像

  1. 在pom.xml所在目录执行以下命令即可构建镜像:
mvn clean package -U -DskipTests docker:build
  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 images
REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE
bolingcavalry/eureka-server   0.0.1-SNAPSHOT      90b736eb388e        5 seconds ago       683MB
bolingcavalry/eureka-server   latest              90b736eb388e        5 seconds ago       683MB
java                          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 images
REPOSITORY                    TAG                 IMAGE ID            CREATED              SIZE
bolingcavalry/eureka-server   0.0.1-SNAPSHOT      be262f101e2c        About a minute ago   683MB
bolingcavalry/eureka-server   latest              be262f101e2c        About a minute ago   683MB
<none>                        <none>              90b736eb388e        9 minutes ago        683MB
java                          8u111-jdk           d23bdf5b1b1b        2 years ago          643MB

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

清理dangling images

  1. 如下所示,执行命令docker image prune即可删除dangling images:
root@hedy:/home/willzhao/temp/201906/01/eureka-server# docker images
REPOSITORY                    TAG                 IMAGE ID            CREATED              SIZE
bolingcavalry/eureka-server   0.0.1-SNAPSHOT      be262f101e2c        About a minute ago   683MB
bolingcavalry/eureka-server   latest              be262f101e2c        About a minute ago   683MB
<none>                        <none>              90b736eb388e        9 minutes ago        683MB
java                          8u111-jdk           d23bdf5b1b1b        2 years ago          643MB
root@hedy:/home/willzhao/temp/201906/01/eureka-server# docker image prune
WARNING! This will remove all dangling images.
Are you sure you want to continue? [y/N] y
Deleted Images:
deleted: sha256:90b736eb388e42df2e4bc2ab3c8770a9a5f7563bb5af2493b88428c610f14f6b
deleted: sha256:db3657b5e27b7bf6c780e3280d6f2e24ffc26592a14b4efa651000130294b429
deleted: sha256:afd10ec003e3132ea5e1c489ba5e51f53b0759351bdc175184ba82daaac178d1

Total reclaimed space: 39.94MB
  1. 再次查看,发现90b736eb388e已经不在列表中:
root@hedy:/home/willzhao/temp/201906/01/eureka-server# docker images
REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE
bolingcavalry/eureka-server   0.0.1-SNAPSHOT      be262f101e2c        39 minutes ago      683MB
bolingcavalry/eureka-server   latest              be262f101e2c        39 minutes ago      683MB
java                          8u111-jdk           d23bdf5b1b1b        2 years ago         643MB

一点疑问

再来看看官方对dangling images的解释,如下所示,有两个条件:没有标签、并且不再被容器使用:
在这里插入图片描述
我的疑问:如果没有标签,但是正在被容器使用的镜像,应该不算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在列表中已经显示成了<none>:<none>
root@hedy:/home/willzhao/temp/201906/01/eureka-server# docker images
REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE
bolingcavalry/eureka-server   0.0.1-SNAPSHOT      f83762738051        4 seconds ago       683MB
bolingcavalry/eureka-server   latest              f83762738051        4 seconds ago       683MB
<none>                        <none>              be262f101e2c        About an hour ago   683MB
java                          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 ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
f4e9b7aa6d25        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 prune
WARNING! This will remove all dangling images.
Are you sure you want to continue? [y/N] y
Total reclaimed space: 0B
root@hedy:/home/willzhao/temp/201906/01/eureka-server# docker images
REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE
bolingcavalry/eureka-server   0.0.1-SNAPSHOT      f83762738051        5 minutes ago       683MB
bolingcavalry/eureka-server   latest              f83762738051        5 minutes ago       683MB
<none>                        <none>              be262f101e2c        About an hour ago   683MB
java                          8u111-jdk           d23bdf5b1b1b        2 years ago         643MB

来源:https://blog.csdn.net/boling_cavalry/article/details/90727359

上一篇: Docker多阶段构建实战(multi-stage builds)

下一篇: 本地服务调用K8S环境中的SpringCloud微服务实战