博客
关于我
springboot与docker集成三步曲之项目打包部署
阅读量:156 次
发布时间:2019-02-26

本文共 5844 字,大约阅读时间需要 19 分钟。

前面我们构建了jdk运行环境的镜像,这里我们只需要将springboot项目打包到该镜像中,形成一个新的镜像springboot-docker,到时候,利用这个镜像启动容器。再做端口映射,就可以访问我们部署在docker中的springboot项目了。

1、构建springboot项目,编写pom.xml文件。

4.0.0
com.xxx
springboot-docker
0.0.1-SNAPSHOT
jar
springboot-docker
http://maven.apache.org
UTF-8
org.springframework.boot
spring-boot-starter-parent
2.0.4.RELEASE
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-maven-plugin
com.spotify
docker-maven-plugin
1.1.1
local/${project.artifactId}
src/main/docker
/
${project.build.directory}
${project.build.finalName}.jar

项目结构:

 

2、编写App.java启动类,该启动类还兼顾controller的角色。

package com.xxx.springbootwithdocker;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;@SpringBootApplication@RestControllerpublic class App{    public static void main( String[] args ){    	SpringApplication.run(App.class, args);    }        @GetMapping("/")    public String index(){    	return "{\"id\":\"1001\",\"content\":\"this is a docker\"}";    }    }

 3、准备打包的Dockerfile文件。src/main目录下新建docker文件夹,并新建Dockerfile文件,文件内容如下:

FROM local/centos7-jdk8VOLUME /tmpADD springboot-docker-0.0.1-SNAPSHOT.jar app.jarRUN sh -c 'touch /app.jar'ENV JAVA_OPTS=""ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ]

4、编译打包镜像。gitshell命令行切换到springboot-docker项目目录下,运行mvn clean package docker:build

$ mvn clean package docker:build[INFO] Scanning for projects...[INFO][INFO] ------------------------------------------------------------------------[INFO] Building springboot-docker 0.0.1-SNAPSHOT[INFO] ------------------------------------------------------------------------[INFO][INFO] --- maven-clean-plugin:3.0.0:clean (default-clean) @ springboot-docker ---[INFO] Deleting d:\git\GameWorld\Service\springboot-docker\target[INFO][INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ springboot-docker ---[INFO] Using 'UTF-8' encoding to copy filtered resources.[INFO] skip non existing resourceDirectory d:\git\GameWorld\Service\springboot-docker\src\main\resources[INFO] skip non existing resourceDirectory d:\git\GameWorld\Service\springboot-docker\src\main\resources[INFO][INFO] --- maven-compiler-plugin:3.7.0:compile (default-compile) @ springboot-docker ---[INFO] Changes detected - recompiling the module![INFO] Compiling 1 source file to d:\git\GameWorld\Service\springboot-docker\target\classes[INFO][INFO] --- maven-resources-plugin:3.0.2:testResources (default-testResources) @ springboot-docker ---[INFO] Using 'UTF-8' encoding to copy filtered resources.[INFO] skip non existing resourceDirectory d:\git\GameWorld\Service\springboot-docker\src\test\resources[INFO][INFO] --- maven-compiler-plugin:3.7.0:testCompile (default-testCompile) @ springboot-docker ---[INFO] No sources to compile[INFO][INFO] --- maven-surefire-plugin:2.21.0:test (default-test) @ springboot-docker ---[INFO] No tests to run.[INFO][INFO] --- maven-jar-plugin:3.0.2:jar (default-jar) @ springboot-docker ---[INFO] Building jar: d:\git\GameWorld\Service\springboot-docker\target\springboot-docker-0.0.1-SNAPSHOT.jar[INFO][INFO] --- spring-boot-maven-plugin:2.0.4.RELEASE:repackage (default) @ springboot-docker ---[INFO][INFO] --- docker-maven-plugin:1.1.1:build (default-cli) @ springboot-docker ---[INFO] Using authentication suppliers: [ConfigFileRegistryAuthSupplier][INFO] Copying d:\git\GameWorld\Service\springboot-docker\target\springboot-docker-0.0.1-SNAPSHOT.jar -> d:\git\GameWorld\Service\springboot-docker\target\docker\springboot-docker-0.0.1-SNAPSHOT.jar[INFO] Copying src\main\docker\Dockerfile -> d:\git\GameWorld\Service\springboot-docker\target\docker\Dockerfile[INFO] Building image local/springboot-dockerStep 0 : FROM local/centos7-jdk8 ---> 526ac3c7d9bcStep 1 : VOLUME /tmp ---> Running in 3e280a0ada9a ---> a5413101d40cRemoving intermediate container 3e280a0ada9aStep 2 : ADD springboot-docker-0.0.1-SNAPSHOT.jar app.jar ---> fd445c67de28Removing intermediate container 6c88bd797017Step 3 : RUN sh -c 'touch /app.jar' ---> Running in 3848c1b364fe ---> 4a5ce0df766fRemoving intermediate container 3848c1b364feStep 4 : ENV JAVA_OPTS "" ---> Running in 7593b0bf0660 ---> e91748a27f6cRemoving intermediate container 7593b0bf0660Step 5 : ENTRYPOINT sh -c java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar ---> Running in 7f3b41d42b33 ---> f6ee77cee236Removing intermediate container 7f3b41d42b33Successfully built f6ee77cee236[INFO] Built local/springboot-docker[INFO] ------------------------------------------------------------------------[INFO] BUILD SUCCESS[INFO] ------------------------------------------------------------------------[INFO] Total time: 30.636 s[INFO] Finished at: 2018-09-26T10:58:38+08:00[INFO] Final Memory: 51M/458M[INFO] ------------------------------------------------------------------------

构建成功,我们可以查看镜像,启动容器。

启动成功,需要做一个端口映射,我们就可以通过windows本机上的浏览器访问springboot项目。

访问 http://localhost:18080/

至此,springboot与docker集成,已经全部完成,整个过程基本都是在配置docker镜像,构建docker镜像。 

你可能感兴趣的文章
mysql 权限整理记录
查看>>
mysql 权限登录问题:ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)
查看>>
MYSQL 查看最大连接数和修改最大连接数
查看>>
MySQL 查看有哪些表
查看>>
mysql 查看锁_阿里/美团/字节面试官必问的Mysql锁机制,你真的明白吗
查看>>
MySql 查询以逗号分隔的字符串的方法(正则)
查看>>
MySQL 查询优化:提速查询效率的13大秘籍(避免使用SELECT 、分页查询的优化、合理使用连接、子查询的优化)(上)
查看>>
mysql 查询数据库所有表的字段信息
查看>>
【Java基础】什么是面向对象?
查看>>
mysql 查询,正数降序排序,负数升序排序
查看>>
MySQL 树形结构 根据指定节点 获取其下属的所有子节点(包含路径上的枝干节点和叶子节点)...
查看>>
mysql 死锁 Deadlock found when trying to get lock; try restarting transaction
查看>>
mysql 死锁(先delete 后insert)日志分析
查看>>
MySQL 死锁了,怎么办?
查看>>
MySQL 深度分页性能急剧下降,该如何优化?
查看>>
MySQL 深度分页性能急剧下降,该如何优化?
查看>>
MySQL 添加列,修改列,删除列
查看>>
mysql 添加索引
查看>>
MySQL 添加索引,删除索引及其用法
查看>>
mysql 状态检查,备份,修复
查看>>