Spring Boot微服务无法创建Docker镜像问题求助
执行./mvnw spring-boot:build-image -DskipTests构建Spring Boot微服务Docker镜像时失败,报错提示Docker API调用localhost/v1.24/images/docker.io/paketobuildpacks/builder:base/json返回404 Not Found。
控制台输出:
[INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 06:09 min [INFO] Finished at: 2022-09-13T14:43:50+03:00 [INFO] ------------------------------------------------------------------------ [ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.7.3:build-image (default-cli) on project currency-exchange-service: Execution default-cli of goal org.springframework.boot:spring-boot-maven-plugin:2.7.3:build-image failed: Docker API call to 'localhost/v1.24/images/docker.io/paketobuildpacks/builder:base/json' failed with status code 404 "Not Found" -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginExecutionException
项目pom.xml内容:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.3</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>currency-exchange-service</artifactId> <version>0.0.1-SNAPSHOT</version> <name>currency-exchange-service</name> <description>Demo project for Spring Boot</description> <properties> <java.version>17</java.version> <spring-cloud.version>2021.0.4</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>io.github.resilience4j</groupId> <artifactId>resilience4j-spring-boot2</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-sleuth</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-sleuth-zipkin</artifactId> </dependency> <!--<dependency> <groupId>org.springframework.amqp</groupId> <artifactId>spring-rabbit</artifactId> </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> <configuration> <image> <name>in28min/mmv2-${project.artifactId}:${project.version}</name> </image> <pullPolicy>IF_NOT_PRESENT</pullPolicy> </configuration> </plugin> </plugins> </build> </project>
这个错误是因为Spring Boot Buildpacks无法找到指定的Paketo builder镜像,以下是几个可行的解决办法:
手动拉取builder镜像
直接用Docker命令拉取对应的builder镜像:docker pull paketobuildpacks/builder:base拉取完成后重新执行Maven构建命令。
修改pullPolicy为ALWAYS
在pom.xml的spring-boot-maven-plugin配置中,将pullPolicy从IF_NOT_PRESENT改为ALWAYS,强制Maven每次构建都拉取最新的builder镜像:<configuration> <image> <name>in28min/mmv2-${project.artifactId}:${project.version}</name> </image> <pullPolicy>ALWAYS</pullPolicy> </configuration>指定兼容的builder镜像版本
可能base标签对应的镜像已被移除或更新,尝试指定具体的版本标签,比如针对Java 17的builder:<configuration> <image> <name>in28min/mmv2-${project.artifactId}:${project.version}</name> </image> <builder>paketobuildpacks/builder:base-platform-api-0.3</builder> <pullPolicy>IF_NOT_PRESENT</pullPolicy> </configuration>或者使用
jammy-base镜像(适用于较新的Spring Boot版本):<builder>paketobuildpacks/builder:jammy-base</builder>检查Docker Daemon连接状态
确保Docker Daemon正在运行,且Maven能正确连接到Docker API。可以执行docker info命令验证Docker是否正常工作,若使用Docker Desktop需确认已启动。
内容的提问来源于stack exchange,提问作者Sercan Noyan Germiyanoğlu

