此外,如果”登录失败,未授权”,需要通过 docker login 登录鉴权一下。此外,更好的做法是,你可以考虑在Maven 中放置凭据。
- <settings>
- ...
- <servers>
- ...
- <server>
- <id>registry.cn-hangzhou.aliyuncs.com</id>
- <username>你的阿里云账号</username>
- <password>你的阿里云密码</password>
- </server>
- </servers>
- </settings>
最后,执行完成后,我们可以在阿里云镜像仓库获取镜像。
大功告成,现在,我们来验证一把。我们通过 docker pull 拉取镜像,并运行。
- docker pull registry.cn-hangzhou.aliyuncs.com/lianggzone/jib-helloworld:v1
- docker run --name jib-helloworld -it registry.cn-hangzhou.aliyuncs.com/lianggzone/jib-helloworld:v1 /bin/bash
执行结果,如下所示。
2. 构建一个 SpringBoot 的可运行 Jar
我们来一个复杂一些的项目,构建一个 SpringBoot 的项目。现在,我们首先需要搭建一个工程,并创建一个启动类。
- @SpringBootApplication
- public class Application {
- public static void main(String[] args) {
- SpringApplication.run(Application.class, args);
- }
- }
同时,需要一个 Web 的接口。
- @RestController
- public class WebController {
- @RequestMapping("/blog")
- public String index() {
- return "http://blog.720ui.com";
- }
- }
紧接着,我们再创建一个 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>
-
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.1.2.RELEASE</version>
- </parent>
-
- <groupId>com.lianggzone.sample.lib</groupId>
- <artifactId>springboot-samples</artifactId>
- <version>0.1</version>
- <packaging>jar</packaging>
- <name>springboot-samples</name>
-
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- </dependencies>
-
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <jib-maven-plugin.version>1.0.2</jib-maven-plugin.version>
- <maven-compiler-plugin.version>3.8.0</maven-compiler-plugin.version>
- </properties>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <version>${maven-compiler-plugin.version}</version>
- <configuration>
- <source>1.8</source>
- <target>1.8</target>
- </configuration>
- </plugin>
- <!-- Jib -->
- <plugin>
- <groupId>com.google.cloud.tools</groupId>
- <artifactId>jib-maven-plugin</artifactId>
- <version>${jib-maven-plugin.version}</version>
- <configuration>
- <from>
- <image>registry.cn-hangzhou.aliyuncs.com/lianggzone/oracle_java8</image>
- </from>
- <to>
- <image>registry.cn-hangzhou.aliyuncs.com/lianggzone/jib-springboot:v1</image>
- </to>
- <container>
- <jvmFlags>
- <jvmFlag>-Xms512m</jvmFlag>
- <jvmFlag>-Xdebug</jvmFlag>
- </jvmFlags>
- </container>
- </configuration>
- <executions>
- <execution>
- <phase>package</phase>
- <goals>
- <goal>build</goal>
- </goals>
- </execution>
- </executions>
- </plugin>
- </plugins>
- </build>
- </project>
(编辑:ASP站长网)
|