3

The IF condition in Dockerfile works only for RUN command.

In Dockerfile I have this (shortened version):

ARG MY_ENV
ARG MY_HOST
ENV JAVA_OPTIONS "-Dspring.profiles.active=${MY_ENV}"

What I want to achieve, IF MY_ENV is local, then define:

ENV JAVA_OPTIONS "-Dspring.profiles.active=${MY_ENV} -DMY_HOST=${MY_HOST}"

else leave as it was ENV JAVA_OPTIONS "-Dspring.profiles.active=${MY_ENV}"

The problem is the if/else works only with RUN, as I tried it like:

RUN if [ "$MY_ENV" = "local" ]; then ...

but this did not work.

balias
  • 499
  • 1
  • 4
  • 17
Darksymphony
  • 2,155
  • 30
  • 54
  • 3
    Could you do it in an [entrypoint](https://docs.docker.com/develop/develop-images/dockerfile_best-practices/#entrypoint) script? Does it make sense for your use-case? – Stefan Golubović Oct 04 '21 at 09:23
  • Also consider whether `ARG` actually makes sense here; you wouldn't normally want to recompile your image/application just because you're running it in a different context. You should be able to pass `-e SPRING_PROFILES_ACTIVE=...` when you run the application to set this variable. – David Maze Oct 04 '21 at 10:45
  • Ok, I solved it by defining another ARG in Jenkins, and in Dockerfile setting it as empty by default: ARG MY_NEW_ARG=''. Then just adding to the existing: ENV JAVA_OPTIONS "-Dspring.profiles.active=${MY_ENV} ${MY_NEW_ARG}". In this case if it is not defined, it does nothing and if it is defined, then it replaces the ${MY_NEW_ARG} – Darksymphony Oct 04 '21 at 10:47

0 Answers0