要把本地的項目生成鏡像在發佈到阿里雲上去, 手動也可以完成, 但是現在google給我們開源了一個更簡單的功能. jib. 很久以前已經發布了1.0版本, 這次我們適用最新的版本 1.8.0. 其實很簡單的
分爲以下幾個步驟:
1. 修改maven的配置
其實就是添加下面的plugin, 就可以了. 運行命令 mvn compile jib:build
這是我新建的一個springboot 項目, 裏面就一個啓動類(省略), 還有一個controller , 一個hello方法. 還有
package com.curefun.authority;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author zhangke
* @date 2019年12月9日15:07:34
*/
@RestController
public class HelloController {
@GetMapping("hello")
public String getHello(){
return "success do it";
}
}
如果開始說沒有授權的話, 是先在命令行執行
docker login [email protected] registry.cn-hangzhou.aliyuncs.com
輸入你設置的密碼 , 記住了, 不是阿里雲的登陸密碼.
用的時候這個密碼
記得把pom中的密碼也更正過來.
build success之後就可以在 鏡像版本 中看到了.
這就是我剛纔上傳的鏡像了.
然後現在我在本地拉取阿里雲上剛上傳的鏡像
docker pull registry.cn-hangzhou.aliyuncs.com/onyx-cc/demo2:v1
哈哈哈, 不得不說, 國內的網絡就是快. 快的不行.
docker images 查看本地的鏡像
運行鏡像:
docker run -it --rm -p8080:8080 鏡像名字
現在你就可以試試, 服務是都正常了. 哈哈哈
查看完了, 該關掉docker 了, 畢竟電腦的內存已經用到了9G, 快爆照了.
docker ps 查看正在運行的容器
docker stop 名字, 關掉正在運行的容器, 好了.結束.
附件:
完整的pom文件
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.curefun.authority</groupId>
<artifactId>authority-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>authority</name>
<description>權限系統</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>io.spring.platform</groupId>
<artifactId>platform-bom</artifactId>
<version>Cairo-SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<version>1.8.0</version>
<configuration>
<!-- 拉取所需的基礎鏡像 - 這裏用於運行springboot項目 -->
<from>
<!--base image-->
<!--<image>openjdk:alpine</image>-->
<image>openjdk:8-jre-alpine</image>
</from>
<!-- 最後生成的鏡像配置 -->
<to>
<!--<image>registry.cn-hangzhou.aliyuncs.com/m65536/jibtest</image>-->
<!--目標鏡像registry地址,爲了方便測試,你需要換成自己的地址,如果你的網絡不好,可以選用國內加速器,比如阿里雲的-->
<image>registry.cn-hangzhou.aliyuncs.com/onyx-cc/demo2</image>
<!-- 鏡像版本號 -->
<tags>
<tag>v1</tag>
</tags>
<auth>
<username>[email protected]</username>
<password>123456</password>
</auth>
</to>
<!--由於我配置的私有鏡像倉庫沒有設置https,所以設置-->
<allowInsecureRegistries>true</allowInsecureRegistries>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- 運行maven命令的時候,讓選擇jdk 還是jre的時候,配置這個 -->
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<fork>true</fork>
<executable>C:\Program Files\Java\jdk1.8.0_172\bin\javac</executable>
</configuration>
</plugin>
</plugins>
</build>
</project>
好了, 就這麼結束了, 這份pom算是很簡單的配置了, 如果需要更加複雜的配置, 就是平時寫的dockerfile中的內容,
請出門左轉去github上查看文檔, 那個老詳細了.
傳送門是:https://github.com/GoogleContainerTools/jib 不用謝.
這裏放2篇我寫時候, 幫助我的博客, 寫的很好, 你們也可以參考一下. 挺好的
https://juejin.im/post/5b4e9c316fb9a04fa01d39d6
http://www.spring4all.com/article/19032
好了,寫完了. 如果您覺得寫得不多, 可以請作者喝一杯咖啡