从 spring maven 项目为 arm64 平台构建 Docker 图像
Building Docker image from spring maven project for arm64 platform
我正在上 Spring 课程,我有一个从 spring initializr 构建的 Maven 应用程序(从课程资源下载)。我可以用 mvn spring-boot:build-image
构建本地 Docker 图像(项目中没有 Docker 文件)。默认情况下,Docker 图像构建为 linux/amd64,但我使用的是 M1 Apple Silicon 芯片 (arm64)。我一直在寻找许多解决方法,但没有成功。最后,我发现也许可以添加一个 Docker 文件并指定它会相应地构建图像的平台。
我的目标是为 arm64 架构构建 docker 映像。
所以,我创建了一个 Docker 文件:
FROM --platform=linux/arm64 maven:3.8.4-jdk-11 AS build
COPY src /home/path_to_app/src
COPY pom.xml /home/path_to_app
RUN mvn -f /home/path_to_app/pom.xml spring-boot:build-image -DskipTests
我的项目结构是:
standard maven structure
在终端中,在同一个 pom.xml 目录中 运行:docker buildx build --platform linux/arm64 --tag <mytag> .
记录成功,直到 => 构建映像 'docker.io/myusername/myimage:mytag',
然后出现异常,构建失败。
#8 125.7 [INFO]
#8 125.7 [INFO] <<< spring-boot-maven-plugin:2.5.0:build-image (default-cli) < package @ currency-exchange-service <<<
#8 125.7 [INFO]
#8 125.7 [INFO]
#8 125.7 [INFO] --- spring-boot-maven-plugin:2.5.0:build-image (default-cli) @ currency-exchange-service ---
#8 125.8 [INFO] Building image 'docker.io/myname/myimage:0.0.1-SNAPSHOT'
#8 125.8 [INFO]
#8 125.8 [INFO] I/O exception (java.io.IOException) caught when processing request to {}->docker://localhost:2376: com.sun.jna.LastErrorException: [2] No such file or directory
#8 125.8 [INFO] Retrying request to {}->docker://localhost:2376
#8 125.8 [INFO] I/O exception (java.io.IOException) caught when processing request to {}->docker://localhost:2376: com.sun.jna.LastErrorException: [2] No such file or directory
#8 125.8 [INFO] Retrying request to {}->docker://localhost:2376
#8 125.8 [INFO] I/O exception (java.io.IOException) caught when processing request to {}->docker://localhost:2376: com.sun.jna.LastErrorException: [2] No such file or directory
#8 125.8 [INFO] Retrying request to {}->docker://localhost:2376
#8 125.9 [INFO] ------------------------------------------------------------------------
#8 125.9 [INFO] BUILD FAILURE
#8 125.9 [INFO] ------------------------------------------------------------------------
#8 125.9 [INFO] Total time: 02:04 min
#8 125.9 [INFO] Finished at: 2022-03-22T21:08:20Z
#8 125.9 [INFO] ------------------------------------------------------------------------
#8 125.9 [ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.5.0:build-image
(default-cli) on project currency-exchange-service: Execution default-cli
of goal org.springframework.boot:spring-boot-maven-plugin:2.5.0:build-image
failed: Connection to the Docker daemon at 'localhost' failed with error "[2]
No such file or directory"; ensure the Docker daemon is running and accessible:
com.sun.jna.LastErrorException: [2] No such file or directory -> [Help 1]
我的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.5.0</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.somename.microservices</groupId>
<artifactId>mymicroservice-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mymicroservice-service-docker</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
<spring-cloud.version>2020.0.2</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.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-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>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-spring-boot2</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>
</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>myusername/myprefix${project.artifactId}:${project.version}</name>
</image>
<pullPolicy>IF_NOT_PRESENT</pullPolicy>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
</project>
我的 docker 守护程序是 运行ning。无法弄清楚为什么它试图到达 localhost:2376 (我的项目中没有这样的端口)。
我的 DOCKER_HOST 设置为 unix:///var/run/docker.sock 并且工作正常。
试图添加到 daemon.json:
{
"hosts": ["tcp://127.0.0.1:2376", "unix:///var/run/docker.sock"]
}
但是docker守护进程无法初始化(绑定错误,端口已被使用)
我不知道com.sun.jna.LastErrorException
从哪里来。
我怀疑可能需要 docker (dind) 中的 Docker,但我不知道如何在我的 Docker文件。或者可能是端口绑定配置。
在此先感谢您的帮助!!!
注意:我只想在本地构建镜像。我不想推送到 docker 注册表。我不希望构建过程中有任何其他阶段。
目前无法使用 mvn spring-boot:build-image
构建 ARM-based 图像,因为 Spring Boot 集成的 Cloud Native Buildpacks 构建器不支持此功能。这是Paketo buildpacks 2022 roadmap可能关注的项目之一,您可以投票给它。
CNBdocuments a work-around为此,但设置并不简单运行。
RUN mvn -f /home/path_to_app/pom.xml spring-boot:build-image -DskipTests
你需要 Docker-in-Docker 才能完成这样的工作,因为 Docker 容器内 运行 的 CNB 构建器进程需要与 Docker 通信守护进程。无论如何,由于上述原因,这不允许您构建 ARM 映像。
我正在上 Spring 课程,我有一个从 spring initializr 构建的 Maven 应用程序(从课程资源下载)。我可以用 mvn spring-boot:build-image
构建本地 Docker 图像(项目中没有 Docker 文件)。默认情况下,Docker 图像构建为 linux/amd64,但我使用的是 M1 Apple Silicon 芯片 (arm64)。我一直在寻找许多解决方法,但没有成功。最后,我发现也许可以添加一个 Docker 文件并指定它会相应地构建图像的平台。
我的目标是为 arm64 架构构建 docker 映像。
所以,我创建了一个 Docker 文件:
FROM --platform=linux/arm64 maven:3.8.4-jdk-11 AS build
COPY src /home/path_to_app/src
COPY pom.xml /home/path_to_app
RUN mvn -f /home/path_to_app/pom.xml spring-boot:build-image -DskipTests
我的项目结构是:
standard maven structure
在终端中,在同一个 pom.xml 目录中 运行:docker buildx build --platform linux/arm64 --tag <mytag> .
记录成功,直到 => 构建映像 'docker.io/myusername/myimage:mytag', 然后出现异常,构建失败。
#8 125.7 [INFO]
#8 125.7 [INFO] <<< spring-boot-maven-plugin:2.5.0:build-image (default-cli) < package @ currency-exchange-service <<<
#8 125.7 [INFO]
#8 125.7 [INFO]
#8 125.7 [INFO] --- spring-boot-maven-plugin:2.5.0:build-image (default-cli) @ currency-exchange-service ---
#8 125.8 [INFO] Building image 'docker.io/myname/myimage:0.0.1-SNAPSHOT'
#8 125.8 [INFO]
#8 125.8 [INFO] I/O exception (java.io.IOException) caught when processing request to {}->docker://localhost:2376: com.sun.jna.LastErrorException: [2] No such file or directory
#8 125.8 [INFO] Retrying request to {}->docker://localhost:2376
#8 125.8 [INFO] I/O exception (java.io.IOException) caught when processing request to {}->docker://localhost:2376: com.sun.jna.LastErrorException: [2] No such file or directory
#8 125.8 [INFO] Retrying request to {}->docker://localhost:2376
#8 125.8 [INFO] I/O exception (java.io.IOException) caught when processing request to {}->docker://localhost:2376: com.sun.jna.LastErrorException: [2] No such file or directory
#8 125.8 [INFO] Retrying request to {}->docker://localhost:2376
#8 125.9 [INFO] ------------------------------------------------------------------------
#8 125.9 [INFO] BUILD FAILURE
#8 125.9 [INFO] ------------------------------------------------------------------------
#8 125.9 [INFO] Total time: 02:04 min
#8 125.9 [INFO] Finished at: 2022-03-22T21:08:20Z
#8 125.9 [INFO] ------------------------------------------------------------------------
#8 125.9 [ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.5.0:build-image
(default-cli) on project currency-exchange-service: Execution default-cli
of goal org.springframework.boot:spring-boot-maven-plugin:2.5.0:build-image
failed: Connection to the Docker daemon at 'localhost' failed with error "[2]
No such file or directory"; ensure the Docker daemon is running and accessible:
com.sun.jna.LastErrorException: [2] No such file or directory -> [Help 1]
我的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.5.0</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.somename.microservices</groupId>
<artifactId>mymicroservice-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mymicroservice-service-docker</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
<spring-cloud.version>2020.0.2</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.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-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>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-spring-boot2</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>
</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>myusername/myprefix${project.artifactId}:${project.version}</name>
</image>
<pullPolicy>IF_NOT_PRESENT</pullPolicy>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
</project>
我的 docker 守护程序是 运行ning。无法弄清楚为什么它试图到达 localhost:2376 (我的项目中没有这样的端口)。 我的 DOCKER_HOST 设置为 unix:///var/run/docker.sock 并且工作正常。 试图添加到 daemon.json:
{
"hosts": ["tcp://127.0.0.1:2376", "unix:///var/run/docker.sock"]
}
但是docker守护进程无法初始化(绑定错误,端口已被使用)
我不知道com.sun.jna.LastErrorException
从哪里来。
我怀疑可能需要 docker (dind) 中的 Docker,但我不知道如何在我的 Docker文件。或者可能是端口绑定配置。
在此先感谢您的帮助!!!
注意:我只想在本地构建镜像。我不想推送到 docker 注册表。我不希望构建过程中有任何其他阶段。
目前无法使用 mvn spring-boot:build-image
构建 ARM-based 图像,因为 Spring Boot 集成的 Cloud Native Buildpacks 构建器不支持此功能。这是Paketo buildpacks 2022 roadmap可能关注的项目之一,您可以投票给它。
CNBdocuments a work-around为此,但设置并不简单运行。
RUN mvn -f /home/path_to_app/pom.xml spring-boot:build-image -DskipTests
你需要 Docker-in-Docker 才能完成这样的工作,因为 Docker 容器内 运行 的 CNB 构建器进程需要与 Docker 通信守护进程。无论如何,由于上述原因,这不允许您构建 ARM 映像。