【问题标题】:Openshift git source with Docker file带有 Docker 文件的 Openshift git 源
【发布时间】:2020-04-14 07:04:35
【问题描述】:

是否可以在 Git 中拥有一个 java 源代码存储库并在 Dockerfile 中拥有一个 Dockerfile Git 存储库的根目录,以便在构建 maven 工件后 openshift 将使用提供的 Docker 文件来创建 docker 映像?

我一直在构建 Maven 工件,但之后它似乎由 S2I 处理。

更新 我正在使用 openshift Online,免费版。

代码是多模块的maven项目。

属性文件也在目标文件中。

【问题讨论】:

  • 如果您可以分享有关构建配置和管道的更多信息,这将有所帮助。

标签: git docker openshift


【解决方案1】:

这完全可以通过使用chained builds来完成。

具体来说,您将从 GitHub 存储库执行 S2I 构建

apiVersion: v1
kind: BuildConfig
metadata:
  name: artifact-build
spec:
  output:
    to:
      kind: ImageStreamTag
      name: artifact-image:latest
  source:
    git:
      uri: https://github.com/openshift/openshift-jee-sample.git
  strategy:
    sourceStrategy:
      from:
        kind: ImageStreamTag
        name: wildfly:10.1
        namespace: openshift

然后您将创建一个使用 Docker build 的链式构建

apiVersion: v1
kind: BuildConfig
metadata:
  name: image-build
spec:
  output:
    to:
      kind: ImageStreamTag
      name: image-build:latest
  source:
    dockerfile: |-
      FROM jee-runtime:latest
      COPY ROOT.war /deployments/ROOT.war
    images:
    - from: 
        kind: ImageStreamTag
        name: artifact-image:latest
      paths: 
      - sourcePath: /wildfly/standalone/deployments/ROOT.war
        destinationDir: "."
  strategy:
    dockerStrategy:
      from: 
        kind: ImageStreamTag
        name: jee-runtime:latest
  triggers:
  - imageChange: {}
    type: ImageChange

【讨论】:

    【解决方案2】:

    我找到了根本原因,这里还有其他需要考虑的事情。请注意以下内容以使其清楚。

    • 在多模块项目中,目标目录不在根目录内,因此 OpenShift S2I 看不到 jar 文件,这是最初的问题。

      修复是在构建配置中添加环境变量和目标文件夹

    • 但是,如果您需要将目标目录中的任何属性文件与 jar 文件一起复制,则必须将 pom 文件本身中的属性文件复制到 /deployment 目录。如下添加 openshift 配置文件并进行复制。

      <profiles>
      <profile>
          <!-- When built in OpenShift the 'openshift' profile will be used when 
              invoking mvn. -->
          <!-- Use this profile for any OpenShift specific customization your app 
              will need. -->
          <id>openshift</id>
          <build>
              <plugins>
                  <plugin>
                      <groupId>org.springframework.boot</groupId>
                      <artifactId>spring-boot-maven-plugin</artifactId>
                  </plugin>
              </plugins>
              <resources>
                  <resource>
                      <filtering>true</filtering><!-- if it is neccessary -->
                      <directory>${project.basedir}/src/main/resources</directory><!-- from -->
                      <targetPath>/deployments</targetPath><!-- to adding ARTIFACT_DIR on openshift only finds the jar file but not the prop, so copy it manually like this-->
                      <includes><!-- what -->
                          <include>*.properties</include>
                          <include>Dockerfile</include>
                      </includes>
                  </resource>
              </resources>
          </build>
      </profile>
      

    • 最后一点是,由于这是 Openshift 免费计划,因此不允许使用 Docker 策略。它将给出以下消息。

      在此集群上禁止使用 docker 策略构建 但是,如果需要基于 docker 的方式,仍然可以使用 oc 命令行工具来完成。

    但是@Will Gordon 提到的链式构建的概念非常重要。 根据我的情况,正常构建就像 250MB,因为它也会有 m2。但是通过链式构建,从初始构建到二次可部署构建创建的复制工件,我能够将图像大小减少到 200MB。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-27
      • 1970-01-01
      • 1970-01-01
      • 2018-10-29
      相关资源
      最近更新 更多