2

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 Answers1

0

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}"]
Eranga Heshan
  • 5,133
  • 4
  • 25
  • 48
  • my starting point is "docker build ...." , so can you tell how to. Example dockerfile 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) FROM openjdk:8-jdk-alpine ARG DEPENDENCY=/workspace/app/build/dependency 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"] –  Jul 04 '21 at 08:16
  • If you need to use only `docker build` and you have multi-stage builds, you need to define `ARG` in global scope before any `FROM` commands. And in the stage that you want to use the value, again define `ARG`. The rest is similar to my answer. – Eranga Heshan Jul 04 '21 at 08:36
  • 1
    I updated my answer with the Dockerfile content you sent in the comment. I tried to add the 3 environment variables you had in the original question. Hope this helps. – Eranga Heshan Jul 04 '21 at 08:42
  • I'm accepting this as answer as it was very helpful (though I think there could be my fault with commands too) –  Jul 04 '21 at 10:59