Docker: Change port number during runtime - spring-boot

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"

Related

Spring boot cant find application-$profile.properties file: Docker file

Docker file: Spring unable to find profile.properties file
FROM java:8u111-jre
ARG VERSION
ARG PROFILE
RUN mkdir /app
WORKDIR /app
RUN chown 1001:0 -R /app
RUN chmod 777 /app
COPY target/jira2bd-$VERSION.jar app.jar
USER 1001
CMD java $JAVA_OPTS -jar /app.jar --spring.profiles.active=$PROFILE
EXPOSE 8080
How I know Docker ARGs are not allowed in CMD instruction.
The solution is to convert them into the ENV variables. Try something like that
ARG PROFILE
ENV PROFILE ${PROFILE}
CMD java ${JAVA_OPTS} -jar /app.jar --spring.profiles.active=${PROFILE}
Also take a look here Is Docker ARG allowed within CMD instruction

How to add storage using Volume in a Docker container

I have a spring boot application and I want to start it on a docker container. However, I want to persist data, so I do not have to rebuild image to see the changes I made to RestAPI.
My docker file
FROM maven:3.6.1-jdk-8-alpine
RUN mkdir -p /usr/src
WORKDIR /usr/src
COPY . /usr/src/
RUN mvn clean install
WORKDIR /usr/src/target
ENTRYPOINT ["java", "-jar", "SecurityAndDocker-0.0.1-SNAPSHOT.jar"]
EXPOSE 8080:8080
Image build is ok
Running docker run -p 8080:8080 image-name works perfectly fine
How would I attach a volume to this application?
Thank you!
you can mount the volume, while running the container.
docker run -d -p 8080:8080 --name <container-name> --mount source=myvol2,target=/app <image-name>
you can also read the documentation: https://docs.docker.com/storage/volumes/
Feel free to ask in case you found any issue.
If I understand you want the target directory to be extracted out in a volume from your custom image's container.
use VOLUME keyword to specify the directory.
Your dockerfile should go like this.
FROM maven:3.6.1-jdk-8-alpine
RUN mkdir -p /usr/src
WORKDIR /usr/src
COPY . /usr/src/
RUN mvn clean install
VOLUME /usr/src/target
WORKDIR/usr/src/target
ENTRYPOINT ["java", "-jar", "SecurityAndDocker-0.0.1-SNAPSHOT.jar"]
EXPOSE 8080:8080
The WORKDIR instruction sets the working directory for any RUN, CMD, ENTRYPOINT, COPY and ADD instructions that follow it in the Dockerfile. If you want to save the working directory just add VOLUME in your dockerfile.

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!

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

Resources