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"]
Related
the java code below allows you to insert values in a java report to then create a pdf file, when I run the code below I have the following error: Classpath resource not found when running as jar, what is it due to and how I solve this problem?
java code:
ClassPathResource reportResource = new ClassPathResource("templates/printcantiere.jasper");
Map<String, Object> reportParameters = new HashMap<>();
JRBeanCollectionDataSource itemsJRBean = new JRBeanCollectionDataSource(tablevalues);
reportParameters.put("ItemDataSource", itemsJRBean);
reportParameters.put("RagioneSociale", c.getRagioneSociale());
reportParameters.put("Commessa", c.getCommessa());
reportParameters.put("NomeCantiere", c.getNomeCantiere());
reportParameters.put("Stato", c.getStatoCantiere());
docker.sh
#!/bin/bash
#Kill process on port
kill -9 $(lsof -ti:8081)
#delete old jar file and regenerate jar file
mvn clean package
rm -rf test/*.jar
mvn clean install
#generate new jar file mvn clean package
mvn eclipse:eclipse
#define docker name
dockername=quote
#Delete all containers and all volumes
#docker system prune -a
#Build of container and image
docker rm $dockername
docker build -t $dockername .
#Run container
docker run --publish 8081:8081 $dockername
Dockefile:
FROM adoptopenjdk/openjdk16
ARG JAR_FILE=target/report-0.0.1-SNAPSHOT.jar
WORKDIR /opt/app
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","app.jar"]
Pom file link
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"]
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.
I have a spring boot application which i have deployed on docker container, everything is working fine, but i want to deploy and run the application in docker container with docker compose.
This is my DockerFile
FROM java:8
VOLUME /tmp
COPY /target/order-0.0.1-SNAPSHOT.jar order.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/order.jar"]
Step 1 :- Created jar with mvn clean package
Step 2 :- docker build -t order
Step 3 :- docker run -it -d -p 8080:8080
Here everything work's fine
But if i don't execute the step 1 and want's to deploy the the application in container with docker-compose.
While trying to execute docker-compose up i am getting exception /target/order-0.0.1-SNAPSHOT.jar not found
So how to execute the mvn package command in docker-compose ?
Is their any other way's to acheive this ?
This is my docker-compose.yml
version: '3'
services:
order:
restart: always
build: ./order
working_dir: /order
volumes:
- /tmp:/logs
expose:
- "8080"
try with this one
Dockerfile
FROM maven:alpine AS build
COPY src /home/app/src
COPY pom.xml /home/app
RUN mvn -f /home/app/pom.xml clean package
FROM openjdk:alpine
COPY --from=build /home/app/target/*.jar /usr/local/lib/demo.jar
ENTRYPOINT ["java","-jar","/usr/local/lib/demo.jar"]
if you get error like Can't execute jar- file: “no main manifest attribute”
replace last command
ENTRYPOINT ["java","-cp","/usr/local/lib/demo.jar","com.packagename.classnamewithoutextension"]
Created basic HelloWorld microservice using Spring Boot (2.1.3), Java 8, Maven.
pom.xml has maven plugin entry like below
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.example.HelloWorldApplication</mainClass>
</configuration>
</plugin>
Dockerfile looks like below
FROM openjdk:8
VOLUME /tmp
ADD target/helloworld.jar helloworld.jar
EXPOSE 8081
ENTRYPOINT ["java","-jar","helloworld.jar"]
Created image on local machine using command
docker build . -t helloworld:v1
Verified by creating container out of it.
Checked in code to docker-hub account and github account.
Logged into Google cloud platform (GCP), created kubernetes cluster, created pipeline(using container builder) by configuring github url where helloworld microservice code resides. There are two options to run build (use Dockerfile or cloudbuild.yaml). I am using Dockerfile to run build.
When build is picked up to run, it fails for this line in Dockerfile
ADD target/helloworld.jar helloworld.jar
Error seen in GCP logs:
ADD failed: stat /var/lib/docker/tmp/docker-builderxxxxxx/target/helloworld.jar: no such file or directory
I tried to replace it with COPY command and still the issue is same.
Note: I tried to go with cloudbuild.yaml
Here is how my cloudbuild.yaml looks:
steps:
# Build the helloworld container image.
- name: 'gcr.io/cloud-builders/docker'
args:
- 'build'
- '-t'
- 'gcr.io/${PROJECT_ID}/helloworld:${TAG_NAME}'
- '.'
This didn't make any difference. Issue remains the same.
Any idea if Springboot Java application has some specific configuration for Dockerfile to be built fine in Google Cloud Platform?
UPDATE - 1
Based on comments tried below steps on local machine:
ran command mvn clean . That cleaned target folder
updated Dockerfile
FROM maven:3.5-jdk-8 AS build
COPY src .
COPY pom.xml .
RUN mvn -f pom.xml clean package
FROM openjdk:8
VOLUME /tmp
COPY --from=build target/helloworld.jar helloworld.jar
EXPOSE 8081
ENTRYPOINT ["java","-jar","helloworld.jar"]
Ran docker build . -t helloworld:v1 command and that created image.
Then run command to start container:
docker run -p 8081:8081 -n helloworld-app -d helloworld:v1
container starts and exits with error in log:
Exception in thread "main" java.lang.ClassNotFoundException: com.example.HelloWorldApplication at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
Looks like a problem with file paths.
Try the following updated Dockerfile, which explicitly sets the working directory. It also uses explicit file paths when copying the jar between images.
FROM maven:3.5-jdk-8-slim AS build
WORKDIR /home/app
COPY src /home/app/src
COPY pom.xml /home/app
RUN mvn clean package
FROM openjdk:8-jre-slim
COPY --from=build /home/app/target/helloworld-0.0.1-SNAPSHOT.jar /usr/local/lib/helloworld.jar
EXPOSE 8081
ENTRYPOINT ["java","-jar","/usr/local/lib/helloworld.jar"]
Additional Notes:
See the related answer for a full example building a spring boot app
I've based the second stage on a JRE image. Reduces the size of the output image.