Home > Back-end >  Override Spring active profiles with Docker Compose
Override Spring active profiles with Docker Compose

Time:04-28

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?

CodePudding user response:

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
  • Related