Passing env variables to DOCKER Spring Boot - spring

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" ]

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

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*>

Related

How to pass application.properties to SpringBoot app in Docker container?

Assume we have a simple Dockerfile where my_sb_app.jar is a SpringBoot application:
FROM bellsoft/liberica-openjdk-alpine:11.0.9-12
WORKDIR /app
COPY target/my_sb_app.jar /app
EXPOSE 8080
ENTRYPOINT java -jar my_sb_app.jar
I would like to pass custom app.properties on each run
docker run my_image_name /somewhere/on/my/host/app.properties
so the expected final command (inside the container) could be identical to the following
java -jar my_sb_app.jar --spring.config.location=%%file_with_contents_of_app.properties%%
What is the standard way of passing a file into a Docker container?
How the Docker image sould be changed?
The standard way to pass files from host to the container is with volumes:
https://docs.docker.com/storage/bind-mounts/
https://docs.docker.com/storage/volumes/
In your example:
docker run -v /somewhere/on/my/host/app.properties:/opt/app.properties my_image_name
java -jar my_sb_app.jar --spring.config.location=/opt/app.properties

unable to run docker container as getting this error:Unable to access jarfile app.jar

I have created the image using below Docker file.Its working fine in local when I run it in the container using this image[docker run -p 80:8080 username/spring-boot-docker-aws-demo:tag
FROM openjdk:8-jdk-alpine
COPY target/spring-boot-docker-aws-demo.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","app.jar"]
I have pushed this image to Docker hub to run it in EC2 instance.when I run it its saying "Error: Unable to access jarfile app.jar"
https://hub.docker.com/layers/chaituu/spring-boot-docker-aws-demo/docker-aws-demo-tag/images/sha256-110363a016eb2250264d96c8890dede518d8e519fdc9a27174334fe1096a540e?context=repo
what could be the issue?
I had a similar issue today (although in my case it wouldn't even run locally). Here's what your Dockerfile would look like with my fix:
FROM openjdk:8-jdk-alpine
COPY target/spring-boot-docker-aws-demo.jar /app/app.jar
EXPOSE 8080
ENTRYPOINT ["sh"]
CMD ["-c", "java -jar /app/app.jar"]

Docker: Change port number during runtime

FROM openjdk:8-jre-alpine
COPY --from=builder tmp/target/Application*.jar app.jar
RUN mkdir -p /app
ARG specified_port=8090
ENV EXPOSED_PORT=$specified_port
EXPOSE $EXPOSED_PORT
ENTRYPOINT ["java", "-jar", "-Dspring.profiles.active=prod", "app.jar"]
And during the runtime how can i change the ports during docker run
server.port to 8092
sample docker run
docker run --net="host" -p 8092:8092 -p 9992:9992 4794973497437
Using Environment Variables
Add this in dockerfile
ENV PORT 8090
then run
docker run -d image_name -e "PORT=8092"
or Another way is
ARG PORT
and use it like
CMD [ $PORT]
and then run your docker container like this
export PORT=8092; docker run -d image_name"

Docker run with -e not working.Failed to pass spring.profiles.active in the run command

I am facing one issue with my docker file.Image build was successful but while running I am getting an error because the active profile I am setting in the run command is not reflecting.
# Get java
FROM openjdk:8-jdk-alpine
VOLUME /tmp
ARG JAR
COPY ${JAR} app.jar
EXPOSE 8080
ENV severn_js_key=1234qasw
ENTRYPOINT ["java", "-jar", "app.jar"]
My run command is like
sudo docker run -e SPRING_PROFILES_ACTIVE=dev -p 8088:80 -t tws-apps/service:1.0.0-SNAPSHOT
I am getting a null pointer exception in the server log while executing this statement
String environment = System.getProperty("spring.profiles.active");
switch (environment) {
Please help
You pass the SPRING_PROFILES_ACTIVE to the docker container as a system environment variable. You should pass it as a Java System Property instead. A solution would be to run the container by overriding the entrypoint:
docker run --entrypoint java -t tws-apps/service:1.0.0-SNAPSHOT -Dspring.profiles.active=dev -jar app.jar
In alternative, in your Dockerfile change the entrypoint. It could be a script that reads the SPRING_PROFILES_ACTIVE environment variable and then runs Java with the var as a system property.
Hope it helps.

How to measure Docker startup time

I'm buiding a docker image from a jar file ( Spring Boot application), but I could not use the command time docker run my-image to check the startup time of the container. Because of the output from Spring Boot, the docker run command will not stop.
The Dockerfile:
FROM openjdk:8-jre-alpine
VOLUME /tmp
ADD target/myApp-0.0.1-SNAPSHOT.jar app.jar
RUN sh -c 'touch /app.jar'
ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/app.jar" ]
Is there other way to measure the startup time of the container?
Thanks in advance!

Resources