Docker部署Spring-boot项目的示例代码

2020-06-17 06:52:40易采站长站整理

检查API是否生效


$ curl -XGET 'http://localhost:9090/api/docker/hello'
hello docker

浏览器检查

http://localhost:9090/api/docker/hello

1.2 打包启动

项目打包

完成上面步骤之后,执行打包命令:


$ mvn clean -U -Dmaven.test.skip compile package

因为上面的pom文件里面定义了

finalName
,所以在这里会看到编译打包之后
target
目录下会生成
spring-docker.jar


<finalName>spring-docker</finalName>

测试运行


$ java -jar target/spring-docker.jar

不出意外(有问题留言~)运行结果同上并检查API是否生效即可.

二、Docker快速安装

接下来开始准备Docker

安装

官网下载安装

检查安装、查看帮助


$ docker --version
Docker version 18.06.0-ce, build 0ffa825

$ docker --help
Usage: docker [OPTIONS] COMMAND
A self-sufficient runtime for containers
...

镜像加速

中国官方镜像加速

三、配置Spring-boot + Docker

pom.xml 添加docker plugin


<properties>
<docker.image.prefix>springboot</docker.image.prefix>
</properties>

<build>
<plugins>
<!-- Docker maven plugin -->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.0.0</version>
<configuration>
<imageName>${docker.image.prefix}/${project.build.finalName}</imageName>
<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>

创建

Dockerfile
文件

根据上面

pom.xml
文件配置
<dockerDirectory>src/main/docker</dockerDirectory>