Mobile wallpaper 1Mobile wallpaper 2
309 字
2 分钟
使用Jib部署SpringBoot应用

Jib是什么?#

使用Jib,您可以在本机没有安装Docker的情况下为您的Java应用构建优化的Docker和OCI镜像,并且可以将构建好的镜像推送到远程镜像仓库。它提供Maven插件和Gradle插件以及Java库形式。

项目环境#

  • Spring Boot 2.3.0.RELEASE
  • Java 8

pom.xml#

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>example</groupId>
<artifactId>spring-boot-jib-example</artifactId>
<version>0.1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<properties>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<version>1.8.0</version>
<configuration>
<from>
<image>openjdk:8-jdk</image>
<!-- <auth>
<username>${env.REGISTRY_USERNAME}</username>
<password>${env.REGISTRY_PASSWORD}</password>
</auth>-->
</from>
<to>
<!-- 阿里云ACR 镜像仓库 -->
<image>registry.cn-shenzhen.aliyuncs.com/xxxxxx/spring-boot-jib-demo</image>
<tags>
<!--maven 内置变量-->
<tag>${project.version}</tag>
</tags>
<auth>
<username>${env.REGISTRY_USERNAME}</username>
<password>${env.REGISTRY_PASSWORD}</password>
</auth>
</to>
<container>
<mainClass>org.example.springbootjibdemo.SpringbootJibDemoApplication</mainClass>
<!-- 使用当前时间作为镜像的create-->
<creationTime>USE_CURRENT_TIMESTAMP</creationTime>
<volumes>
<volume>/logs</volume>
</volumes>
</container>
<!--容器相关的属性-->
<container>
<ports>
<port>8080</port>
</ports>
</container>
<!-- 如果私有镜像仓库没有启用https,设置allowInsecureRegistries参数为true-->
<allowInsecureRegistries>true</allowInsecureRegistries>
</configuration>
<!-- 将build绑定到package中,使用mvn package会自动执行build -->
<!-- <executions>
<execution>
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>-->
</plugin>
</plugins>
</build>
</project>

Controller#

package org.example.springbootjibdemo.controller;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
@RestController
public class HelloController {
@RequestMapping("/")
public String index() {
return "Greetings from Spring Boot and Jib!";
}
}

构建镜像并推送到docker仓库#

mvn compile -DskipTests=true jib:build

启动容器#

docker run -it -d --name spring-boot-jib-demo -p 8080:8080 registry.cn-shenzhen.aliyuncs.com/xxxxxx/spring-boot-jib-demo:latest

运行后查看启动容器的情况#

docker ps

测试#

浏览器访问 http://localhost:8080

使用Jib部署SpringBoot应用
https://blog.dongge.de/20200610223503/
作者
V.V.
发布于
2020-06-10
许可协议
CC BY-SA 4.0
封面
加载中...
加载中...
封面
加载中...
加载中...
0:00 / 0:00