侧边栏壁纸
博主头像
Lang博主等级

十七岁想打职业。

  • 累计撰写 10 篇文章
  • 累计创建 11 个标签
  • 累计收到 1 条评论
隐藏侧边栏

Jib构建springboot的docker镜像

Lang
2021-11-05 / 1 评论 / 0 点赞 / 657 阅读 / 2,434 字
温馨提示:
本文最后更新于 2022-01-21,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

1.介绍

Jib 是一个Google 开发的快速而简单的容器镜像构建工具;它不需要你编写 Dockerfile 或安装 Docker,而是可以直接集成到 Maven 和 Gradle中。

2.配置阿里云docker镜像仓库

地址:https://cr.console.aliyun.com/cn-hangzhou/instances

  1. 创建个人实例

  2. 创建命名空间和镜像仓库

  3. 命名空间开启自动创建仓库就可以不用手动创建仓库

  4. 设置仓库密码(账号为阿里云账号)

3.jib maven配置

<plugin>
    <groupId>com.google.cloud.tools</groupId>
    <artifactId>jib-maven-plugin</artifactId>
    <version>2.4.0</version>
    <configuration>
        <!-- 相当于 Dockerfile 中的 FROM -->
        <from>
            <image>openjdk:alpine</image>
        </from>
        <to>
            <!--构建镜像名称,阿里云镜像仓库地址-->
            <image>registry.cn-hangzhou.aliyuncs.com/lang_io/yr_cloud_clock</image>
            <!--阿里云镜像仓库账号密码-->
            <auth>
                <username>1102076808@qq.com</username>
                <password>xxxx</password>
            </auth>
            <!--Docker 镜像的 tag 这里使用maven定义的版本号-->
            <tags>
                <tag>
                    ${project.version}
                </tag>
            </tags>
        </to>
        <container>
            <mainClass>cn.yr.ClockApplication</mainClass>
            <jvmFlags>
                <jvmFlag>-Xms64M</jvmFlag>
                <jvmFlag>-Xmx64M</jvmFlag>
            </jvmFlags>
            <!-- 容器覆盖的启动参数 -->
            <args>
                <arg>
                    --spring.profiles.active=product
                </arg>
                <arg>
                    --spring.redis.host=172.18.0.1
                </arg>
                <arg>
                    --spring.datasource.host=172.18.0.1:3306
                </arg>
                <arg>
                    --spring.datasource.password=MySql@88!
                </arg>
            </args>
            <!--使用当前时间-->
            <creationTime>USE_CURRENT_TIMESTAMP</creationTime>
            <!--容器在运行时暴露的端口-->
            <ports>
                <port>60010</port>
            </ports>
        </container>
        <!--如果私有镜像仓库没有启用https,设置allowInsecureRegistries参数为true-->
        <allowInsecureRegistries>true</allowInsecureRegistries>
    </configuration>
    <!--绑定jib:build到 Maven生命周期,例如package-->
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>build</goal>
            </goals>
        </execution>
    </executions>
</plugin>

4.上传到阿里云镜像仓库

项目根目录执行

mvn clean compile jib:build

或者Idea的maven工具中执行jib:build

因为设置了 绑定jib:build到 Maven生命周期package;当执行完maven的package后会自动执行jib:build。

springboot的docker镜像推送到仓库成功

5.docker拉取镜像

  1. 登录阿里云Docker Registry
$ docker login --username=110207****@qq.com registry.cn-hangzhou.aliyuncs.com
  1. 从Registry中拉取镜像
$ docker pull registry.cn-hangzhou.aliyuncs.com/lang_io/yr_cloud_clock:[镜像版本号]

  1. 启动容器
docker run --name yr-cloud-clock \
-p 60010:60010 \
-v /data/server/yr-cloud-clock/resources:/app/resources \
-v /data/server/yr-cloud-clock/logs:/logs \
-d yr_cloud_clock
  1. 查看日志
docker logs -f --tail=100 容器id

0

评论