gpt4 book ai didi

jib - Google Jib - 是否可以使用 CMD/ENTRYPOINT 在容器启动时运行 linux 命令?

转载 作者:行者123 更新时间:2023-12-02 18:32:31 28 4
gpt4 key购买 nike

是否可以在容器使用 cmd 或入口点或 jib maven 插件中的任何其他机制从 jib 图像启动时运行 linux 命令,然后启动 java 进程?

在我的例子中,我想运行这个命令:

echo "127.0.0.1 $HOSTNAME" >> /etc/hosts

最佳答案

你总是可以set a custom entrypoint using <container><entrypoint> .您可以启动 shell 脚本,运行不同的程序等。有时,您可能想使用 <extraDirectories>复制脚本的功能(并为其授予可执行权限)。

参见 here有关运行 shell 脚本的一些想法:

Another option is to define your own <entrypoint> to use a shell.(Therefore, you need a base image that includes a shell binary (suchas /bin/bash). Note that the default base image prior to Jib 3.0 wasDistroless anddid not include a shell program. OTOH, Jib 3.0+ doesn't useDistroless.)In this method, you'll need to know the right Java runtime classpathand the main class to use in your JVM launch command. To help this,starting with Jib >= 3.1, Jib creates two JVM argumentfilesinside a built image; they will hold, respectively, the classpath andthe main class inside a built image.

Knowing the entrypoint, you can write a shell script(my-entrypoint.sh):

#!/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 )

Alternatively, if you are on Java 9+, you can leverage the @-argumentfile:

exec java $JAVA_OPTS -cp @/app/jib-classpath-file @/app/jib-main-class-file

Place my-entrypoint.sh under <project root>/src/main/jib. This isthe default directory for Jib's <extraDirectories> feature, and Jibwill place src/main/jib/my-entrypoint.sh at the root directory inthe container image. Then set the default <entrypoint> to thisscript:

<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>

Alternatively, if you invoke /bin/sh as below, you don't have toconfigure <extraDirectories> to make the file executable. This maynot look customary; you would normally make the script executable andrun it directly. But this is perfectly valid, and there is nodifference in terms of actual execution (as long as the shebang of/entrypoint.sh is the same #!/bin/sh).

<container>
<entrypoint>
<arg>/bin/sh</arg>
<arg>/my-entrypoint.sh</arg>
</entrypoint>
</container>

It's also possible to do this without creating a script (basicallyembedding the entire script in pom.xml and passing it to a shellprogram). In this case, you don't need to configure<extraDirectories>.

          <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>

关于jib - Google Jib - 是否可以使用 CMD/ENTRYPOINT 在容器启动时运行 linux 命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69254438/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com