更方便的运行方式
在以前只能在Linux或者Windows系统中一一搭建所有应用程序的开发环境,包括MySQL,Redis,RabbitMQ等.但自从Docker出现之后,改变了这一切.开发环境和运行环境全都可以直接放到Docker容器中运行,最主要的是,搭建方便.基本只需一条命令就可以安装好.
关于Spring Boot的运行
完整源码: Gitee仓库
其实Spring Boot一般有两种运行方式:
- 放在Tomcat中,以war包的方式运行.
- 在Java命令行中,以jar包的方式运行.
Docker中,推荐以第二种方式更好,更方便一些.
推送方式
把Spring Boot打包之后的jar包,推送到Docker有两种方式:
- dockerfile Maven插件
- jib Maven插件
这里只介绍第一种用的比较多的dockerfile方式(但是,源码里面提供了两种方式的示例: Gitee仓库),源码如下:
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 http://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.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>xyz.suancaiyu</groupId> <artifactId>custom-spring-boot-docker-demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>custom-spring-boot-docker-demo</name> <description>Demo project for Spring Boot</description> <properties> <docker.image.prefix>192.168.37.130</docker.image.prefix> <java.version>12</java.version> </properties> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <!-- tag::plugin[] --> <plugin> <groupId>com.spotify</groupId> <artifactId>dockerfile-maven-plugin</artifactId> <version>1.4.9</version> <configuration> <repository>${docker.image.prefix}/${project.artifactId}</repository> </configuration> </plugin> <!-- end::plugin[] --> <!-- tag::unpack[] --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>unpack</id> <phase>package</phase> <goals> <goal>unpack</goal> </goals> <configuration> <artifactItems> <artifactItem> <groupId>${project.groupId}</groupId> <artifactId>${project.artifactId}</artifactId> <version>${project.version}</version> </artifactItem> </artifactItems> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>12</source> <target>12</target> </configuration> </plugin> <!-- end::unpack[] --> </plugins> </build> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> </project>
需要注意: docker.image.prefix 这个值是Docker服务端的地址,需要修改为自己的.
Dockerfile 文件:
定义程序的依赖,运行方式:
FROM openjdk:12-jdk-alpine
VOLUME /tmp
ARG DEPENDENCY=target/dependency
COPY ${DEPENDENCY}/BOOT-INF/lib /app/lib
COPY ${DEPENDENCY}/META-INF /app/META-INF
COPY ${DEPENDENCY}/BOOT-INF/classes /app
ENTRYPOINT [“java”,”-cp”,”app:app/lib/*”,”xyz.suancaiyu.customspringbootdockerdemo.CustomSpringBootDockerDemoApplication”]
编译并运行应用程序:
1. (Windows)在本地机器上配置环境变量:
DOCKER_HOST=192.168.37.130
2. (Windows)在当前目录执行下面的语句编译(推荐从菜单栏单独打开Power Shell,或者cmd.):
mvn install dockerfile:build
3. (Linux)在192.168.37.130的机器上运行(启动应用):
docker run -p 8080:8080 -t 192.168.37.130/custom-spring-boot-docker-demo
4. (Windows)在浏览器中访问: http://192.168.37.130:8080/
更多可以参考: Spring官方示例
完整源码: Gitee仓库