Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing env variables to DOCKER Spring Boot

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" ]
like image 845
deejo Avatar asked Oct 12 '17 16:10

deejo


People also ask

How do I pass environment variables to docker containers?

With a Command Line Argument The command used to launch Docker containers, docker run , accepts ENV variables as arguments. Simply run it with the -e flag, shorthand for --env , and pass in the key=value pair: sudo docker run -e POSTGRES_USER='postgres' -e POSTGRES_PASSWORD='password' ...

Can I use ENV variable in Dockerfile?

Environment variables can be defined in the Dockerfile. This can simply be done through the use of the ENV instruction. The environment variables defined by the ENV instruction in the Dockerfile can be used in the resulting image and container.

What is difference between CMD and entrypoint?

Differences between CMD & ENTRYPOINT CMD commands are ignored by Daemon when there are parameters stated within the docker run command while ENTRYPOINT instructions are not ignored but instead are appended as command line parameters by treating those as arguments of the command.


2 Answers

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
like image 130
Danylo Zatorsky Avatar answered Oct 25 '22 23:10

Danylo Zatorsky


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*>
like image 23
deejo Avatar answered Oct 25 '22 21:10

deejo