13
votes

I have a SpringBoot application and its Dockerfile is as follows. I have application.properties for different environments like local/dev/qa/prod. When I run the application locally in IDE, I pass -Dspring.profiles.active=local in VM options so that it loads the application-local.properties. For running as docker containers, I build an image which comprises of all the application.properties. i.e. it's only SAME docker image for all the environments.

When I run the image in an environment, I want to somehow make the SpringBoot to understand that its dev env, so it has to load application-dev.properties. I am using AWS ECS for managing the containers.

FROM openjdk:8-jdk-alpine
VOLUME /tmp
ADD target/sample-test-sb-sample-app-1.0-exec.jar app.jar
EXPOSE 8080
ENV JAVA_OPTS=""
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ]
2

2 Answers

11
votes

The easiest (and probably the best way) to do it via environment variable in a docker container:

SPRING_PROFILES_ACTIVE=dev,swagger

UPDATE:

In order to set environment variables to docker, you do not need to modify Dockerfile. Just build your docker image and then run it with the env variables set:

docker run your-docker-container -e SPRING_PROFILES_ACTIVE='dev,swagger' -p 8080:8080
8
votes

In the .Dockerfile file:

ENTRYPOINT [ "sh", "-c", "java -Dspring.profiles.active=**${ENV}** -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ]

And while running the docker:

docker run --env ENV=*local* -d -p 8080:8080 <*image id*>

This way, the environment variable gets local as value and passes to Dockerfile when we bring up a container.

Update

You can also do like

ENTRYPOINT ["java","-jar", "-Dspring.profiles.active=${ENV} -Djava.security.egd=file:/dev/./urandom","app.jar"]

and while docker image

docker run --env ENV=local -d -p 8080:8080 <*image id*>