目录
- 手动部署
- 1、idea创建spring boot项目
- 2、项目打成 jar 包
- 3、构建 docker image
- 4、查看并运行镜像
- 插件部署
- 运行推送命令
将spring boot项目部署到docker中有两种方法,手动部署和插件部署
手动部署
1、idea创建spring boot项目
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">
<parent>
<artifactid>spring-cloud-examples</artifactid>
<groupid>org.example</groupid>
<version>1.0-snapshot</version>
</parent>
<modelversion>4.0.0</modelversion>
<artifactid>dockerdemo</artifactid>
<dependencies>
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-web</artifactid>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-maven-plugin</artifactid>
</plugin>
</plugins>
</build>
</project>
必须添加 spring-boot-maven-plugin 插件,该插件的作用是在打 jar 包时引入依赖包,当运行“mvn package”进行打包时,会打包成一个可以直接运行的 jar 文件,使用 “java -jar” 命令就可以直接运行。
启动类
package dockerdemo;
import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.springbootapplication;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.restcontroller;
@restcontroller
@springbootapplication
public class application {
public static void main(string[] args) {
springapplication.run(application.class, args);
}
@requestmapping("/hello")
public string hello(){
return "hello docker world!";
}
}
2、项目打成 jar 包
然后在项目pom.xml文件所在目录执行maven命令将项目打成 jar 包
$ mvn package
从输出日志可知 jar 在 target 目录下,直接运行 jar 包
$ java -jar dockerdemo-1.0-snapshot.jar
然后在浏览器中输入 http://localhost:8080/hello 进行测试
3、构建 docker image
创建dockerfile文件
from java:8 volume /tmp add dockerdemo-1.0-snapshot.jar dockerdemo.jar run bash -c "touch /dockerdemo.jar" entrypoint ["java","-djava.security.egd=file:/dev/./urandom","-jar","/dockerdemo.jar"]
参数解释:
- from:表示以jdk8为基础镜像制作docker镜像
- volume:表示创建一个挂载点,容器目录为 /tmp,主机目录自动生成。创建 /tmp 是因为spring boot内嵌的tomcat容器默认使用 /tmp 作为工作目录
- add:将容器外的 dockerdemo-1.0-snapshot.jar 拷贝到容器中,并重命名为 dockerdemo.jar
- run:run 后面跟着bash命令,-c 表示将后面的字符串当命令执行,即执行 touch /dockerdemo.jar,该命令修改 dockerdemo.jar 文件的访问时间和修改时间为当前时间
- entrypoint:容器启动时运行的命令,相当于我们在命令行中输入java -jar xxxx.jar,为了缩短tomcat的启动时间,添加 java.security.egd 的系统属性指向 /dev/urandom 作为 entrypoint
创建好 dockerfile 后,把打包好的 spring boot 项目 jar 包和 dockerfile 文件放在任意一个目录下,使用 docker 命令构建镜像文件:
$ docker image build -t dockerdemo:1 .
参数解释:
- build:表示制作镜像
- -t:表示给镜像打个标签,相当于 docker tag 镜像id 新镜像名:版本号
- .:表示dockerfile文件所在位置,. 表示在当前目录
4、查看并运行镜像
#查看镜像: $ docker images #运行镜像: $ docker container run --name dockerdemo -d -p 80:8080 dockerdemo:1
参数解释:
- docker container run:表示运行容器
- –name:给容器起个别名,操作容器的时候可以使用别名来代替容器id,方便容器的管理
- -d:表示容器开启后在后台运行
- -p:端口映射。将容器内部的8080端口映射到宿主机的80端口
插件部署
插件部署要在项目的 pom.xml 文件中添加 dockerfile-maven-plugin 插件
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">
<parent>
<artifactid>spring-cloud-docker</artifactid>
<groupid>org.example</groupid>
<version>1.0-snapshot</version>
</parent>
<modelversion>4.0.0</modelversion>
<artifactid>spring-cloud-eureka</artifactid>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceencoding>utf-8</project.build.sourceencoding>
<project.reporting.outputencoding>utf-8</project.reporting.outputencoding>
<!-- 镜像前缀,推送镜像到远程库时需要,这里配置了一个阿里云的私有库 -->
<docker.image.prefix>
registry.cn-huhehaote.aliyuncs.com/monkeybrain
</docker.image.prefix>
<!-- docker镜像的tag -->
<docker.tag>latest</docker.tag>
<!-- 激活的profile -->
<!--<activatedproperties></activatedproperties>-->
</properties>
<dependencies>
<dependency>
<groupid>org.springframework.cloud</groupid>
<artifactid>spring-cloud-starter-eureka-server</artifactid>
</dependency>
</dependencies>
<profiles>
<!-- docker环境 -->
<!--<profile>
<id>docker</id>
<properties>
<activatedproperties>docker</activatedproperties>
<docker.tag>docker-demo-${project.version}</docker.tag>
</properties>
</profile>-->
</profiles>
<build>
<!--默认maven命令-->
<defaultgoal>install</defaultgoal>
<finalname>${project.artifactid}</finalname>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<!-- 配置spring boot maven插件,把项目打包成可运行的jar包 -->
<plugin>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-maven-plugin</artifactid>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
<!-- 打包时跳过单元测试 -->
<plugin>
<groupid>org.apache.maven.plugins</groupid>
<artifactid>maven-surefire-plugin</artifactid>
<configuration>
<skiptests>true</skiptests>
</configuration>
</plugin>
<!-- 配置docker maven插件,绑定install生命周期,在运行maven install时生成docker镜像 -->
<plugin>
<groupid>com.spotify</groupid>
<artifactid>docker-maven-plugin</artifactid>
<version>0.4.13</version>
<!--<executions>
<execution>
<phase>install</phase>
<goals>
<goal>build</goal>
<goal>tag</goal>
</goals>
</execution>
</executions>-->
<configuration>
<!-- 修改这里的docker节点ip,需要打开 docker节点的远程管理端口2375,
具体如何配置可以参照之前的 docker安装和配置的文章 -->
<dockerhost>http://localhost:2375</dockerhost>
<imagename>${docker.image.prefix}/${project.build.finalname}</imagename>
<serverid>aliyun-docker-registry</serverid>
<registryurl>registry.cn-huhehaote.aliyuncs.com</registryurl>
<pushimage>true</pushimage>
<!--镜像的标签-->
<imagetags>
<imagetag>latest</imagetag>
</imagetags>
<!--基础镜像-->
<baseimage>java:8</baseimage>
<!-- 这里的 entrypoint 定义了容器启动时的运行命令,容器启动时运行 java -jar 包名 -->
<entrypoint>
["java","-jar","/${project.build.finalname}.jar"]
</entrypoint>
<resources>
<resource>
<targetpath>/</targetpath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalname}.jar</include>
</resource>
</resources>
<!--<image>${docker.image.prefix}/${project.build.finalname}</image>
<newname>${docker.image.prefix}/${project.build.finalname}:${docker.tag}</newname>
<forcetags>true</forcetags>-->
<!-- 如果需要在生成镜像时推送到远程库,pushimage设为true -->
<!--<pushimage>false</pushimage>-->
</configuration>
</plugin>
</plugins>
</build>
</project>
运行推送命令
$ mvn clean package docker:build -dpushimage
到此这篇关于spring boot应用通过docker发布部署的流程分析的文章就介绍到这了,更多相关spring boot应用docker部署内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!