0

I have a docker image that have the following Dockerfile content:

FROM openjdk:8

ENV MYHOME /home/docker

ARG JAR_FILE
ENV JAR ${JAR_FILE}

COPY $JAR_FILE ${MYHOME}/$JAR_FILE

WORKDIR ${MYHOME}
ENTRYPOINT ["sh", "-c", "java -jar ${MYHOME}/${JAR} --spring.profiles.active=docker,foo"]

When I try to include this docker image in a docker compose file together with other images, I want to override Spring active profiles.

I tried using the following docker-compose.yml content:

...
my_image:
  image: my_image
  environment:
    SPRING_PROFILES_ACTIVE: 'docker,bar'
...

But when runnig the docker compose with the above configuration and looking into the container log, I found that the active profiles are:

...
The following profiles are active: docker,foo
...

In my case, how can I set the docker,bar profiles using docker compose?

  • 1
    IIRC, command line has highest priority for spring properties; you're probably best off to either change the docker file, or create another image based on it (with a new command line). Alternatively, you can try to give the same parameter again and hope it overrides the existing one: https://stackoverflow.com/questions/37366857/how-to-pass-arguments-to-entrypoint-in-docker-compose-yml – daniu Apr 27 '22 at 11:56

1 Answers1

1

As @daniu says in a comment, the --spring.profiles.active command-line option takes precedence over the SPRING_PROFILES_ACTIVE environment variable. The actual precedence listing is described in [Externalized Configuration] in the Spring Boot documentation.

On the other hand, a docker run -e option will take precedence over an environment variable set in the image. So if you set ENV SPRING_PROFILES_ACTIVE=... in your image, the docker run -e option will work as you expect; do not include the command-line option in your application startup.

ENV SPRING_PROFILES_ACTIVE=docker,foo
CMD ["java", "-jar", "/app/myapp.jar"] # without --spring.profiles.active option
David Maze
  • 130,717
  • 29
  • 175
  • 215