你想要的Docker自动部署Spring Boot就在这

你想要的Docker自动部署Spring Boot就在这

简介

Spring Boot的快速开发特性,正受越来越多Java开发者的欢迎,配合supervisord可以轻松使其作为一个独立的服务运行。而随着Docker的流行,其轻量级、进程级、资源隔离等特性,使Spring Boot的部署、运行更加灵活,若将其打包成Docker镜像可以真正做到“一次打包,到处运行”,有效的解决了开发、测试、预生产、生产等环境的差异。

下面我们就从Docker手动、自动部署Spring Boot项目来讲解下,Docker是如何轻松部署的。因此你想要的Docker自动部署Spring Boot就在这了。

我们正式开始讲解吧。

手动构建spring boot应用

此部分通过直接手动打包、Docker手动部署Spring Boot,运行helloworld项目。

helloworld应用

  1. 创建spring boot工程

使用IntelliJ IDEA的“Spring Assistant”插件创建spring web项目

Group id: com.docker

Artifact id: hellworld

Project name: helloworld

Package name: com.docker.helloworld

  1. 创建HelloworldController
<code>vim HelloworldController
package com.docker.helloworld;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloworldController {
    
    private static final Logger logger= LoggerFactory.getLogger(HelloworldController.class);
    @RequestMapping("/")
    public String index(){
        return "Hello world";
    }/<code>
  1. 编译运行
<code>mvn clean package && java -jar target/helloworld-0.0.1-SNAPSHOT.jar/<code>

maven手动打包后并运行jar包,通过localhost:8080可以直接hellworld项目了,下面手动将jar包放到Docker中运行。

docker构建镜像

  1. Dockerfile
<code>#基础镜像java 1.8
From java
#匿名数据卷,在启动容器时忘记挂载数据卷,会自动挂载到匿名卷
VOLUMN /tmp
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} helloworld.jar
ENTRYPOINT ["java","-jar","/helloworld.jar"]/<code>
  1. 构建镜像并运行容器
<code>#从dockerfile构建镜像
[root@test]# docker build -t docker/helloworld .

#docker/helloworld就是我们构建的新镜像
[root@test]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker/helloworld   latest              75c3d26e3c57        8 minutes ago       661MB

#运行容器
[root@test]# docker run -d -p 8080:8080 --name hellworld docker/helloworld
#查看镜像
[root@test]# docker ps -l
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                       PORTS                                        NAMES
feb8b6ef9941        docker/helloworld   "java -jar /hellowor…"   8 minutes ago       Up 8 minutes                 0.0.0.0:8080->8080/tcp                       hellworld/<code>

利用Dockerfile构建一个运行helloworld的镜像,基与这个镜像启动容器后,我们可以通过localhost:8080访问了helloworld了。

以上两步其实就是Docker自动部署Spring Boot主要完成的两个步骤,整个过程不是手动完成,而下面要讲的是通过maven提供的插件自动完成build、tag、push镜像。

自动构建Spring Boot

配置maven插件

maven插件docker-maven-plugin可以打包应用并将构建docker镜像,推送到docker仓库。

<code>#添加docker-maven-plugin到pom.xml
#版本已经使用新版本

	com.spotify
	docker-maven-plugin
	1.2.2
	
		docker/helloworld
		src/main/docker
		
			
				/
				${project.build.directory}
				${project.build.finalName}.jar
			
		
	
/<code>

其中:

  1. 是构建镜像的名称;
  2. 是Dockerfile位置,最好是只有Dockerfile,因为在mvn打包时此目录下的所有文件包括Dockerfile将会被copy到${project.build.directory}/docker目录下,此目录一般是target/docker目录;
  3. 作用是将${project.build.directory}目录下的${project.build.finalName}.jar,复制到${project.build.directory}/docker目录下;如:将target/helloworld-0.0.1-SNAPSHOT.jar 文件复制到target/docker/下;

打包构建

  1. maven可以直接将应用打包成docker镜像
<code>[root@test]# mvn clean package docker:build

[INFO] --- docker-maven-plugin:1.2.2:build (default-cli) @ helloworld ---
[INFO] Using authentication suppliers: [ConfigFileRegistryAuthSupplier]
[INFO] Copying /home/yanggd/java/docker/helloworld/target/helloworld-0.0.1-SNAPSHOT.jar -> /home/yanggd/java/docker/helloworld/target/docker/helloworld-0.0.1-SNAPSHOT.jar
[INFO] Copying src/main/docker/Dockerfile -> /home/yanggd/java/docker/helloworld/target/docker/Dockerfile
[INFO] Building image docker/helloworld
Step 1/5 : From java

