每当容器从 jib 映像启动时,是否可以使用 cmd 或入口点或 jib maven 插件中的任何其他机制运行 Linux 命令,然后启动 java 进程?
就我而言,我想运行这个命令:
echo "127.0.0.1 $HOSTNAME" >> /etc/hosts
Run Code Online (Sandbox Code Playgroud)
您始终可以使用 设置自定义入口点<container><entrypoint>。您可以启动 shell 脚本、运行不同的程序等。有时,您可能希望使用该<extraDirectories>功能来复制脚本(并为其授予可执行权限)。
请参阅此处了解运行 shell 脚本的一些想法:
另一种选择是定义您自己的
<entrypoint>外壳来使用。(因此,您需要一个包含 shell 二进制文件的基础映像(例如/bin/bash)。请注意,Jib 3.0 之前的默认基础映像是 Distroless,并且不包含 shell 程序。OTOH,Jib 3.0+ 不使用 Distroless。)在此方法中,您需要知道正确的 Java 运行时类路径和要在 JVM 启动命令中使用的主类。为了帮助实现这一点,从 Jib >= 3.1 开始,Jib 在构建的映像中创建了两个 JVM 参数文件;它们将分别保存构建映像中的类路径和主类。知道了入口点,你可以编写一个shell脚本(
my-entrypoint.sh):Run Code Online (Sandbox Code Playgroud)#!/bin/sh # Assumes `java` is on PATH in the base image. exec java $JAVA_OPTS \ -cp $( cat /app/jib-classpath-file ) \ $( cat /app/jib-main-class-file )或者,如果您使用的是 Java 9+,则可以利用 @-argument 文件:
Run Code Online (Sandbox Code Playgroud)exec java $JAVA_OPTS -cp @/app/jib-classpath-file @/app/jib-main-class-file放置
my-entrypoint.sh在<project root>/src/main/jib. 这是 Jib 功能的默认目录<extraDirectories>,Jib 将放置src/main/jib/my-entrypoint.sh在容器镜像的根目录下。<entrypoint>然后将此脚本设置为默认值:Run Code Online (Sandbox Code Playgroud)<container> <!-- Assumes you have /bin/sh as specified at the top of /my-entrypoint.sh. --> <entrypoint>/my-entrypoint.sh</entrypoint> </container> <!-- You also need to make the script executable. --> <extraDirectories> <permissions> <permission> <file>/my-entrypoint.sh</file> <mode>755</mode> </permission> </permissions> </extraDirectories>或者,如果您按
/bin/sh如下方式调用,则无需配置<extraDirectories>即可使文件可执行。这可能看起来不符合惯例;您通常会使脚本可执行并直接运行它。但这是完全有效的,并且在实际执行方面没有任何区别(只要 的 shebang/entrypoint.sh相同#!/bin/sh)。Run Code Online (Sandbox Code Playgroud)<container> <entrypoint> <arg>/bin/sh</arg> <arg>/my-entrypoint.sh</arg> </entrypoint> </container>也可以在不创建脚本的情况下执行此操作(基本上将整个脚本嵌入
pom.xml并将其传递给 shell 程序)。在这种情况下,您不需要配置<extraDirectories>。Run Code Online (Sandbox Code Playgroud)<container> <entrypoint> <arg>/bin/sh</arg> <arg>-c</arg> <arg>exec java $JAVA_OPTS -cp $( cat /app/jib-classpath-file ) $( cat /app/jib-main-class-file )</arg> </entrypoint> </container>
| 归档时间: |
|
| 查看次数: |
6344 次 |
| 最近记录: |