使用docker部署springBoot并且yml配置文件不打包到jar中
1.打包springBoot项目为demo.jar 以下贴上项目pom打包配置(使用idea的maven helper插件打包 )
<?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.1.9.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.docker</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <!-- maven install时跳过单元测试 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <skipTests>true</skipTests> </configuration> </plugin> </plugins> <resources> <resource> <filtering>true</filtering> <directory>src/main/resources</directory> <excludes> <exclude>**/*.yml</exclude> </excludes> </resource> </resources> </build> </project>
2.打包完复制jar包和配置文件到服务器中 以下是目录结构
config ->application.yml
lib -> demo2.jar
dockerfile
3.创建dockerfile
FROM openjdk:8-jdk-alpine COPY ./config /usr/web/live/config COPY ./lib /usr/web/live/lib EXPOSE 8080 ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom",\ "-jar",\ "-Dspring.config.location=/usr/web/live/config/application.yml",\ "/usr/web/live/lib/demo2.jar"]
4.创建镜像命令(记得用 sudo su获取最大权限操作)
docker build -t demo2 .
5.创建容器并运行
docker run --name demo2 -p 8035:8080 -d demo2
6.查看日志
docker logs -f -t demo2
效果演示
⎛⎝官萧何⎠⎞一只快乐的爪哇程序猿;邮箱:1570608034@qq.com