How to pass flags to java process in Docker contatiner built by Cloud Native Buildpacks (CNB)

五迷三道 提交于 2021-02-11 07:23:56

问题


Normally when I create Docker image I do something like this:

FROM openjdk:15-jdk-slim

ARG version=undefined
LABEL version=${version}

WORKDIR /
COPY build/libs/v2t-api-$version.jar /v2t-api.jar

USER nobody

VOLUME ["/tmp"]

EXPOSE 8080
EXPOSE 8081

ENTRYPOINT ["sh", "-c", "exec java ${JAVA_OPTS} -jar v2t-api.jar"]

So, when I start my container in for example kubernetes I can set JAVA_OPTS which defines how many memory is available for heap or how many CPUs are actually there.

Packaging OCI Images using built-in gradle plugin from Spring Boot looks interesting, but I can't find how to do something similar in buildpacks - I would like to set some flags to java process, so how do I do that?


回答1:


According to the Paketo.io docs there are 2 kinds of environment variables one could pass to the Paketo build:

  1. Build-time Environment Variables
  2. Runtime Environment Variables

If I understand your question correctly you're looking for a way to configure Runtime Environment variables (2.). In order to do that you need to pass environment variables to the container running your app (which was build by CNB/Paketo before) e.g. by using the --env flag. As the docs state

Users may configure runtime features of the app image by setting environment variables in the app container. The names of variables accepted by buildpack-provided runtime components [..] are prefixed with BPL_ or have well-known conventional meanings outside of Paketo (e.g JAVA_TOOL_OPTIONS).

So for example if you build an container image called v2t-api:latest, then you can run your container with:

docker run --env "JAVA_TOOL_OPTIONS=-Xms1024m -Xmx2048m" v2t-api:latest

I can advice you to switch from JAVA_OPTS to JAVA_TOOL_OPTIONS, because your application won't receive the sigterm in case of a graceful shutdown & your app wouldn't be shutdown correctly (see this so answer for more details).



来源:https://stackoverflow.com/questions/64964709/how-to-pass-flags-to-java-process-in-docker-contatiner-built-by-cloud-native-bui

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!