Andrej Urvantsev
Andrej Urvantsev

Reputation: 461

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

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?

Upvotes: 1

Views: 1910

Answers (1)

jonashackt
jonashackt

Reputation: 14429

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

Upvotes: 1

Related Questions