基于springboot的linux-docker部署
概述
详细
一、运行效果
二、实现过程
①、linux上安装docker
进入Centos终端,执行命令:
yum install docker
静待一会,等待安装成功,之后再执行
systemctl start docker
然后查看安装的docker版本
[root@iZ2zejaebtdc3kosrltpqaZ ~]# docker versionClient: Version: 1.12.6 API version: 1.24 Package version: docker-1.12.6-68.gitec8512b.el7.centos.x86_64 Go version: go1.8.3 Git commit: ec8512b/1.12.6 Built: Mon Dec 11 16:08:42 2017 OS/Arch: linux/amd64 Server: Version: 1.12.6 API version: 1.24 Package version: docker-1.12.6-68.gitec8512b.el7.centos.x86_64 Go version: go1.8.3 Git commit: ec8512b/1.12.6 Built: Mon Dec 11 16:08:42 2017 OS/Arch: linux/amd64
②、准备srpingboot项目
简单的创建一个工程,实现一个接口即可,然后在pom中添加docker插件,相关代码如下
package com.zxh; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DockerApplication { public static void main(String[] args) { SpringApplication.run(DockerApplication.class); } }
package com.zxh.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloContrller { @RequestMapping("/hello") public String hello() { return "<h1>Hello Spring-Boot Maven Docker</h1>"; } }
添加maven打包docker插件
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>0.4.3</version> <configuration> <imageName>${docker.image.prefix}/${project.artifactId}</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,注意里面的ADD xxx.jar
是你使用maven打包之后的jar包的名称,其他的不变
FROM frolvlad/alpine-oraclejdk8:slim VOLUME /tmp ADD springboot-docker-quick-demo-0.0.1-SNAPSHOT.jar app.jar RUN sh -c 'touch /app.jar' ENV JAVA_OPTS="" ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
③、使用docker打包部署项目
将src文件和pom放在任意文件夹下,执行命令
mvn package docker:build
在打包的过程中比以往多了以下几步
[WARNING] [WARNING] Some problems were encountered while building the effective settings [WARNING] expected START_TAG or END_TAG not TEXT (position: TEXT seen ...l use to store artifacts.\n |\n | Default: ~/.m2/repository\n <l... @53:5) @ /opt/java/maven-3.0.5/conf/settings.xml, line 53, column 5 [WARNING] [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building springboot-docker-quick-demo 0.0.1-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ springboot-docker-quick-demo --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 1 resource [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ springboot-docker-quick-demo --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ springboot-docker-quick-demo --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory /opt/java/src/springboot-docker-quick-demo/src/test/resources [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ springboot-docker-quick-demo --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ springboot-docker-quick-demo --- [INFO] [INFO] --- maven-jar-plugin:2.5:jar (default-jar) @ springboot-docker-quick-demo --- [INFO] Building jar: /opt/java/src/springboot-docker-quick-demo/target/springboot-docker-quick-demo-0.0.1-SNAPSHOT.jar [INFO] [INFO] --- spring-boot-maven-plugin:1.3.3.RELEASE:repackage (default) @ springboot-docker-quick-demo --- [INFO] [INFO] --- docker-maven-plugin:0.4.3:build (default-cli) @ springboot-docker-quick-demo --- SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details. [INFO] Copying /opt/java/src/springboot-docker-quick-demo/target/springboot-docker-quick-demo-0.0.1-SNAPSHOT.jar -> /opt/java/src/springboot-docker-quick-demo/target/docker/springboot-docker-quick-demo-0.0.1-SNAPSHOT.jar [INFO] Copying src/main/docker/Dockerfile -> /opt/java/src/springboot-docker-quick-demo/target/docker/Dockerfile [INFO] Building image protect/springboot-docker-quick-demo Step 1/6 : FROM java:8 ---> d23bdf5b1b1b Step 2/6 : VOLUME /tmp ---> Using cache ---> ce25c4054b41 Step 3/6 : ADD springboot-docker-quick-demo-0.0.1-SNAPSHOT.jar app.jar ---> 35994ca2df16 Removing intermediate container 0e8803fbeab1 Step 4/6 : RUN sh -c 'touch /app.jar' ---> Running in 0d1059be208c ProgressMessage{id=null, status=null, stream=null, error=null, progress=null, progressDetail=null} ---> cbc9e74114a5 Removing intermediate container 0d1059be208c Step 5/6 : ENV JAVA_OPTS "" ---> Running in c03397a182b2 ---> c780ddd10559 Removing intermediate container c03397a182b2 Step 6/6 : ENTRYPOINT java -Djava.security.egd=file:/dev/./urandom -jar /app.jar ---> Running in 31d6e2eb9123 ---> 84b2591f18d4 Removing intermediate container 31d6e2eb9123 Successfully built 84b2591f18d4 [INFO] Built protect/springboot-docker-quick-demo [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 9.674s [INFO] Finished at: Mon Jul 08 16:10:31 CST 2019 [INFO] Final Memory: 34M/322M [INFO] ------------------------------------------------------------------------
Docker启动服务
执行docker images
简单的看到了docker中存在的镜像、标签、镜像ID、已经创建的时间和大小,看下protect/springboot-docker-quick-demo
这个是在pom中<imageName>${docker.image.prefix}/${project.artifactId}</imageName>
配置的,比较重要,因为它和接下来要讲的将镜像提交到DockerHub有着密切的联系。
执行docker run -d -p 8080:8080 protect/springboot-docker-quick-demo
来启动服务。
简单解释下这个命令
-d 代表后台运行
-p 标识宿主机与docker服务的端口映射,注意谁前谁后:【宿主端口:docker内服务端口】
protect/springboot-docker-quick-demo 就是启动镜像的名称,当然了使用IMAGE ID 也是可以的
好了,看下docker是否将服务启动成功,执行docker ps
很顺利,运行成功了
CONTAINER ID
容器IDPORTS
宿主与docker内部的服务映射NAMES
容器名称,跟容器ID对应,如果你不指定名称的话,docker会随机给你分配一个name, 名字还是很有意思的
接下来,就是验证的时候了,在浏览器输入对应URL
三、项目结构图
四、补充-Docker的优点
1、简化程序:
Docker 让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,便可以实现虚拟化。Docker改变了虚拟化的方式,使开发者可以直接将自己的成果放入Docker中进行管理。方便快捷已经是 Docker的最大优势,过去需要用数天乃至数周的 任务,在Docker容器的处理下,只需要数秒就能完成。2、避免选择恐惧症:
如果你有选择恐惧症,还是资深患者。Docker 帮你 打包你的纠结!比如 Docker 镜像;Docker 镜像中包含了运行环境和配置,所以 Docker 可以简化部署多种应用实例工作。比如 Web 应用、后台应用、数据库应用、大数据应用比如 Hadoop 集群、消息队列等等都可以打包成一个镜像部署。3、节省开支:
一方面,云计算时代到来,使开发者不必为了追求效果而配置高额的硬件,Docker 改变了高性能必然高价格的思维定势。Docker 与云的结合,让云空间得到更充分的利用。不仅解决了硬件管理的问题,也改变了虚拟化的方式。