How to include extern env.properties? - spring-boot

Here is my Dockerfile:
FROM openjdk:11-jre
COPY build/libs/project-lion-api-0.0.1-SNAPSHOT.jar app.jar
ENTRYPOINT ["java", "-jar", "app.jar"]
My Spring resources directory contains:
../src/../resources in ..
application.yml
application-dev.yml
application-test.yml
env.properties <-- git ignore
This env file contains sensitive information, such as database passwords. So the file is not uploaded to Git.
However, the server does not work because the env.properties file was not found after docker deploy. How do I include env.properties?

Related

H2 CSVRead gives FileNotFoundException

I have a Spring Boot application that uses JPA with H2. data.sql has the following content:
INSERT INTO artists
SELECT *
FROM CSVREAD('/usr/local/data/artists.csv’);
and that path works locally (though changed to a local directory). However when dockerized with the following image:
# Build stage
FROM maven:3.8.6-amazoncorretto-17 AS build
COPY src /home/app/src
COPY pom.xml /home/app
RUN mvn -f /home/app/pom.xml clean package -DskipTests
# Package state
FROM amazoncorretto:17-alpine
COPY --from=build /home/app/target/webusengus-0.0.1-SNAPSHOT.jar /usr/local/lib/webusengus.jar
COPY --from=build /home/app/src/main/resources/artists.csv /usr/local/data/artists.csv
EXPOSE 8080
ENTRYPOINT ["java","-jar","/usr/local/lib/webusengus.jar”]
on startup a java.nio.file.NoSuchFileException: /usr/local/data/artists.csv is thrown, but when exploring the image through docker export the file is in /usr/local/data/artists.csv
Given that docker is a superuser, I also don’t see how this could relate to file access?

My spring boot application was running fine but when I dockerized it I got File not Found error

I don't know what I'm getting this error when I dockerized my spring-boot application
this is my Dockerfile
enter image description here
First, I wonder is your application working on your IDE?
Second, I think you make sure to build and package.
It is not sure to exists a jar file in your target folder.
You always have to build and check by yourself.
Make the build process automatic.
How about using this Dockerfile?
FROM maven:3.8.6-openjdk-18-slim as MAVEN_BUILD
WORKDIR /build
COPY pom.xml .
RUN mvn dependency:go-offline
COPY src ./src
RUN mvn package -Dmaven.test.skip=true
FROM openjdk:18-alpine
WORKDIR /app
ARG JAR_FILE=*.jar
COPY --from=MAVEN_BUILD /build/target/${JAR_FILE} ./app.jar
EXPOSE 8080
CMD ["java", "-jar", "app.jar"]
If maven or openjdk version not matched, check this site.
https://hub.docker.com/_/maven
https://hub.docker.com/_/openjdk
In your Spring Boot application, from what I can see your BookingController is missing. Make sure you have the file present and build your image accordingly.
For your Dockerfile, try to change add to ADD, as it seems your target files are not been copied into the image built.

Creating Docker image with springboot layertools - does not find profile specific application properties

I am following the tutorial here and here on how to create a layered Docker image from my springboot backend. I end up with the following Dockerfile:
FROM openjdk:8-jre-slim as builder
WORKDIR application
ARG JAR_FILE=target/*-exec.jar
COPY ${JAR_FILE} application.jar
RUN java -Djarmode=layertools -jar application.jar extract
FROM openjdk:8-jre-slim
WORKDIR application
COPY --from=builder application/dependencies/ ./
COPY --from=builder application/spring-boot-loader/ ./
COPY --from=builder application/snapshot-dependencies/ ./
COPY --from=builder application/application/ ./
ENTRYPOINT ["java", "org.springframework.boot.loader.JarLauncher"]
The problem is that when I run this in my docker-compose with a Spring profile TST, that it does not find application-tst.properties. I can see from the logs that Spring profile TST is active on startup, yet it only loads the properties from application.properties.
As a sanity check I copied the properties from application-tst.properties over to application.properties and rebuilt my image, which then worked fine (it connects to the database container etc).
I extracted the contents of my executable jar (which is the jar from line 3 in the Dockerfile) and can confirm that application-tst.properties is present there. Anyone know what the issue is?
I found the answer and it's a really dumb one... The Spring profile is case sensitive, so
SPRING_PROFILES_ACTIVE=TST
should become
SPRING_PROFILES_ACTIVE=tst

Multi-Stage Spring Boot Dockerization Using Maven

Trying to get the multi-stage Spring Boot application Dockerfile that works.
The idea is to:
Build and package the project using mvn package command
Run the built .jar file
After some research, I found this article. It provides complete Dockerfile, but it does not work for me.
I modified the initial Dockerfile, and now it looks like this:
FROM maven:3.6.2-jdk-8-slim AS MAVEN_BUILD
COPY pom.xml /build/
COPY src /build/src/
WORKDIR /build/
RUN mvn -Dmaven.test.skip=true package -Ptest # This line does not work properly
FROM openjdk:8-jre
WORKDIR /app
COPY --from=MAVEN_BUILD /build/target/platform-0.0.1.jar /app/
ENTRYPOINT ["java", "-jar", "platform-0.0.1.jar"]
I created a docker-compose.yml that tries to build this Dockerfile:
[...]
api:
build:
context: .
dockerfile: Dockerfile
depends_on:
- mysql-db
ports:
- "8080:8085"
[...]
After running the docker-compose up --build -d command, I always get this error:
Am I missing something?
Running the mvn -Dmaven.test.skip=true package -Ptest command in the actual project folder works normally...
The problem was with my docker client installation. It somehow did not setup necessary permissions after installation. On other machines, this Dockerfile works fine.
Well, this was a lesson for some future times.

Packaging war file into docker image in multiple layers

I'm trying to put a Spring Boot application with war packaging into a docker image. The simplest way to get this can be using the following Dockerfile:
FROM adoptopenjdk/openjdk8:alpine-slim
VOLUME /tmp
COPY target/demo.war app.war
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.war"]
Following this approach I'm creating a big layer every time I change the project's source code. An alternative to this is to split the war into different docker layers as it is explained in this post: https://spring.io/blog/2018/11/08/spring-boot-in-a-container#a-better-dockerfile
However, I'm afraid this is only possible with jar packaging and not with war. Am I right?
Thanks in advance.
This is the way I get a multilayer docker image with a war applicaton.
First open the war file to find out which folders have to be copied to the image:
jar -xf youapp.war
In my project the war file is composed of these folders:
/WEB-INF
/META-INF
/resources
/org
Based on this, I created the following Dockerfile:
FROM openjdk:8-jdk-alpine AS builder
WORKDIR target/dependency
ARG APPWAR=target/*.war
COPY ${APPWAR} app.war
RUN jar -xf ./app.war
RUN mv WEB-INF/lib-provided lib-provided
RUN mv WEB-INF/lib lib
FROM openjdk:8-jre-alpine
VOLUME /tmp
ARG DEPENDENCY=target/dependency
COPY --from=builder ${DEPENDENCY}/lib /app/WEB-INF/lib
COPY --from=builder ${DEPENDENCY}/lib-provided /app/WEB-INF/lib-provided
COPY --from=builder ${DEPENDENCY}/org /app/org
COPY --from=builder ${DEPENDENCY}/resources /app/resources
COPY --from=builder ${DEPENDENCY}/META-INF /app/META-INF
COPY --from=builder ${DEPENDENCY}/WEB-INF /app/WEB-INF
ENTRYPOINT ["java","-cp","/app/WEB-INF/classes:/app/WEB-INF/lib/*:/app/WEB-INF/lib-provided/*","com.company.project.Application"]
I hope it is useful.
Try using the Tomcat Docker image to deploy your warfile. For example:
FROM tomcat:latest
COPY target/demo.war /usr/local/tomcat/webapps/
Reference:
https://hub.docker.com/_/tomcat

Resources