Jib是什么?
使用Jib,您可以在本机没有安装Docker的情况下为您的Java应用构建优化的Docker和OCI镜像,并且可以将构建好的镜像推送到远程镜像仓库。它提供Maven插件和Gradle插件以及Java库形式。
项目环境
- Spring Boot 2.3.0.RELEASE
- Java 8
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
| <?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
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| 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仓库
1
| mvn compile -DskipTests=true jib:build
|
启动容器
1
| docker run -it -d --name spring-boot-jib-demo -p 8080:8080 registry.cn-shenzhen.aliyuncs.com/xxxxxx/spring-boot-jib-demo:latest
|
运行后查看启动容器的情况
测试
浏览器访问 http://localhost:8080