Docker与Jib(maven插件版)实战

本文涉及的产品
容器镜像服务 ACR,镜像仓库100个 不限时长
简介: 实战Jib的maven插件,将java应用构建成docker,并且推送到本地镜像仓库或者hub.docker.com

欢迎访问我的GitHub

这里分类和汇总了欣宸的全部原创(含配套源码): https://github.com/zq2599/blog_demos

关于Jib

  • Java应用如果要运行在docker环境,就要制作包含该应用的docker镜像,Jib就是用来制作此镜像的的工具,有三种使用Jib的方法:
  1. Maven插件:jib-maven-plugin;
  2. Gradle插件:jib-gradle-plugin;
  3. Java库:Jib Core;

实战环境信息

  1. 操作系统:CentOS Linux release 7.6.1810
  2. docker:1.13.1
  3. jdk:1.8.0_191
  4. maven:3.6.0
  • 接下来一起开发一个java应用,并使用jib的maven插件,来构建该应用对应的docker镜像;

源码下载

  • 如果您不打算写代码,也可以从GitHub上下载本次实战的源码,地址和链接信息如下表所示:
名称 链接 备注
项目主页 https://github.com/zq2599/blog_demos 该项目在GitHub上的主页
git仓库地址(https) https://github.com/zq2599/blog_demos.git 该项目源码的仓库地址,https协议
git仓库地址(ssh) git@github.com:zq2599/blog_demos.git 该项目源码的仓库地址,ssh协议
  • 这个git项目中有多个文件夹,本章的源码在hellojib文件夹下,如下图红框所示:

在这里插入图片描述

准备java应用

  • 准备一个最简单的springboot应用hellojib,用于实战构建docker镜像;
  • 该应用是基于maven构建的,启动类如下:
package com.bolingcavalry.hellojib;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class HellojibApplication {

    public static void main(String[] args) {
        SpringApplication.run(HellojibApplication.class, args);
    }

}
  • 有一个controller用于后面验证服务是否正常:
package com.bolingcavalry.hellojib.controller;

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

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Description: 普通的controller
 * @author: willzhao E-mail: zq2599@gmail.com
 * @date: 2019/6/29 20:21
 */
@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String hello(){
        return "Hello, " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
    }
}
  • 最重要的部分来了,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.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.bolingcavalry</groupId>
    <artifactId>hellojib</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>hellojib</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

            <!--使用jib插件-->
            <plugin>
                <groupId>com.google.cloud.tools</groupId>
                <artifactId>jib-maven-plugin</artifactId>
                <version>1.3.0</version>
                <configuration>
                    <!--from节点用来设置镜像的基础镜像,相当于Docerkfile中的FROM关键字-->
                    <from>
                        <!--使用openjdk官方镜像,tag是8-jdk-stretch,表示镜像的操作系统是debian9,装好了jdk8-->
                        <image>openjdk:8-jdk-stretch</image>
                    </from>
                    <to>
                        <!--镜像名称和tag,使用了mvn内置变量${project.version},表示当前工程的version-->
                        <image>bolingcavalry/hellojib:${project.version}</image>
                    </to>
                    <!--容器相关的属性-->
                    <container>
                        <!--jvm内存参数-->
                        <jvmFlags>
                            <jvmFlag>-Xms4g</jvmFlag>
                            <jvmFlag>-Xmx4g</jvmFlag>
                        </jvmFlags>
                        <!--要暴露的端口-->
                        <ports>
                            <port>8080</port>
                        </ports>
                    </container>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
  • 上述xml中有以下几处需要注意:

a. 插件的节点中增加了插件jib-maven-plugin的配置;
b. from节点用来设置基础镜像,一般使用openjdk官方镜像,如果您想了解更多请参考《openjdk镜像的tag说明》
c. to节点用于指定镜像名称和tag;
d. container节点用来设置容器的属性,例如对外暴露的端口、jvm参数等;

  • 完成了上述设置,就可以构建镜像了;

