In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
这篇文章主要讲解了"如何使用Docker部署SpringBoot项目",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"如何使用Docker部署SpringBoot项目"吧!
首先构建一个简单的 spring boot 项目,然后给项目添加 docker 支持,最后对项目进行部署。
一个简单 spring boot 项目
在 pom.xml 中 ,使用 spring boot 2.0 相关依赖
org.springframework.boot spring-boot-starter-parent 2.0.0.release
添加 web 和测试依赖
org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test
创建一个 dockercontroller,在其中有一个index()方法,访问时返回:hello docker!
@restcontrollerpublic class dockercontroller { @requestmapping("/") public string index() { return "hello docker!"; }}
启动类
@springbootapplicationpublic class dockerapplication { public static void main(string[] args) { springapplication.run(dockerapplication.class, args); }}
添加完毕后启动项目,启动成功后浏览器访问:http://localhost:8080/,页面返回:hello docker!,说明 spring boot 项目配置正常。
spring boot 项目添加 docker 支持
在 pom.xml-properties中添加 docker 镜像名称
springboot
plugins 中添加 docker 构建插件:
org.springframework.boot spring-boot-maven-plugin com.spotify docker-maven-plugin 1.0.0 ${docker.image.prefix}/${project.artifactid} src/main/docker / ${project.build.directory} ${project.build.finalname}.jar
在目录src/main/docker下创建 dockerfile 文件,dockerfile 文件用来说明如何来构建镜像。
from openjdk:8-jdk-alpinevolume /tmpadd spring-boot-docker-1.0.jar app.jarentrypoint ["java","-djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
这个 dockerfile 文件很简单,构建 jdk 基础环境,添加 spring boot jar 到镜像中,简单解释一下:
from ,表示使用 jdk8 环境 为基础镜像,如果镜像不是本地的会从 dockerhub 进行下载
volume ,volume 指向了一个/tmp的目录,由于 spring boot 使用内置的tomcat容器,tomcat 默认使用/tmp作为工作目录。这个命令的效果是:在宿主机的/var/lib/docker目录下创建一个临时文件并把它链接到容器中的/tmp目录
add ,拷贝文件并且重命名
entrypoint ,为了缩短 tomcat 的启动时间,添加java.security.egd的系统属性指向/dev/urandom作为 entrypoint
这样 spring boot 项目添加 docker 依赖就完成了。
构建打包环境
我们需要有一个 docker 环境来打包 spring boot 项目,在 windows 搭建 docker 环境很麻烦,因此我这里以 centos 7 为例。
安装 docker 环境
安装
yum install docker
安装完成后,使用下面的命令来启动 docker 服务,并将其设置为开机启动:
service docker startchkconfig docker on #lctt 译注:此处采用了旧式的 sysv 语法,如采用centos 7中支持的新式 systemd 语法,如下:systemctl start docker.servicesystemctl enable docker.service
使用docker 中国加速器
vi /etc/docker/daemon.json #添加后:{ "registry-mirrors": ["https://registry.docker-cn.com"], "live-restore": true}
重新启动docker
systemctl restart docker
输入docker version 返回版本信息则安装正常。
安装jdk
yum -y install java-1.8.0-openjdk*
配置环境变量 打开 vim /etc/profile添加一下内容
export java_home=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.161-0.b14.el7_4.x86_64export path=$path:$java_home/bin
修改完成之后,使其生效
source /etc/profile
输入java -version 返回版本信息则安装正常。
安装maven
下载:
## 解压tar vxf apache-maven-3.5.2-bin.tar.gz## 移动mv apache-maven-3.5.2 /usr/local/maven3
修改环境变量, 在/etc/profile中添加以下几行
maven_home=/usr/local/maven3export maven_homeexport path=${path}:${maven_home}/bin
记得执行source /etc/profile使环境变量生效。
输入mvn -version 返回版本信息则安装正常。
这样整个构建环境就配置完成了。
使用 docker 部署 spring boot 项目
将项目 spring-boot-docker 拷贝服务器中,进入项目路径下进行打包测试。
#打包mvn package#启动java -jar target/spring-boot-docker-1.0.jar
看到 spring boot 的启动日志后表明环境配置没有问题,接下来我们使用 dockerfile 构建镜像。
mvn package docker:build
第一次构建可能有点慢,当看到以下内容的时候表明构建成功:
...step 1 : from openjdk:8-jdk-alpine ---> 224765a6bdbestep 2 : volume /tmp ---> using cache ---> b4e86cc8654estep 3 : add spring-boot-docker-1.0.jar app.jar ---> a20fe75963abremoving intermediate container 593ee5e1ea51step 4 : entrypoint java -djava.security.egd=file:/dev/./urandom -jar /app.jar ---> running in 85d558a10cd4 ---> 7102f08b5e95removing intermediate container 85d558a10cd4successfully built 7102f08b5e95[info] built springboot/spring-boot-docker[info] ------------------------------------------------------------------------[info] build success[info] ------------------------------------------------------------------------[info] total time: 54.346 s[info] finished at: 2018-03-13t16:20:15+08:00[info] final memory: 42m/182m[info] ------------------------------------------------------------------------
使用docker images命令查看构建好的镜像:
docker imagesrepository tag image id created sizespringboot/spring-boot-docker latest 99ce9468da74 6 seconds ago 117.5 mb
springboot/spring-boot-docker 就是我们构建好的镜像,下一步就是运行该镜像
docker run -p 8080:8080 -t springboot/spring-boot-docker
启动完成之后我们使用docker ps查看正在运行的镜像:
docker pscontainer id image command created status ports names049570da86a9 springboot/spring-boot-docker "java -djava.security" 30 seconds ago up 27 seconds 0.0.0.0:8080->8080/tcp determined_mahavira
可以看到我们构建的容器正在在运行,访问浏览器:http://192.168.0.x:8080/,返回
hello docker!
说明使用 docker 部署 spring boot 项目成功!
感谢各位的阅读,以上就是"如何使用Docker部署SpringBoot项目"的内容了,经过本文的学习后,相信大家对如何使用Docker部署SpringBoot项目这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.