我正在尝试创建一个管道,其中使用 JIB(通过 Maven 插件)创建 docker 映像并将其推送到我的 Gitlab 注册表。
当我登录到我的 docker 注册表时,这在本地工作得很好。
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<version>1.0.0</version>
<configuration>
<allowInsecureRegistries>true</allowInsecureRegistries>
<from>
<image>dockerhost/projectgroup/alpine</image>
</from>
<to>
<image>dockerhost/project/imagename:${project.version}</image>
</to>
<container>
<useCurrentTimestamp>true</useCurrentTimestamp>
</container>
</configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)
假设我有一个 .gitlab-ci.yml ,如下所示:
stages:
- build_image
build_image:
stage: build_image
tags:
- dev
script: |
mvn compile jib:build
Run Code Online (Sandbox Code Playgroud)
现在,当管道被触发时,我遇到了异常
Build image failed: Failed to authenticate with registry dockerhost/projectgroup/alpine because: peer not authenticated
Run Code Online (Sandbox Code Playgroud)
我假设我收到此错误是因为我尚未运行 docker login -u [用户名] -p [密码/令牌]
我如何需要一个使用 docker-in-docker 映像的 .gitlab-ci.yml 才能在我的脚本中运行 docker 登录?
除了使用 docker-in-docker 映像在我的 Gitlab CI 上构建此映像之外,还有其他选择吗?
JHipster 现在使用 maven-jib-plugin。在此之前,我在 docker-container 中运行的 jenkins 服务器能够使用 *.war 文件构建 docker 映像,并使用“Jenkinsfile”(对于 gradle,但我切换了)通过管道将其推送到我自己的 docker-registry到 Maven),作业完成后,另一个作业通过使用 ssh 在远程主机上执行 shell 脚本,将新构建的 docker-image 拉入我服务器上的新 docker-container 中。
这项任务的阶段是:
def dockerImage
stage('build docker') {
sh "cp -Rvvv src/main/docker build/"
sh "cp -vvv build/libs/*.war build/docker/"
dockerImage = docker.build("$IMAGE_NAME:$IMAGE_TAG", "build/docker")
}
stage('publish docker') {
docker.withRegistry("$REGISTRY_URL", "$REGISTRY_USER") {
dockerImage.push "$IMAGE_TAG"
}
}
stage('Remove Unused docker image') {
sh "docker rmi $IMAGE_NAME:$IMAGE_TAG"
}
Run Code Online (Sandbox Code Playgroud)
现在据我所知,使其变得更容易,并且生成jib
的相关部分可以归结为Jenkinsfile
$ jhipster ci-cd
def dockerImage
stage('publish docker') {
sh "./mvnw -ntp jib:build -Dimage=$REGISTRY/$IMAGE_NAME:$IMAGE_TAG …
Run Code Online (Sandbox Code Playgroud) 在 distroless java docker image 中更改次要版本。
当前的 java 项目使用 maven jib 来构建 docker 镜像。docker 镜像的默认版本为 java 11。此 docker 镜像的次要 java 版本设置为 11.0.6。
如何将此映像 gcr.io/distroless/java:11 的 java 次要版本从 11.0.6 更改为更高版本。
我想尝试一下具有预览功能的最新 Java 15。我使用的是 Spring Boot 2.4.0-M2 和 Gradle 6.7-rc2,它们都支持 Java 15 功能。
我想使用 jib 从我的项目构建一个 docker 镜像。这是我的副臂配置:
jib {
from {
image = 'openjdk:15-jdk'
}
to {
image = '<username>/<project>'
}
container {
jvmFlags = ['--enable-preview']
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,当我运行时,./gradlew jib
我收到以下错误:
Execution failed for task ':jib'.
> Records requires ASM8
Run Code Online (Sandbox Code Playgroud)
这是使用标志运行时的输出--info
:
> Task :jib FAILED
Caching disabled for task ':jib' because:
Build cache is disabled
Task ':jib' is not up-to-date because:
Task has not declared any outputs …
Run Code Online (Sandbox Code Playgroud) 在过去的几个月里,我一直在试验 docker,并享受在容器内构建和运行 java 应用程序的好处。
几周前,我偶然发现了jib maven 插件,并注意到 jib 可以在不使用 docker daemon 的情况下构建镜像到 docker 注册表。
在将 jib 添加到我的一个项目并运行mvn clean install jib:build
(在没有安装 docker 的 VM 上)之后,我惊讶地发现 jib 实际上构建了一个包含我的项目的映像并将其推送到远程注册表。
出于好奇,我上网阅读了更多关于 jib 如何在没有安装 docker 的情况下构建和推送 docker 镜像的信息,但几乎没有找到关于该主题的信息。我设法找到了一篇文章,它解释了一些不使用 docker 来创建图像的方法,并试图jib:build
通过阅读它的源代码来了解 Maven 目标的工作原理,但是这两篇文章都没有给我任何关于运行时幕后发生的事情的见解jib:build
.
如果有人分享更多关于 jib maven 插件以及它如何在不使用 docker 守护进程的情况下在幕后实际构建和推送图像,我将不胜感激。
我有一个 Spring boot 应用程序,并使用 spotify 插件来对我的应用程序进行 Dockerize。所以,我将有一个像下面这样的 Dockerfile。
FROM jdk1.8:latest
RUN mkdir -p /opt/servie
COPY target/service.war /opt/service
ENV JAVA_OPTS="" \
JAVA_ARGS=""
CMD java ${JAVA_OPTS} -jar /opt/service/service.war ${JAVA_ARGS}
Run Code Online (Sandbox Code Playgroud)
我遇到了 JIB,它看起来很酷。但是,努力让它工作。
我在下面添加了 pom 条目。
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<version>0.9.6</version>
<configuration>
<from>
<image>jdk1.8:latest</image>
</from>
<to>
<image>docker.hub.com/test/service</image>
</to>
</configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)
mvn 编译 jib:build
我看到以下内容。
[INFO] 构建依赖层... [INFO] 构建类层... [INFO] 构建资源层...
当我运行 docker 镜像时,它说 Jar 文件不存在。我有一个多模块 maven 项目,并希望在从父 pom 运行 mvn compile jib:build 时对多个模块进行 dockerize。这有什么帮助吗?
使用 Jib 构建 Docker 镜像是否有助于优化远程 Docker 存储库存储?
我们在 Docker 中使用 Spring Boot 和 Gradle。目前,我们正在创建标准的 fat Boot jar,其中包含所有依赖项,然后用它创建一个映像,如下所示:
FROM gcr.io/distroless/java:11
COPY ./build/libs/*.jar app.jar
CMD ["app.jar"]
Run Code Online (Sandbox Code Playgroud)
这会导致我们每次构建时都会生成一个大的(250 MB)新映像,即使实际上更改的代码很少。这是因为 fat jar 包含共享依赖项(不经常更改)和我们的代码。这是我们的私有存储库中存储空间的低效使用,我们希望改变这一点。
为此,想法如下:
我们创建一个基本映像,其中仅包含 /opt/libs 中的依赖项,让我们调用它spring-base:1.0.0
并将其推送到我们的私有 Docker 注册表。
我们使用该图像作为仅包含我们的代码的应用程序图像的父级/基础。Dockerfile 看起来与此类似(未经测试,只是为了展示概念):
FROM our-registry/spring-base:1.0.0
COPY ./build/classes/kotlin/main/* /opt/classes
COPY ./build/resources/main/* /opt/resources
ENTRYPOINT ["java", "-cp", "/opt/libs/*:/opt/resources:/opt/classes", "com.example.MainKt"]
Run Code Online (Sandbox Code Playgroud)期望这些镜像要小得多,并且具有依赖关系的大基础镜像仅存储一次,从而节省大量存储空间。
我们的一位同事研究了 Jib,并坚称它正是这样做的,但在阅读了整个文档和常见问题解答并尝试了一下之后,我不太确定。我们集成并使用它./gradlew jibDockerBuild
,它似乎确实为依赖项、资源和类创建了层,但仍然只有一张大图像。Jib 似乎专注于加快构建时间(通过利用 Docker 层缓存)和可重现的构建,但我认为当我们将该映像上传到存储库时,相对于我们当前的解决方案不会发生任何变化 - 我们仍将存储“静态”依赖项多次,但现在我们将在每个新图像中拥有多个图层,而不是只有一个图层。
有更多 Docker 和 Jib 经验的人能否解释一下 Jib 是否为我们提供了我们正在寻找的存储空间优化?
编辑:当我在等待答案时,我尝试了所有这些并使用了https://github.com/wagoodman/dive,docker system df
并docker images …
我正在从这里关注 Spring Boot Docker 教程。
我在连接互联网时得到了确切的结果。现在我需要在没有互联网连接的环境中产生相同的结果。我将 maven 存储库和 docker 镜像复制到新环境中。我很确定 maven 和 docker 已经启动并正在运行。
当我尝试运行以下命令时,com.google.cloud.tools:jib-maven-plugin:dockerBuild -Dimage=eureka
我收到错误消息。我猜有一些文件插件无法找到但不确定哪些文件。
我正在添加错误消息
[INFO] Scanning for projects...
[INFO]
[INFO] ---------------------------< com.ays:eureka >---------------------------
[INFO] Building eureka 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- jib-maven-plugin:2.1.0:dockerBuild (default-cli) @ eureka ---
[INFO]
[INFO] Containerizing application to Docker daemon as eureka...
[WARNING] Base image 'gcr.io/distroless/java:8' does not use a specific image digest - build may not be reproducible
[ERROR] I/O error for image [gcr.io/distroless/java]:
[ERROR] org.apache.http.conn.ConnectTimeoutException …
Run Code Online (Sandbox Code Playgroud) 普通docker build
命令在本地创建映像,并且不会自动推送到注册表。是否可以使用 Gradle 插件对 jib 执行相同的操作?
看起来像是从 Jib 3.0 开始;您的 Java 应用程序不再拥有默认的 distroless 映像。相反,如果您不指定,您将获得一个 AdoptOpenjdk 基础镜像。您仍然可以按照此链接配置和使用 distroless 基础映像。我只是想知道 采用的 OpenJDK 镜像是否比 distroless 更安全、更纤薄?有什么好处?
我正在尝试构建 docker 映像,但收到一条错误消息,告诉我 jib-maven-plugin 失败。导致出现不支持的类文件主要版本 61 错误。
起初我认为这与我使用的 java 版本(Java 17)有关。所以我从我的机器上卸载了它并安装了 Java 15,但没有成功。
我试图运行的命令:
./mvnw compile jib:dockerBuild -Djib.to.image=fullstack:v1
Run Code Online (Sandbox Code Playgroud)
我得到的错误响应:
Failed to execute goal com.google.cloud.tools:jib-maven-plugin:2.5.2:dockerBuild (default-cli) on project demo: Execution default-cli of goal com.google.cloud.tools:jib-maven-plugin:2.5.2:dockerBuild failed: Unsupported class file major version 61 -> [Help 1]
Run Code Online (Sandbox Code Playgroud)
我的 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 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.4.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>15</java.version>
</properties>
<dependencies> …
Run Code Online (Sandbox Code Playgroud) 我正在创建一个项目并使用 Jib 创建容器并将它们推送到 ECR。这是一个多模块maven项目,有3个子模块,其中2个是标准的java spring-boot项目,可以与Jib一起正常工作,另一个是使用mvn构建的npm项目。这是它的 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>my-search-frontend</artifactId>
<packaging>pom</packaging>
<version>1.11.0-SNAPSHOT</version>
<name>my search frontend</name>
<description>my search frontend</description>
<parent>
<artifactId>my-search</artifactId>
<groupId>com.regalo.my</groupId>
<version>1.11.0-SNAPSHOT</version>
</parent>
<build>
<finalName>my-search-frontend</finalName>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<filesets>
<fileset>
<directory>node_modules</directory>
<includes>
<include>**/*</include>
</includes>
<followSymlinks>false</followSymlinks>
</fileset>
<fileset>
<directory>build</directory>
<includes>
<include>**/*</include>
</includes>
<followSymlinks>false</followSymlinks>
</fileset>
</filesets>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<id>npm-install</id>
<phase>initialize</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>npm</executable>
<arguments>
<argument>install</argument>
</arguments>
<workingDirectory>${project.basedir}</workingDirectory>
</configuration>
</execution>
<execution>
<id>npm-run-build</id>
<phase>generate-sources</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration> …
Run Code Online (Sandbox Code Playgroud) 如果是docker文件,我想通过执行以下命令来删除该目录。
RUN rm /usr/bin/wget
Run Code Online (Sandbox Code Playgroud)
我该怎么做?任何帮助表示赞赏
jib ×13
docker ×11
java ×6
maven ×4
maven-jib ×3
spring-boot ×3
gradle ×2
containers ×1
docker-build ×1
dockerfile ×1
gcr.io ×1
gitlab ×1
gitlab-ci ×1
jhipster ×1
maven-plugin ×1
pom.xml ×1