构建成tar文件

  • 还记得常用的docker到处到处命令么:
  • docker save命令将本地仓库的docker镜像导出成tar格式的文件;
  • docker load命令可以将tar格式的镜像文件导入到本地镜像仓库;
  • 利用Jib,我们可以将java应用做成docker镜像并生成tar格式的文件,操作步骤如下:
  • 在上述java工程的pom.xml文件所在目录,执行以下命令:
mvn compile jib:buildTar
  • 构建成功后,控制台提示以下信息:
[root@centos7 hellojib]# mvn compile jib:buildTar
[INFO] Scanning for projects...
[INFO]
[INFO] ---------------------< com.bolingcavalry:hellojib >---------------------
[INFO] Building hellojib 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ hellojib ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ hellojib ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- jib-maven-plugin:1.3.0:buildTar (default-cli) @ hellojib ---
[INFO]
[INFO] Containerizing application to file at '/root/temp/201906/29/hellojib/target/jib-image.tar'...
[INFO] The base image requires auth. Trying again for openjdk:8-jdk-stretch...
[INFO]
[INFO] Container entrypoint set to [java, -Xms4g, -Xmx4g, -cp, /app/resources:/app/classes:/app/libs/*, com.bolingcavalry.hellojib.HellojibApplication]
[INFO]
[INFO] Built image tarball at /root/temp/201906/29/hellojib/target/jib-image.tar
[INFO] Executing tasks:
[INFO] [==============================] 100.0% complete
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  16.521 s
[INFO] Finished at: 2019-06-30T21:43:32+08:00
[INFO] ------------------------------------------------------------------------
  • 去java工程的target目录下,发现生成了名为jib-image.tar的文件,如下图红框所示:

在这里插入图片描述

  • 在java工程的target目录下执行命令docker load < jib-image.tar即可将该tar文件中的镜像加载到本地镜像仓库:
[root@centos7 target]# docker load < jib-image.tar
2c8d31157b81: Loading layer [==================================================>] 50.07 MB/50.07 MB
ea20c4bf3aae: Loading layer [==================================================>] 4.932 MB/4.932 MB
51774d97c868: Loading layer [==================================================>]    221 B/221 B
51566e3f832b: Loading layer [==================================================>]   104 MB/104 MB
64c0b8425948: Loading layer [==================================================>] 14.91 MB/14.91 MB
e4c3b920750d: Loading layer [==================================================>]    240 B/240 B
8917d4e7f7b3: Loading layer [==================================================>] 1.034 kB/1.034 kB
Loaded image: bolingcavalry/hellojib:0.0.1-SNAPSHOT
  • 执行命令docker images可见镜像加载成功:
[root@centos7 target]# docker images
REPOSITORY                         TAG                         IMAGE ID            CREATED             SIZE
bolingcavalry/hellojib             0.0.1-SNAPSHOT              cf93bd81fbd5        49 years ago        505 MB
  • 可见通过jib生成的tar是正常的docker镜像文件,可以导入

验证镜像是否可用

  • 执行以下命令,即可启动容器:
docker run --rm -p 8080:8080 bolingcavalry/hellojib:0.0.1-SNAPSHOT
  • 控制台显示springboot的启动信息:
[root@centos7 target]# docker run --rm -p 8080:8080 bolingcavalry/hellojib:0.0.1-SNAPSHOT

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.6.RELEASE)

2019-06-30 13:56:50.969  INFO 1 --- [           main] c.b.hellojib.HellojibApplication         : Starting HellojibApplication on c08711533071 with PID 1 (/app/classes started by root in /)
2019-06-30 13:56:50.975  INFO 1 --- [           main] c.b.hellojib.HellojibApplication         : No active profile set, falling back to default profiles: default
2019-06-30 13:56:52.147  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2019-06-30 13:56:52.173  INFO 1 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-06-30 13:56:52.173  INFO 1 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.21]
2019-06-30 13:56:52.261  INFO 1 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-06-30 13:56:52.261  INFO 1 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1223 ms
2019-06-30 13:56:52.475  INFO 1 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-06-30 13:56:52.772  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2019-06-30 13:56:52.811  INFO 1 --- [           main] c.b.hellojib.HellojibApplication         : Started HellojibApplication in 2.202 seconds (JVM running for 2.446)
  • 用浏览器访问:http://192.168.121.132:8080/hello ,其中192.168.121.132是宿主机IP地址,如下图,可见springboot服务可以正常访问:

在这里插入图片描述

构建到本地镜像仓库

  • 上面的操作是将java应用生成tar文件然后再导入到本地镜像仓库,您也可以直接将java应用构建到本地镜像仓库中:
  • 先执行命令docker rmi bolingcavalry/hellojib:0.0.1-SNAPSHOT将前面构建的镜像删除;
  • 在pom.xml文件所在目录执行以下命令即可:
mvn clean compile jib:dockerBuild -U
  • 终端提示如下:
[root@centos7 hellojib]# mvn clean compile jib:dockerBuild -U
[INFO] Scanning for projects...
[INFO]
[INFO] ---------------------< com.bolingcavalry:hellojib >---------------------
[INFO] Building hellojib 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ hellojib ---
[INFO] Deleting /root/temp/201906/29/hellojib/target
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ hellojib ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ hellojib ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to /root/temp/201906/29/hellojib/target/classes
[INFO]
[INFO] --- jib-maven-plugin:1.3.0:dockerBuild (default-cli) @ hellojib ---
[INFO]
[INFO] Containerizing application to Docker daemon as bolingcavalry/hellojib:0.0.1-SNAPSHOT...
[INFO] The base image requires auth. Trying again for openjdk:8-jdk-stretch...
[INFO]
[INFO] Container entrypoint set to [java, -Xms4g, -Xmx4g, -cp, /app/resources:/app/classes:/app/libs/*, com.bolingcavalry.hellojib.HellojibApplication]
[INFO]
[INFO] Built image to Docker daemon as bolingcavalry/hellojib:0.0.1-SNAPSHOT
[INFO] Executing tasks:
[INFO] [==============================] 100.0% complete
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  20.923 s
[INFO] Finished at: 2019-06-30T22:04:36+08:00
[INFO] ------------------------------------------------------------------------
  • 执行命令docker images可见镜像已经在镜像仓库中了:
[root@centos7 hellojib]# docker images
REPOSITORY                         TAG                         IMAGE ID            CREATED             SIZE
bolingcavalry/hellojib             0.0.1-SNAPSHOT              cf93bd81fbd5        49 years ago        505 MB

构建到hub.docker.com

  • docker的中央仓库是 hub.docker.com ,jib也可以在镜像构建成功后自动推送到hub.docker.com网站,前提是您已经在该网站注册过,例如我已经注册过,账号是bolingcavalry,因此名为bolingcavalry/XXXXXXXX的镜像都可以推送到 hub.docker.com:
  • 在pom.xml文件所在目录执行以下命令即可自动推送到 hub.docker.com,其中bolingcavalry是我在hub.docker.com上的注册账号,123456是密码:
mvn compile jib:build -Djib.to.auth.username=bolingcavalry -Djib.to.auth.password=123456
  • 终端信息如下:
[root@centos7 hellojib]# mvn compile jib:build -Djib.to.auth.username=bolingcavalry -Djib.to.auth.password=123456
[INFO] Scanning for projects...
[INFO]
[INFO] ---------------------< com.bolingcavalry:hellojib >---------------------
[INFO] Building hellojib 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ hellojib ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ hellojib ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- jib-maven-plugin:1.3.0:build (default-cli) @ hellojib ---
[INFO]
[INFO] Containerizing application to bolingcavalry/hellojib:0.0.1-SNAPSHOT...
[INFO] The base image requires auth. Trying again for openjdk:8-jdk-stretch...
[INFO]
[INFO] Container entrypoint set to [java, -Xms4g, -Xmx4g, -cp, /app/resources:/app/classes:/app/libs/*, com.bolingcavalry.hellojib.HellojibApplication]
[INFO]
[INFO] Built and pushed image as bolingcavalry/hellojib:0.0.1-SNAPSHOT
[INFO] Executing tasks:
[INFO] [==============================] 100.0% complete
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  02:47 min
[INFO] Finished at: 2019-06-30T22:16:19+08:00
[INFO] ------------------------------------------------------------------------
  • 在hub.docker.com上已经可以看到此镜像了,如下图(要登录后再看自己的镜像列表,如果没有登录是无法立即查到的):

在这里插入图片描述

  • 至此,Jib的实战已经完成,常用的操作相信您已经熟悉了,希望本文能帮助您做出满意的镜像;

欢迎关注阿里云开发者社区博客:程序员欣宸

学习路上,你不孤单,欣宸原创一路相伴...
相关实践学习
通过容器镜像仓库与容器服务快速部署spring-hello应用
本教程主要讲述如何将本地Java代码程序上传并在云端以容器化的构建、传输和运行。
Kubernetes极速入门
Kubernetes(K8S)是Google在2014年发布的一个开源项目,用于自动化容器化应用程序的部署、扩展和管理。Kubernetes通常结合docker容器工作,并且整合多个运行着docker容器的主机集群。 本课程从Kubernetes的简介、功能、架构,集群的概念、工具及部署等各个方面进行了详细的讲解及展示,通过对本课程的学习,可以对Kubernetes有一个较为全面的认识,并初步掌握Kubernetes相关的安装部署及使用技巧。本课程由黑马程序员提供。 &nbsp; 相关的阿里云产品:容器服务 ACK 容器服务 Kubernetes 版(简称 ACK)提供高性能可伸缩的容器应用管理能力,支持企业级容器化应用的全生命周期管理。整合阿里云虚拟化、存储、网络和安全能力,打造云端最佳容器化应用运行环境。 了解产品详情:&nbsp;https://www.aliyun.com/product/kubernetes
相关文章
|
3天前
|
关系型数据库 应用服务中间件 PHP
实战~如何组织一个多容器项目docker-compose
本文介绍了如何使用Docker搭建Nginx、PHP和MySQL的环境。首先启动Nginx容器并查看IP地址,接着启动Alpine容器并安装curl测试连通性。通过`--link`方式或`docker-compose`配置文件实现服务间的通信。最后展示了Nginx配置文件和PHP代码示例,验证了各服务的正常运行。
18 3
实战~如何组织一个多容器项目docker-compose
|
19天前
|
存储 缓存 监控
Docker容器性能调优的关键技巧,涵盖CPU、内存、网络及磁盘I/O的优化策略,结合实战案例,旨在帮助读者有效提升Docker容器的性能与稳定性。
本文介绍了Docker容器性能调优的关键技巧,涵盖CPU、内存、网络及磁盘I/O的优化策略,结合实战案例,旨在帮助读者有效提升Docker容器的性能与稳定性。
54 7
|
28天前
|
运维 Cloud Native 云计算
云原生之旅:Docker容器化实战
本文将带你走进云原生的世界,深入理解Docker技术如何改变应用部署与运维。我们将通过实际案例,展示如何利用Docker简化开发流程,提升应用的可移植性和伸缩性。文章不仅介绍基础概念,还提供操作指南和最佳实践,帮助你快速上手Docker,开启云原生的第一步。
|
1月前
|
机器学习/深度学习 数据采集 Docker
Docker容器化实战:构建并部署一个简单的Web应用
Docker容器化实战:构建并部署一个简单的Web应用
|
21天前
|
Kubernetes Linux 开发者
深入探索容器化技术——Docker 的实战应用
深入探索容器化技术——Docker 的实战应用
67 0
|
22天前
|
存储 Cloud Native 开发者
深入探索容器化技术——Docker的实战应用
深入探索容器化技术——Docker的实战应用
35 0
|
22天前
|
存储 安全 Docker
Docker 的实战应用与优化策略
Docker 的实战应用与优化策略
33 0
|
23天前
|
JavaScript Linux 持续交付
深入探索容器化技术——Docker 的实战应用
深入探索容器化技术——Docker 的实战应用
48 0
|
29天前
|
Kubernetes 监控 安全
容器化技术:Docker与Kubernetes的实战应用
容器化技术:Docker与Kubernetes的实战应用
|
1月前
|
Kubernetes Docker 容器
掌握Docker容器化技术:从入门到实战
掌握Docker容器化技术:从入门到实战
27 0