Docker部署Spring-boot项目的示例代码
一、基础spring-boot快速启动
1.1 快速启动 pom.xml加入如下依赖
<parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version>2.0.5.release</version> </parent> <properties> <java.version>1.8</java.version> <project.build.sourceencoding>utf-8</project.build.sourceencoding> </properties> <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> <build> <finalname>spring-docker</finalname> <plugins> <plugin> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> </plugin> </plugins> </build>
spring-boot启动类
@springbootapplication public class dockerapplication { public static void main(string[] args) { springapplication.run(dockerapplication.class, args); } }
测试api
@restcontroller public class dockerstarterapi { @getmapping("/api/docker/hello") public string hello() { return "hello docker"; } }
配置启动配置文件 application.yml
server: port: 9090 # 为了展示效果, 这里改了默认端口8080
检查spring启动
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: spring boot :: (v2.0.2.release) ... 2018-12-17 17:26:13.385 info 48740 --- [ main] o.s.j.e.a.annotationmbeanexporter : registering beans for jmx exposure on startup 2018-12-17 17:26:13.448 info 48740 --- [ main] o.s.b.w.embedded.tomcat.tomcatwebserver : tomcat started on port(s): 9090 (http) with context path '' 2018-12-17 17:26:13.453 info 48740 --- [ main] pers.study.docker.dockerapplication : started dockerapplication in 1.982 seconds (jvm running for 2.602)
检查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>
,这里配置了docker配置文件的目录,所以需要再 src/main
下面创建docker文件夹,同时创建 dockerfile
文件。
目录机构如图:
docker配置文件结构.png
编辑 dockerfile
from openjdk:8-jdk-alpine volume /tmp add spring-docker.jar app.jar entrypoint ["java","-djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
from 表示以java8为基础镜像
volume 表示挂载目录
add 拷贝打包文件并重命名为 app.jar
entrypoint 根据下面的官方文档解释大致是为了缩短tomcat启动时间而添加的一个系统属性。
we added a
volume
pointing to/tmp
because that is where a spring boot application creates working directories for tomcat by default. the effect is to create a temporary file on your host under/var/lib/docker
and link it to the container under/tmp
. this step is optional for the simple app that we wrote here but can be necessary for other spring boot applications if they need to actually write in the filesystem.to reduce tomcat startup time we added a system property pointing to "/dev/urandom" as a source of entropy. this is not necessary with more recent versions of spring boot, if you use the "standard" version of tomcat (or any other web server).
配置完成!
四、docker启动spring-boot
进入 module
执行:
$ mvn package docker:build [info] scanning for projects... ... ---> running in e1f8aba72bdf removing intermediate container e1f8aba72bdf ---> 36a61c09f09a progressmessage{id=null, status=null, stream=null, error=null, progress=null, progressdetail=null} successfully built 36a61c09f09a successfully tagged springboot/spring-docker:latest [info] built springboot/spring-docker [info] ------------------------------------------------------------------------ [info] build success [info] ------------------------------------------------------------------------ [info] total time: 6.367 s [info] finished at: 2018-12-17t20:48:21+08:00 [info] ------------------------------------------------------------------------
查看镜像
$ docker images repository tag image id created size springboot/spring-docker latest 36a61c09f09a 2 minutes ago 123mb
运行镜像
$ docker run -p 9090:9090 -t springboot/spring-docker . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: spring boot :: (v2.0.2.release) 2018-12-17 12:53:21.502 info 1 --- [ main] pers.study.docker.dockerapplication : starting dockerapplication v1.0-snapshot on 94991c04be5d with pid 1 (/app.jar started by root in /) 2018-12-17 12:53:21.509 info 1 --- [ main] pers.study.docker.dockerapplication : no active profile set, falling back to default profiles: default ··· 2018-12-17 12:53:25.255 info 1 --- [ main] o.s.j.e.a.annotationmbeanexporter : registering beans for jmx exposure on startup 2018-12-17 12:53:25.337 info 1 --- [ main] o.s.b.w.embedded.tomcat.tomcatwebserver : tomcat started on port(s): 9090 (http) with context path '' 2018-12-17 12:53:25.353 info 1 --- [ main] pers.study.docker.dockerapplication : started dockerapplication in 4.485 seconds (jvm running for 5.346)
查看容器
$ docker ps container id image command created status ports names 94991c04be5d springboot/spring-docker "java -djava.securit…" 53 seconds ago up 52 seconds 0.0.0.0:9090->9090/tcp quizzical_bhabha
验证启动,访问api
$ curl -xget 'http://localhost:9090/api/docker/hello' hello docker
至此docker部署spring-boot搭建完成。
五、移除镜像
停止容器
$ docker stop 94991c04be5d 94991c04be5d
删除容器
$ docker rm 94991c04be5d 94991c04be5d
删除镜像
$ docker image rm springboot/spring-docker untagged: springboot/spring-docker:latest deleted: sha256:36a61c09f09ab88cfe5a05f564deb57498682f4a6f3ec01d2a8c4fdc80ac1e41 deleted: sha256:3f9aef70be6d4d43c205454d8874f10bc2f7280f70eb88cd1f04937b7965dd27 deleted: sha256:9a5800e93615bb4c5128bb36d31ec494327c01f1a9a768c1ff538badf76628b9 deleted: sha256:d9c66f907448fa9e61fd5f9267d7fcf8e1f4b52d0a20466414f2f45777261284
六、其他配置功能
添加环境属性
$ docker run -e "spring_profiles_active=prod" -p 9090:9090 -t springbooot/spring-docker
后台启动运行
$ docker run -p 9090:9090 -d springboot/spring-docker
开启容器debug 修改 dockerfile
from openjdk:8-jdk-alpine volume /tmp add spring-docker.jar app.jar env java_opts '' cmd java -djava.security.egd=file:/dev/./urandom $java_opts -jar app.jar
docker run
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。