Jib - 将图像中的 webapp 文件夹复制到哪里?

Jib - where to copy webapp folder inside image?

我正在使用 google 的 Jib maven 插件创建 docker 图像,图像创建成功并且后端服务工作正常,但我的 webapp 文件夹不是该图像的一部分。在 jib 之前,我正在创建一个包含所有内容的 zip(包括该 zip 根目录中的 webapp 文件夹以及可执行 jar),它工作正常。

现在jib创建的镜像在app根目录下有类、库、资源。我应该如何以及在哪里复制 webapp 文件夹?

它通过使用 maven jib 插件提供的外部目录为我工作。

<extraDirectories>
    <paths>
      <path>webapp</path>
      <path>
        <from>webapp</from>
        <into>/app/webapp</into>
      </path>
    </paths>
</extraDirectories>

上面的对我不起作用,但下面的对我有用。

<extraDirectories>
    <paths>
        <path>
            <from>../path/to/frontend/app/dist</from>
            <into>/app/resources/static</into>
        </path>
    </paths>
</extraDirectories>

我目前正在使用 运行ing spring-boot 版本 2.4.10 并且应用程序被打包为 jar.

我的项目在以下位置有 JSP:

src/main/webapp/WEB-INF/jsp

这很重要,因为它允许我将应用程序 运行 作为可执行 jar:java -jar ./target/app.jar --spring.profiles.active=prod

jib-plugin默认不会将src/main/webapp目录复制到容器镜像中,所以我们需要手动添加,包括如下配置。

<extraDirectories>
   <paths>
      <path>
          <from>src/main/webapp/WEB-INF</from>
          <into>/app/resources/META-INF/resources/WEB-INF</into>
      </path>
   </paths>
</extraDirectories>

我提供jib-plugin一个自定义entrypoint.sh

entrypoint.sh 位于 src/main/jib

#!/bin/sh
echo "The application will start in ${APPLICATION_SLEEP}s..." && sleep ${APPLICATION_SLEEP}
exec java ${JAVA_OPTS} -noverify -XX:+AlwaysPreTouch \
-Djava.security.egd=file:/dev/./urandom -cp /app/resources/:/app/classes/:/app/libs/* \
"com.demo.application.Application"  "$@"

我的最终 jib-plugin 配置如下:

<plugin>
    <groupId>com.google.cloud.tools</groupId>
    <artifactId>jib-maven-plugin</artifactId>
    <version>${jib-maven-plugin.version}</version>
    <configuration>
        <from>
            <image>adoptopenjdk:11-jre-hotspot</image>
        </from>
        <to>
            <image>myprivateregistry/app/${project.name}</image>
            <tags>
                <tag>latest</tag>
                <tag>${project.version}</tag>
            </tags>
        </to>
        <container>
            <entrypoint>
                <shell>bash</shell>
                <option>-c</option>
                <arg>/entrypoint.sh</arg>
            </entrypoint>
            <ports>
                <port>8080</port>
            </ports>
            <environment>
                <SPRING_OUTPUT_ANSI_ENABLED>ALWAYS</SPRING_OUTPUT_ANSI_ENABLED>
                <APPLICATION_SLEEP>0</APPLICATION_SLEEP>
            </environment>
            <creationTime>USE_CURRENT_TIMESTAMP</creationTime>
        </container>
        <extraDirectories>
            <paths>
                <path>src/main/jib</path>
                <path>
                    <from>src/main/webapp/WEB-INF</from>
                    <into>/app/resources/META-INF/resources/WEB-INF</into>
                </path>
            </paths>
            <permissions>
                <permission>
                    <file>/entrypoint.sh</file>
                    <mode>755</mode>
                </permission>
            </permissions>
        </extraDirectories>
    </configuration>
    <!-- Make JIB plugin run during the packing life cycle -->
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>build</goal>
            </goals>
        </execution>
    </executions>
</plugin>