Java -jar is not running the tests compiled in the container - java

I can't run the tests in maven install because the database service isn't up to date, so I want to run the test when I run the .jar but it's not working
#
# Build stage
#
FROM maven:3.8.3-openjdk-16 AS build
WORKDIR /app
COPY src /app/src
COPY pom.xml /app
RUN mvn -f /app/pom.xml clean package -DskipTests=true
RUN mvn test-compile
#
# Package stage
#
FROM openjdk:16
COPY --from=build /app/target/bank-api.jar /usr/local/lib/bank-api.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","/usr/local/lib/bank-api.jar"]

Related

Build maven project inside docker container

I'm having some problems while trying to build a mvn project inside a Docker container. Everytime I try to build using the following Dockerfile, I get this error:
[10/10] COPY target/*.jar app.jar:
------
lstat /var/lib/docker/tmp/buildkit-mount9100795/target: no such file or directory
My current Dockerfile:
FROM maven:3.8.1-openjdk-17 as builder
COPY src /tmp/src/
COPY pom.xml /tmp/
WORKDIR /tmp/
RUN mvn clean install
FROM maven:3.8.1-openjdk-17
WORKDIR /tmp/app
COPY target/*.jar app.jar
EXPOSE 8081
ENTRYPOINT ["java","-jar","app.jar"]
I've found the correct configurations for the dockerfile and relative paths. Since I've set the WORKDIR for the buider as /tmp/ my target folder was being generated at /tmp/target/, so specifying the source files with the ---from=builder tag, and updating the path, I was able to succesfully copy the .jar file
My current working dockerfile:
FROM maven:3.8.1-openjdk-17 as builder
COPY src /tmp/src/
COPY pom.xml /tmp/
WORKDIR /tmp/
RUN mvn clean install
FROM maven:3.8.1-openjdk-17
COPY --from=builder /tmp/target/*.jar app.jar
EXPOSE 8081
ENTRYPOINT ["java","-jar","app.jar"]

How to do a Gradle composite build in Docker?

I am able to do a Gradle composite build locally.
Now I'm trying to build the docker image in a docker container instead of on my local laptop. And I got this error:
Included build '/home/yang-liu/eCommerceServiceDataAccessLayer' does not exist.
So it complains that above package doesn't exist.
I'm not sure how to build docker image inside docker container when multiple packages are involved. My two packages are in different GitHub repositories.
More Context
Project structure
settings.gradle files
settings.gradle for eCommerceService:
rootProject.name = 'eCommerceService'
includeBuild("../eCommerceServiceDataAccessLayer")
settings.gradle for eCommerceServiceDataAccessLayer:
rootProject.name = 'eCommerceServiceDataAccessLayer'
Dockerfile in eCommerceService
########################################################################################
# We are multi-stage builds here to build the docker image.
# https://docs.docker.com/develop/develop-images/multistage-build/
########################################################################################
########################################################################################
# Build Stage
########################################################################################
FROM openjdk:11 AS BUILD_ARTIFACT
RUN ls
ARG APP_NAME=ecommerce-service
ARG USER_NAME=yang-liu
ENV APP_HOME=/home/$USER_NAME/$APP_NAME
WORKDIR $APP_HOME
COPY build.gradle settings.gradle gradlew $APP_HOME
COPY gradle $APP_HOME/gradle
# https://stackoverflow.com/questions/25873971/docker-cache-gradle-dependencies
RUN ./gradlew build || return 0
COPY . .
RUN ./gradlew build
########################################################################################
# Run Stage
########################################################################################
FROM openjdk:11-jre AS RUN_ARTIFACT
# Run as a non-root user to mitigate security risks
# https://security.stackexchange.com/questions/106860/can-a-root-user-inside-a-docker-lxc-break-the-security-of-the-whole-system
ARG GROUP_NAME=ecommerce-service
ARG USER_NAME=yang-liu
ARG APP_NAME=ecommerce-service
ARG USER_NAME=yang-liu
ENV APP_HOME=/home/$USER_NAME/$APP_NAME
RUN addgroup $GROUP_NAME
ENV APP_HOME=/home/$USER_NAME
WORKDIR $APP_HOME
RUN adduser --ingroup $GROUP_NAME $USER_NAME --home $APP_HOME
USER $USER_NAME
# Copy the artifact from BUILD_ARTIFACT stage
COPY --from=BUILD_ARTIFACT $APP_HOME/build/libs/eCommerceService-0.0.1.jar eCommerceService-0.0.1.jar
# Set ENTRYPOINT in exec form to run the container as an executable
ENTRYPOINT ["java","-classpath", "-jar","eCommerceService-0.0.1.jar"]

Strange Liquibase MySQL connection issue from docker

I'm trying to dockerize a Spring project of mine that has the Maven Liquibase plugin and I am facing a strange issue.
Given this Dockerfile for Spring:
FROM maven:3.6.3-jdk-11-slim AS build
WORKDIR /app
COPY pom.xml .
COPY src ./src
COPY mvnw ./
COPY mvnw.cmd ./
ARG MAVEN_OPTS="-Xmx2G -XX:MaxPermSize=1G"
ENV MAVEN_OPTS="${MAVEN_OPTS}"
RUN mvn clean package
RUN mvn liquibase:update
ENTRYPOINT ["java","-Xmx1.5G","-XX:MaxPermSize=1G", "-jar","target/api-0.0.1-SNAPSHOT.jar"]
Liquibase throws the following error:
liquibase.exception.DatabaseException: liquibase.exception.DatabaseException: liquibase.exception.DatabaseException: Connection could not be created to jdbc:mysql://mysqldb:3306/dockerapi?autoReconnect=true&serverTimezone=Europe/Bucharest&useSSL=false&createDatabaseIfNotExist=true with driver com.mysql.cj.jdbc.Driver. Could not create connection to database server. Attempted reconnect 3 times. Giving up.
If I use this Dockefile instead:
FROM maven:3.6.3-jdk-11-slim AS build
WORKDIR /app
COPY pom.xml .
COPY src ./src
COPY mvnw ./
COPY mvnw.cmd ./
ARG MAVEN_OPTS="-Xmx2G -XX:MaxPermSize=1G"
ENV MAVEN_OPTS="${MAVEN_OPTS}"
RUN mvn clean package
ENTRYPOINT ["tail", "-f", "/dev/null"]
to start the container, then exec into the container:
If I ping mysqldb, the result is successful:
2 packets transmitted, 2 received, 0% packet loss, time 41ms
If I run mvn:liquibase:update directly, I get the same error as above:
liquibase.exception.DatabaseException: liquibase.exception.DatabaseException: liquibase.exception.DatabaseException: Connection could not be created to jdbc:mysql://mysqldb:3306/dockerapi?autoReconnect=true&serverTimezone=Europe/Bucharest&useSSL=false&createDatabaseIfNotExist=true with driver com.mysql.cj.jdbc.Driver. Could not create connection to database server. Attempted reconnect 3 times. Giving up.
However, if I first run the app (java -jar target/api-0.0.1-SNAPSHOT.jar) or run mvn test and only then run mvn liquibase:update, the liquibase command will be successful.
It feels like there is something that Liquibase fails to initialise itself and the Spring app can initialise.
I have tried adding mvn test to the Dockerfile, however Liquibase throws the same error.
FROM maven:3.6.3-jdk-11-slim AS build
WORKDIR /app
COPY pom.xml .
COPY src ./src
COPY mvnw ./
COPY mvnw.cmd ./
ARG MAVEN_OPTS="-Xmx2G -XX:MaxPermSize=1G"
ENV MAVEN_OPTS="${MAVEN_OPTS}"
RUN mvn clean package
RUN mvn test
RUN mvn liquibase:update
ENTRYPOINT ["java","-Xmx1.5G","-XX:MaxPermSize=1G", "-jar","target/api-0.0.1-SNAPSHOT.jar"]

Unable to run 'RUN ./mvnw dependency:go-offline -B' when building docker image from "openjdk:8-jdk-alpine" for Spring Boot app

So I am trying to run a spring boot app with maven wrapper inside the docker container. Here is my Docker file:
### Stage 1: Build the application
FROM openjdk:8-jdk-alpine as build
RUN mkdir -p /app
#Set the current working directory inside the image
WORKDIR /app
#copy maven executable to the image
COPY mvnw .
COPY .mvn .mvn
#Copy the pom.xml file
COPY pom.xml .
#Build all the dependencies in preparation to go offline
#This is a separate step so the dependencies will be cached unless
#the pom.xml file has changed
RUN ./mvnw dependency:go-offline -B
#Copy the project source
COPY src src
#Package the application
RUN ./mvnw package -DskipTests
RUN mkdir -p target/dependency && (cd target/dependency; jar -xf ../*.jar)
I have this error:
Step 7/16 : RUN ./mvnw dependency:go-offline -B
---> Running in 642a32f86392
/bin/sh: ./mvnw: not found
ERROR: Service 'app-server' failed to build: The command '/bin/sh -c ./mvnw dependency:go-offline -B' returned a non-zero code: 127
I am working with windows 10 pro. Please I need your help
Maybe a duplicate of Unable to run './mvnw clean install' when building docker image based on "openjdk:8-jdk-alpine" for Spring Boot app
Can you check the line endings of the mvnw shell script?
You could fix it by adding this before executing the mvnw command:
RUN dos2unix mvnw
Alternatively, if the file is in git, you can also fix it by adding the following to a .gitattributes file and checking the file out again:
*.bat text eol=crlf
mvnw text eol=lf
You have to copy the project files into the /app dir first. And you don't have the maven wrapper in the context folder where you run the docker build.
Try change the end of line mvnw file from Windows style CRLF to Unix style LF. Then rebuild the image.

multi-module Maven project on Dockers

I have a multi-module maven project where the single modules are all runnable microservice applications containing their own Dockerfile, so in production every module will be a containerized application.
The parent project, which contains the child-modules only contains the parent pom.xml and the docker-compose.yml
I have tried to use the following Dockerfile (on sub-module level):
FROM sgrio/java-oracle
RUN apt-get update
RUN apt-get install -y maven
COPY ../pom.xml /usr/local/service/Oogaday/pom.xml
COPY pom.xml /usr/local/service/Oogaday/OogadayApi/pom.xml
COPY src /usr/local/service/Oogaday/OogadayApi/src
WORKDIR /usr/local/service/Oogaday/OogadayApi/
RUN mvn package -DskipTests
CMD ["java","-jar","org.oogaday.api-1.0-SNAPSHOT-jar-with-dependencies.jar"]
But I am getting a security error because I am trying to copy the parent pom.xml file (which is not placed in the directory from which I am running the build).
So is there a way to build a maven based sub-module with parent pom?
This is my suggestion, you should take advantage the more you can of docker cache.
Assume this multi-module project pom layout:
+-- my-project
+-- module1
| `-- pom.xml
+-- module2
| `-- pom.xml
`- pom.xml
Dockerfile:
# cache as most as possible in this multistage dockerfile.
FROM maven:3.6-alpine as DEPS
WORKDIR /opt/app
COPY module1/pom.xml module1/pom.xml
COPY module2/pom.xml module2/pom.xml
# you get the idea:
# COPY moduleN/pom.xml moduleN/pom.xml
COPY pom.xml .
RUN mvn -B -e -C org.apache.maven.plugins:maven-dependency-plugin:3.1.2:go-offline
# if you have modules that depends each other, you may use -DexcludeArtifactIds as follows
# RUN mvn -B -e -C org.apache.maven.plugins:maven-dependency-plugin:3.1.2:go-offline -DexcludeArtifactIds=module1
# Copy the dependencies from the DEPS stage with the advantage
# of using docker layer caches. If something goes wrong from this
# line on, all dependencies from DEPS were already downloaded and
# stored in docker's layers.
FROM maven:3.6-alpine as BUILDER
WORKDIR /opt/app
COPY --from=deps /root/.m2 /root/.m2
COPY --from=deps /opt/app/ /opt/app
COPY module1/src /opt/app/module1/src
COPY module2/src /opt/app/module2/src
# use -o (--offline) if you didn't need to exclude artifacts.
# if you have excluded artifacts, then remove -o flag
RUN mvn -B -e -o clean install -DskipTests=true
# At this point, BUILDER stage should have your .jar or whatever in some path
FROM openjdk:8-alpine
WORKDIR /opt/app
COPY --from=builder /opt/app/<path-to-target>/my-1.0.0.jar .
EXPOSE 8080
CMD [ "java", "-jar", "/opt/app/my-1.0.0.jar" ]

Categories

Resources