将 Spring Boot 应用和依赖打成一个胖 JAR,编辑 pom.xml 文件:
<build> <finalName>app</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> <mainClass>com.dyingbleed.server.Application</mainClass> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
编译并运行,确保应用正常启动:
mvn clean package && java -jar target/app.jar
新建 Dockerfile 文件:
FROM openjdk:8-jdk-alpine EXPOSE 8080 COPY target/app.jar app.jar ENTRYPOINT ["java", "-jar", "/app.jar"]
构建 Docker 镜像:
docker build -t app