mac springboot com.spotify Docker 容器化部署
项目目录如下
sh
qiyu-live-user-provider/
├── docker/
│ └── Dockerfile
├── target/
│ └── qiyu-live-user-provider-docker.jar
├── src/
├── pom.xml
└── 其他文件...
会遇到如下几种错误
- java.lang.UnsatisfiedLinkError 解决方案 或者使用下面的
Mac电脑
sh
Failed to execute goal com.spotify:dockerfile-maven-plugin:1.4.13:build (default) on project my-project: Could not build image: java.util.concurrent.ExecutionException: com.spotify.docker.client.shaded.javax.ws.rs.ProcessingException: java.lang.UnsatisfiedLinkError: could not load FFI provider com.spotify.docker.client.shaded.jnr.ffi.provider.jffi.Provider: ExceptionInInitializerError: Can't overwrite cause with java.lang.UnsatisfiedLinkError: java.lang.UnsatisfiedLinkError: /private/var/folders/h8/cj2lhn7n2wx3vd38y5q71fc00000gn/T/jffi14954787931679265064.dylib: dlopen(/private/var/folders/h8/cj2lhn7n2wx3vd38y5q71fc00000gn/T/jffi14954787931679265064.dylib, 0x0001): tried: '/private/var/folders/h8/cj2lhn7n2wx3vd38y5q71fc00000gn/T/jffi14954787931679265064.dylib' (fat file, but missing compatible architecture (have 'i386,x86_64', need 'arm64e')), '/usr/lib/jffi14954787931679265064.dylib' (no such file)
[ERROR]
- 报错无效的发行版解决办法 解决方案
sh
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project xxxxx: Fatal error compiling: 无效的目标发行版: 17 -> [Help 1]
非Mac电脑
- qiyu-live-user-provider的pom中加入以下代码
xml
<build>
<finalName>qiyu-live-user-provider-docker</finalName>
<plugins>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.2.0</version>
<executions>
<!-- 当mvn执行install操作的时候,执行docker的build -->
<execution>
<id>build</id>
<phase>install</phase>
<goals>
<goal>build</goal>
<goal>push</goal>
</goals>
</execution>
</executions>
<configuration>
<imageTags>
<imageTag>${project.version}</imageTag>
</imageTags>
<imageName>${docker.registry.address}/${docker.registry.namespace}/${project.build.finalName}</imageName>
<!--指定Dockerfile文件的位置-->
<dockerDirectory>${project.basedir}/docker</dockerDirectory>
<resources>
<!-- 指定jar包路径,这里对应Dockerfile中复制 jar 包到 docker 容器指定目录配置,也可以写到 Docokerfile 中 -->
<resource>
<targetPath>/</targetPath>
<!-- 将下边目录的内容,拷贝到docker镜像中 -->
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
<resource>
<targetPath>/</targetPath>
<directory>${arthus.zip.address}</directory>
<include>arthas-bin.zip</include>
</resource>
</resources>
</configuration>
</plugin>
<!-- 将SpringBoot应用打包成jar-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
- 在qiyu-live-user-provide目录下创建docker文件夹并且创建Dockerfile,文件内容如下
sh
#如果非Mac电脑使用 openjdk:17-jdk-alpine
FROM openjdk:17-jdk-alpine
#如果Mac电脑使用 eclipse-temurin:17-jdk-centos7 或者 eclipse-temurin:17-jdk,但是建议使用eclipse-temurin:17-jdk 因为很多工具命令eclipse-temurin:17-jdk-centos7创建的容器里面是没有的
#FROM eclipse-temurin:17-jdk-centos7
#FROM eclipse-temurin:17-jdk
VOLUME /tmp
COPY ../target/arthas-packaging-3.7.2-bin.zip arthas-bin.zip
COPY ../target/qiyu-live-user-provider-docker.jar app.jar
ENV JAVA_OPTS="\
-server \
-Xmx1g \
-Xms1g \
-Xmn256m"
ENTRYPOINT java ${JAVA_OPTS} -Djava.security.egd=file:/dev/./urandom --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.base/java.util.concurrent=ALL-UNNAMED --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED --add-opens=java.base/java.lang.reflect=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.base/java.math=ALL-UNNAMED -jar app.jar
- 在控制台执行如下命令
sh
#cd 到 qiyu-live-user-provider目录下
cd qiyu-live-user-provider
#确保Java版本是17
java -version
#执行打包命令并生成镜像文件
mvn clean
mvn install
#查看生成的镜像文件
docker images
Mac电脑
- qiyu-live-user-provider的pom中加入以下代码
注意事项:三个插件的顺序一点要按照以下顺序 不然会出错 不是打包失败找不到arthas-packaging-3.7.2-bin.zip,就是构建的镜像文件jar文件不完整
xml
<build>
<finalName>qiyu-live-user-provider-docker</finalName>
<plugins>
<!-- 将SpringBoot应用打包成jar-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<!--有关参考exec-maven-plugin的使用 https://zhuanlan.zhihu.com/p/686297981-->
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>copy-file</id>
<phase>package</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<workingDirectory>${project.basedir}</workingDirectory>
<executable>cp</executable>
<arguments>
<argument>/Users/zhujianmin/Documents/NoiICloud.nosync/work/local/arthus/arthas-packaging-3.7.2-bin.zip</argument>
<argument>target/</argument>
</arguments>
</configuration>
</execution>
<execution>
<id>docker-build</id>
<phase>package</phase>
<!--如果打包之后镜像文件缺少 jar文件 需要直接重新package 感觉问题在于jar文件还没有生成完整 镜像就已经开始构建了-->
<goals>
<goal>exec</goal>
</goals>
<configuration>
<workingDirectory>${project.basedir}</workingDirectory>
<executable>docker</executable>
<arguments>
<argument>build</argument>
<argument>.</argument>
<argument>-t</argument>
<argument>${docker.registry.address}/${docker.registry.namespace}/${project.build.finalName}:${project.version}</argument>
<argument>-f</argument>
<argument>docker/Dockerfile</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
- 在qiyu-live-user-provide目录下创建docker文件夹并且创建Dockerfile,文件内容如下
sh
#如果非Mac电脑使用 openjdk:17-jdk-alpine
#FROM openjdk:17-jdk-alpine
#如果Mac电脑使用 eclipse-temurin:17-jdk-centos7 或者 eclipse-temurin:17-jdk,但是建议使用eclipse-temurin:17-jdk 因为很多工具命令eclipse-temurin:17-jdk-centos7创建的容器里面是没有的
#FROM eclipse-temurin:17-jdk-centos7
FROM eclipse-temurin:17-jdk
VOLUME /tmp
COPY ../target/arthas-packaging-3.7.2-bin.zip arthas-bin.zip
COPY ../target/qiyu-live-user-provider-docker.jar app.jar
ENV JAVA_OPTS="\
-server \
-Xmx1g \
-Xms1g \
-Xmn256m"
ENTRYPOINT java ${JAVA_OPTS} -Djava.security.egd=file:/dev/./urandom --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.base/java.util.concurrent=ALL-UNNAMED --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED --add-opens=java.base/java.lang.reflect=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.base/java.math=ALL-UNNAMED -jar app.jar
- 在控制台执行如下命令
sh
#cd 到 qiyu-live-user-provider目录下
cd qiyu-live-user-provider
#确保Java版本是17
java -version
#执行打包命令并生成镜像文件
mvn clean
mvn install
#查看生成的镜像文件
docker images
不管是Mac还是非Mac电脑
直接通过docker build命令构建
- 新建Dockerfile文件
sh
#如果非Mac电脑使用 openjdk:17-jdk-alpine
#FROM openjdk:17-jdk-alpine
#如果Mac电脑使用 eclipse-temurin:17-jdk-centos7 或者 eclipse-temurin:17-jdk,但是建议使用eclipse-temurin:17-jdk 因为很多工具命令eclipse-temurin:17-jdk-centos7创建的容器里面是没有的
#FROM eclipse-temurin:17-jdk-centos7
FROM eclipse-temurin:17-jdk
VOLUME /tmp
COPY ../target/arthas-packaging-3.7.2-bin.zip arthas-bin.zip
COPY ../target/qiyu-live-user-provider-docker.jar app.jar
ENV JAVA_OPTS="\
-server \
-Xmx1g \
-Xms1g \
-Xmn256m"
ENTRYPOINT java ${JAVA_OPTS} -Djava.security.egd=file:/dev/./urandom --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.base/java.util.concurrent=ALL-UNNAMED --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED --add-opens=java.base/java.lang.reflect=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.base/java.math=ALL-UNNAMED -jar app.jar
- 将
qiyu-live-user-provider-docker.jar
copy到docker目录下 - 执行构建命令
sh
cd qiyu-live-user-provider/docker
docker build -t registry.baidubce.com/qiyu-live-test/qiyu-live-user-provider-docker:1.0.1 .