Spring boot - Jasypt - Dockerfile - Build - how to pass environemnt variables

Viewed 515

I am following spring io article on how to use docker for spring boot. So using Dockerfile https://github.com/spring-guides/top-spring-boot-docker/tree/main/demo

Issue: Test failing

Caused by: java.lang.IllegalArgumentException at PropertyPlaceholderHelper.java:178

My application has a few environment variables dependency

jasypt.encryptor.password=${mjasypt.secret}
jasypt.encryptor.algorithm=${mjasypt.algo}

I am running build as below

docker build --build-arg DEPENDENCY=build/dependency -t ....

I tried 3 new env args into dockerfile

ARG CBS=a
ARG JSP=b
ARG JSA=c

and update entry point

ENTRYPOINT ["java","-noverify","-XX:TieredStopAtLevel=1","-cp","app:app/lib/*","-Dspring.main.lazy-initialization=true","-Dv1=${CBS}", "-Dv2=${JSP}", "-Dv3=${JSA}",...

and running command as, but didn't work

docker build --build-arg DEPENDENCY=build/dependency --build-arg CBS=.. --build-arg JSP=..1 --build-arg JSA=P..ES -t ...

Ive added RUN echo ${CBS} in the dockerfile and I can see the value that I passed from docker build (not a), but how can I know if that is inside ENTRY POINT too

CORRECTION: Dockerfile is multistage and also has

RUN --mount=type=cache,target=/root/.gradle ./gradlew clean build

How can I pass values to this command , as it runs tests

1 Answers

ARG in Dockerfile is only available in the build stage.

When you want to run it as a container, ARG values will not be available but ENV will be. This means you can not directly access those values in ENTRYPOINT (also not available in CMD). You can read this similar question for more info.

If you want to pass environment parameters to the container using the build arguments (ARG), the simple solution will be to do something like this:

ARG CBS=a
ARG JSP=b
ARG JSA=c

ENV CBS_ENV=$CBS
ENV JSP_ENV=$JSP
ENV JSA_ENV=$JSA

Then update your ENTRYPOINT

ENTRYPOINT ["java","-noverify","-XX:TieredStopAtLevel=1","-cp","app:app/lib/*","-Dspring.main.lazy-initialization=true","-Dv1=${CBS_ENV}", "-Dv2=${JSP_ENV}", "-Dv3=${JSA_ENV}",...

If your values are only used when the container is running, then you can remove ARG entirely, and pass it as arguments when the container is starting. Read this similar question for more info.

e.g.:

docker run -e CBS='a' -e JSP='b' -e JSA='c' mynicevue

UPDATE:

ARG CBS=a
ARG JSP=b
ARG JSA=c

# First stage
FROM openjdk:8-jdk-alpine AS build

WORKDIR /workspace/app

RUN --mount=type=cache,target=/root/.gradle ./gradlew clean build

RUN mkdir -p build/dependency && (cd build/dependency; jar -xf ../libs/*.jar)

# Second stage
FROM openjdk:8-jdk-alpine

ARG DEPENDENCY=/workspace/app/build/dependency

ARG CBS
ARG JSP
ARG JSA

ENV CBS=$CBS
ENV JSP=$JSP
ENV JSA=$JSA

COPY --from=build ${DEPENDENCY}/BOOT-INF/lib /app/lib

COPY --from=build ${DEPENDENCY}/META-INF /app/META-INF

COPY --from=build ${DEPENDENCY}/BOOT-INF/classes /app 

ENTRYPOINT ["java", "-cp", "app:app/lib/*", "hello.Application", "-Dv1=${CBS}", "-Dv2=${JSP}", "-Dv3=${JSA}"]
Related