SpringBoot项目(单模块、多模块)使用docker容器运行jar包镜像(踩坑)
1.mavem docker插件配置
<!--docker 插件配置-->
<build>
<finalName>生成jar包的名称</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.2.3</version> <configuration>
<imageName>${project.artifactId}</imageName>
<!--docker的配置文件所在目录-->
<dockerDirectory>src/main/docker</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
</plugins>
</build>
2.DockerFile配置文件
VOLUME /tmp
ADD XXXX.jar app.jar
RUN bash -c 'touch /app.jar'
ENTRYPOINT ["java","-D java.security.egd=file:/dev/./urandom","-jar","/app.jar"]
3.配置完成后使用maven命令生成docker镜像(单模块项目情况)
#! /bin/bash
#首先移除原来的镜像
docker stop eureka-server
docker rm eureka-server
docker rmi eureka-server
#找到项目所在目录
cd /home/ubuntu/java/jenkins/springcloud/eureka-server/
#执行maven命令生成jar包和镜像
mvn clean package -Ptest -Dmaven.test.skip=true docker:build
#查看镜像是否存在
docker images
#运行镜像
docker run -p 8761:8761 --name eureka-server -d eureka-server
4.配置完成后使用maven命令生成docker镜像(多模块项目情况)
以springboot Web项目为例
只在web 模块添加Maven docker插件配置以及Dockerfile
#! /bin/bash
#首先移除原来的镜像
docker stop boot
docker rm boot
docker rmi boot
#找到项目所在目录
cd /home/ubuntu/java/jenkins/springboot/
#根目录下进行 install
mvn clean install package -Dmaven.test.skip
#进入web模块下
cd bootweb/
#执行maven命令生成jar包和镜像
mvn package docker:build -Dmaven.test.skip
#查看镜像是否存在
docker images
#运行镜像
docker run -p 8080:8080 --name boot -d boot