如何通过Spring Boot的build-image添加容器标签?
使用spring-boot-maven-plugin添加Docker镜像元数据标签
当使用spring-boot-maven-plugin的mvn spring-boot:build-image命令构建镜像时,你可以直接在插件配置中添加自定义元数据标签,无需编写Dockerfile,具体操作如下:
配置pom.xml
在项目的pom.xml里,找到spring-boot-maven-plugin的配置节点,通过<image><labels>子节点定义所需的元数据标签:
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <image> <!-- 自定义镜像元数据标签 --> <labels> <label>title=My Spring Boot App</label> <label>version=1.0.0-SNAPSHOT</label> <label>authors=stewart</label> </labels> </image> </configuration> </plugin> </plugins> </build>
构建镜像
执行常规的镜像构建命令即可:
mvn spring-boot:build-image
验证标签
镜像构建完成后,使用和Dockerfile构建后相同的命令查看标签:
docker inspect --format='{{json .Config.Labels}}' my-spring-boot-image:latest
执行后就能看到你配置的title、version、authors等标签信息。
内容的提问来源于stack exchange,提问作者Stewart
相关产品推荐
相关产品推荐

