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

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

Related

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"

No such file while indicating application properties using Dockerfile for a Spring Boot jar

I'm using Docker Compose to create a container for a Spring Boot application.
I'm receiving the following errors when I do docker-compose up:
Recreating backend_springboot ... error
ERROR: for backend_springboot Cannot start service service: OCI
runtime create failed: container_linux.go:346: starting container
process caused "exec: \"sh -c java
-Dspring.config.location=/application.properties -Djava.security.egd=file:/dev/./urandom $JAVA_OPTS -jar /app.jar\": stat sh -c java -Dspring.config.location=/application.properties
-Djava.security.egd=file:/dev/./urandom $JAVA_OPTS -jar /app.jar: no such file or directory": unknown
ERROR: for service Cannot start service service: OCI runtime create
failed: container_linux.go:346: starting container process caused
"exec: \"sh -c java -Dspring.config.location=/application.properties
-Djava.security.egd=file:/dev/./urandom $JAVA_OPTS -jar /app.jar\": stat sh -c java -Dspring.config.location=/application.properties
-Djava.security.egd=file:/dev/./urandom $JAVA_OPTS -jar /app.jar: no such file or directory": unknown
My structure:
├── docker-compose.yml
├── spring-boot
│   ├── Dockerfile
│   ├── application.properties
│   └── backofficeservices-0.0.1.jar
docker-compose.yml
version: '3'
services:
service:
container_name: backend_springboot
build: ./spring-boot
ports:
- "80:8080"
restart: always
spring-boot/Dockerfile
FROM openjdk:8-jre-alpine as gradle
COPY backofficeservices-0.0.1.jar /app.jar
COPY application.properties /application.properties
ENV JAVA_OPTS=""
ENTRYPOINT ["sh -c java -Dspring.config.location=/application.properties -Djava.security.egd=file:/dev/./urandom $JAVA_OPTS -jar /app.jar"]
EXPOSE 8080
As far as I know, the application.properties is not found. I will appreciate any help to detect my error.
My goal is to run my Spring Boot app applying the application.properties I have there.
You have specified an ENTRYPOINT using a single "word". When you launch the container, it is trying to run that as a single "word" – it is looking for a binary named sh -c java ..., with spaces and all as part of the filename. If your command has multiple "words" and you're using the JSON-array form, you have to correctly manually split it up into words yourself.
ENTRYPOINT ["sh", "-c", "java -Dspring.config.location=..."]
A Dockerfile CMD and ENTRYPOINT are fairly similar; both provide part of the command the container ultimately runs. If you only need one of them I'd recommend using CMD instead, for two reasons: it makes it easier to get a debugging shell on the built image docker run --rm -it myimage /bin/sh, and there's a fairly standard pattern of using an ENTRYPOINT script to do some initial setup and then exec "$#" to run the CMD.
CMD ["sh", "-c", "java -Dspring.config.location=..."]
If you have this form, Docker can provide the sh -c ... wrapper for you.
CMD java -Dspring.config.location=...
With a little bit more cleanup, that would leave the final Dockerfile something like
FROM openjdk:8-jre-alpine
COPY backofficeservices-0.0.1.jar /app.jar
COPY application.properties /application.properties
ENV JAVA_OPTS=""
CMD java \
-Dspring.config.location=/application.properties \
-Djava.security.egd=file:/dev/./urandom \
$JAVA_OPTS \
-jar /app.jar
EXPOSE 8080

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.

Resources