Pulling from library/java
5040bd298390: Pull complete 
fce5728aad85: Pull complete 
76610ec20bf5: Pull complete 
60170fec2151: Pull complete 
e98f73de8f0d: Pull complete 
11f7af24ed9c: Pull complete 
49e2d6393f32: Pull complete 
bb9cdec9c7f3: Pull complete 
Digest: sha256:c1ff613e8ba25833d2e1940da0940c3824f03f802c449f3d1815a66b7f8c0e9d
Status: Downloaded newer image for java:latest
 ---> d23bdf5b1b1b
Step 2/5 : VOLUME /tmp

 ---> Running in fa47a820cd54
Removing intermediate container fa47a820cd54
 ---> 434e44760301
Step 3/5 : ARG JAR_FILE=*.jar

 ---> Running in 3e0266b2cb16
Removing intermediate container 3e0266b2cb16
 ---> aa3be3ab4daf
Step 4/5 : COPY ${JAR_FILE} helloworld.jar

 ---> 73cfed161b79
Step 5/5 : ENTRYPOINT ["java","-jar","/helloworld.jar"]

 ---> Running in 1e6d6ec86542
Removing intermediate container 1e6d6ec86542
 ---> 9027adc2d0a4
ProgressMessage{id=null, status=null, stream=null, error=null, progress=null, progressDetail=null}
Successfully built 9027adc2d0a4
Successfully tagged docker/helloworld:latest
/<code>

其中:第Step 4/5、Step 5/5可以看到copy过程,剩下为从Dockerfile构建镜像过程。

  1. 查看并运行打包后的镜像
<code>#查看镜像
[root@test]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker/helloworld   latest              9027adc2d0a4        About an hour ago   661MB
#运行
docker run -d -p 8080:8080  --name helloworld docker/helloworld/<code>

从以上看出此镜像是没有tag,通过以下方式添加tag:

<code>#在pom.xml的configuration段中添加如下:


	test


或者
#在命令行直接打tag
mvn clean package docker:build -DdockerImageTags=v1

#构建完成后,同一image_id 有不同的tag
[root@test]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker/helloworld   v1                  6f3b482d2b78        10 seconds ago      661MB
docker/helloworld   latest              6f3b482d2b78        10 seconds ago      661MB
docker/helloworld   test                6f3b482d2b78        10 seconds ago      661MB/<code>

如上,我们通过两种方式给镜像添加了test和v1两个tag。了解如何添加tag后,就会明白后面的push镜像的配置。

以上是通过maven自动构建镜像并添加tag,但是还没有完成自动推送远程仓库,咱们将继续讲解。

推送镜像到远程仓库阿里云镜像库

  1. 创建仓库

在阿里云容器镜像服务创建仓库

<code>registry.cn-qingdao.aliyuncs.com/test/test/<code>
  1. maven配置镜像库认证

在maven配置文件中指定远程的镜像仓库

<code>vim settings.xml
#servers块中插入

  docker-aliyun
  #阿里云开通的账户名
  xxxx
  #开通镜像服务的密码,不是阿里云登录密码
  xxxxx
  
    #阿里云绑定邮箱
    xxxxx
  
/<code>
  1. pom.xml配置

配置pom.xml是因为这些都是maven自动构建、推送镜像的基础参数。

<code>
	
		
			org.springframework.boot
			spring-boot-maven-plugin
		
		
			com.spotify
			docker-maven-plugin
			1.2.2
			
			     
			     
				registry.cn-qingdao.aliyuncs.com/test/test
				src/main/docker
				
					
						/
						${project.build.directory}
						${project.build.finalName}.jar
					
				
				 
				docker-aliyun
				registry.cn-qingdao.aliyuncs.com/test/test
			
		
	
/<code>
  1. 构建并push
<code>#构建,只推送tag为v3至远程仓库
[root@test]# mvn clean package docker:build -DdockerImageTags=v3 -DpushImageTag
[root@test]# docker image ls
REPOSITORY                                              TAG                 IMAGE ID            CREATED              SIZE
registry.cn-qingdao.aliyuncs.com/test/test   latest              08d5a09985a1        20 seconds ago       661MB
registry.cn-qingdao.aliyuncs.com/test/test   v3                  08d5a09985a1        20 seconds ago       661MB/<code> 

此处需要注意:

<code>#若不加-DdockerImageTags=v3 ,会将所有构建的镜像(v3及latest)都上传。
mvn clean package docker:build -DpushImageTag
#若不加-DpushImageTag,只会构建镜像并tag 为v3,但不会push。
mvn clean package docker:build -DdockerImageTags=v3/<code>

最后查看下阿里云的镜像库:


你想要的Docker自动部署Spring Boot就在这

整个推送的过程关键点在远程仓库的认证及将镜像添加tag与远程镜像一致。

以上为maven 构建、tag、推送的整个步骤,前提是要保证pom.xml中的配置信息没有错误。其实我们还可以将Docker的构建过程绑定到maven的各个阶段,我们再来深入讲解下。

绑定Docker命令到Maven各个阶段

Docker 构建过程为 build、tag、push,其中build、tag对应mvn的package阶段,push对应mvn的deploy。通过将docker命令绑定到mvn的各个阶段,我们可以通过mvn deploy实现构建并push的过程。

补充:

  1. package阶段实现项目编译、单元测试、打包功能,但没有把打好的可执行jar包(war包或其它形式的包)部署到本地maven仓库和远程maven私服仓库;
  2. install阶段实现项目编译、单元测试、打包功能,同时把打好的可执行jar包(war包或其它形式的包)布署到本地maven仓库,但没有布署到远程maven私服仓库;
  3. deploy阶段实现项目编译、单元测试、打包功能,同时把打好的可执行jar包(war包或其它形式的包)部署到本地maven仓库和远程maven私服仓库;

1.pom.xml配置插件

<code> 

	org.apache.maven.plugins
	maven-deploy-plugin
	
		true
	


	com.spotify
	docker-maven-plugin
	1.2.2
	
		registry.cn-qingdao.aliyuncs.com/test/test
		src/main/docker
		
			
				/
				${project.build.directory}
				${project.build.finalName}.jar
			
		
		
			${project.version}
		
		docker-aliyun
		true
		registry.cn-qingdao.aliyuncs.com/test/test
	
	
		
			build-image
			package
			
				build
			
		
		
			tag-image
			package
			
				tag
			
			
				
				registry.cn-qingdao.aliyuncs.com/test/test:${project.version}
			
		
		
			push-image
			deploy
			
				push
			
			
				registry.cn-qingdao.aliyuncs.com/test/test:${project.version}
			
		
	
/<code>

注意:一定要通过maven-deploy-plugin设置skip,否则mvn deploy 推送时报错:

<code>Deployment failed: repository element was not specified in the POM inside distributionManagement element or in -DaltDeploymentRepository=id::layout::url parameter/<code>

2.执行命令

<code>#构建、tag 不推送
mvn clean package
#构建、tag及推送到远程仓库
mvn clean deploy 
#跳过整个过程,不执行实际命令,可用测试流程是否正常
mvn clean deploy -DskipDocker
#跳过构建
mvn clean -DskipDockerBuild
#跳过tag
mvn clean -DskipDockerTag
#跳过push
mvn clean -DskipDockerPush/<code>

注意:

  1. 经测试发现,实际tag阶段并不会进行tag,而是在registry.cn-qingdao.aliyuncs.com/test/test进行tag,如果你的imageName设置不正确,则不会推送到自定义仓库,而是默认的官方仓库
  2. 此方式会将镜像的所有tag 版本,如v3和latest都会推送到远程仓库;

通过对比,"docker命令绑定maven各阶段"方式不太灵活,因为这会将镜像所有tag进行推送;而使用不绑定到maven阶段的可以自由设置推送到远程仓库的tag,这点更灵活。我们需要根据实际情况自由选择。

总结

通过以上讲解,我们应该知道Docker部署Spring Boot的原理及细节,剩下只需要在客户端直接运行就可以,真正做到“一次打包,到处运行”。如果你完全掌握那么Docker算是入门了。但是这对于Docker的深入应用还远远不够,在实际过程中我们还需要考虑以下问题:

  1. Spring Boot应用的运行时数据、日志文件的持久化;
  2. 镜像命令、目录问题、私有镜像库等的配合使用;
  3. 流水线操作实现测试、版本发布等;
  4. Docker 健康检查等;

这些需要我们通过不断实践,完善开发、运维、测试等在应用中的需求才能做到的,因此让我们行动起来吧。


分享到:


相關文章: