Java rmi multi-module docker - spring

I'm having a problem with my rmi java application which I wish to dockerize. The problem is that the application is a multi-module application with the following structure
I wish to dockerize the server, however if I try to do that by using docker build -t server .
it gives me the following error:
#12 1.504 [ERROR]
#12 1.504 [ERROR] The project parent_group:server:0.1-SNAPSHOT (/root/pom.xml) has 1 error
#12 1.505 [ERROR] Non-resolvable parent POM for parent_group:server:0.1-SNAPSHOT: Could not find artifact parent_group:parent:pom:0.1-SNAPSHOT and 'parent.relativePath' points at wrong local POM # line 7, co
lumn 11 -> [Help 2]
#12 1.508 org.apache.maven.model.resolution.UnresolvableModelException: Could not find artifact parent_group:parent:pom:0.1-SNAPSHOT
My dockerfile for server:
FROM maven:3.6.3-jdk-11 AS builder
COPY ./src/ /root/src
COPY ./pom.xml /root/
COPY ./checkstyle.xml /root/
WORKDIR /root
RUN mvn -X clean package
RUN java -Djarmode=layertools -jar target/parent-0.1-SNAPSHOT.jar list
RUN java -Djarmode=layertools -jar target/parent-0.1-SNAPSHOT.jar extract
RUN ls -l /root
FROM openjdk:11.0.6-jre
COPY --from=builder /root/dependencies/ ./
COPY --from=builder /root/snapshot-dependencies/ ./
RUN sleep 10
COPY --from=builder /root/spring-boot-loader/ ./
COPY --from=builder /root/application/ ./
ENTRYPOINT ["java", "org.springframework.boot.loader.JarLauncher","-XX:+UseContainerSupport -XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap -XX:MaxRAMFraction=1 -Xms512m -Xmx512m -XX:+UseG1GC -XX:+UseSerialGC -Xss512k -XX:MaxRAM=72m"]

Related

Can't dockerize spring micro-services multi module app

I'm building a application with JAVA 11, maven, spring.
It has a root pom to organize all child modules as seen on:
https://github.com/erickmob/spring-petclinic-microservices
Here's the project structure:
I've tried to create on the root project this docker-compose.yml:
version: '2'
services:
config-server:
build:
context: ./config-server
dockerfile: Dockerfile
image: config-server
container_name: config-server
mem_limit: 512M
ports:
- 8888:8888
And in the config-server this dockerfile:
FROM maven:3.6.3-adoptopenjdk-11 as build
WORKDIR /usr/src/config-server/
COPY . /usr/src/config-server/
RUN mvn clean package
FROM adoptopenjdk/openjdk11:alpine-jre
RUN addgroup -S spring && adduser -S spring -G spring
RUN mkdir -p /files && \
chown -R spring:spring /files
USER spring:spring
VOLUME /files
WORKDIR /app
COPY --from=build /usr/src/config-server/target/config-server-0.0.1-SNAPSHOT.jar /app/app.jar
EXPOSE 8888
ENTRYPOINT ["java", "-jar", "app.jar"]
But it keeps given me this output error:
Building config-server
Step 1/13 : FROM maven:3.6.3-adoptopenjdk-11 as build
---> 739e519745cd
Step 2/13 : WORKDIR /usr/src/config-server/
---> Using cache
---> da21e56bb1f0
Step 3/13 : COPY . /usr/src/config-server/
---> Using cache
---> 08a5585f625f
Step 4/13 : RUN mvn clean package
---> Running in ed76a0ae6b41
[INFO] Scanning for projects...
[ERROR] [ERROR] Some problems were encountered while processing the POMs:
[FATAL] Non-resolvable parent POM for com.erickmob.petclinic:config-server:1.0.0-SNAPSHOT: Could not find artifact com.erickmob.pet-clinic:pet-clinic:pom:1.0-SNAPSHOT and 'parent.relativePath' points at wrong local POM # line 5, column 13
#
[ERROR] The build could not read 1 project -> [Help 1]
[ERROR]
[ERROR] The project com.erickmob.petclinic:config-server:1.0.0-SNAPSHOT (/usr/src/config-server/pom.xml) has 1 error
[ERROR] Non-resolvable parent POM for com.erickmob.petclinic:config-server:1.0.0-SNAPSHOT: Could not find artifact com.erickmob.pet-clinic:pet-clinic:pom:1.0-SNAPSHOT and 'parent.relativePath' points at wrong local POM # line 5, column 13 -> [Help 2]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/ProjectBuildingException
[ERROR] [Help 2] http://cwiki.apache.org/confluence/display/MAVEN/UnresolvableModelException
ERROR: Service 'config-server' failed to build: The command '/bin/sh -c mvn clean package' returned a non-zero code: 1
If I make a mvn clean install on root folder or config-server folder, everything is ok, but when I try to run docker-compose build it gives me error saying that cannot found the parent pom.
This is not really Docker, but Maven.
You are building your config-server/ project only (and here is when Docker is in the middle) but the "context" sent to Docker is just what's inside config-server/ and not the entire project. To build config-server/ you need the entire project because you are referring/using a parent POM.
IIUC,
docker-compose.yml
version: "2"
services:
config-server:
build:
context: ./
dockerfile: Dockerfile
image: config-server
container_name: config-server
mem_limit: 512M
ports:
- 8888:8888
Dockerfile
FROM maven:3.6.3-adoptopenjdk-11 as build
COPY . /usr/src/
WORKDIR /usr/src/
RUN mvn clean package
FROM adoptopenjdk/openjdk11:alpine-jre
RUN addgroup -S spring && adduser -S spring -G spring
RUN mkdir -p /files && \
chown -R spring:spring /files
USER spring:spring
VOLUME /files
WORKDIR /app
COPY --from=build /usr/src/config-server/target/config-server-0.0.1-SNAPSHOT.jar /app/app.jar
EXPOSE 8888
ENTRYPOINT ["java", "-jar", "app.jar"]
run the following command docker-compose up -d
logs:
root#ubuntu:/home/sathya/Desktop/stackoverflo/docker/spring-boot/spring-petclinic-microservices# docker-compose up -d
Building config-server
Step 1/13 : FROM maven:3.6.3-adoptopenjdk-11 as build
---> a4579cf3debf
Step 2/13 : COPY . /usr/src/
---> 9841e6033a86
Step 3/13 : WORKDIR /usr/src/
---> Running in aa2526d60d42
Removing intermediate container aa2526d60d42
---> 6e3b35f66218
Step 4/13 : RUN mvn clean package
---> Running in 5b2bac4dcef4
[INFO] Scanning for projects...
Downloading from central: https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-starter-parent/2.3.2.RELEASE/spring-boot-starter-parent-2.3.2.RELEASE.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-starter-parent/2.3.2.RELEASE/spring-boot-starter-parent-2.3.2.RELEASE.pom (8.6 kB at 2.2 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-dependencies/2.3.2.RELEASE/spring-boot-dependencies-2.3.2.RELEASE.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-dependencies/2.3.2.RELEASE/spring-boot-dependencies-2.3.2.RELEASE.pom (122 kB at 25 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/com/datastax/oss/java-driver-bom/4.6.1/java-driver-bom-4.6.1.pom
Downloaded from central: https://repo.maven.apache.org/maven2/com/datastax/oss/java-driver-bom/4.6.1/java-driver-bom-4.6.1.pom (3.8 kB at 3.1 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/io/dropwizard/metrics/metrics-bom/4.1.11/metrics-bom-4.1.11.pom

Error executing docker file in spring boot maven project

Facing problems in docker file (spring boot maven project). When running docker file, it gives an error "The command '/bin/sh -c mvn -f pom.xml clean package' returned a non-zero code: 1". However if I directly execute the command 'mvn -f pom.xml clean package', I do not get any error.
FROM maven:3.6.1-jdk-8-slim AS build
RUN mkdir -p /workspace
WORKDIR /workspace
COPY pom.xml /workspace
COPY src /workspace/src
RUN mvn -f pom.xml clean package
FROM openjdk:8-alpine
COPY --from=build /workspace/target/*.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","app.jar"]
Please help on the same.
You can use experimental feature cache to speed up building process
# syntax=docker/dockerfile:1.0-experimental
you dont need to run mkdir
RUN mkdir -p /workspace
just workdir is enought
WORKDIR /workspace
you can copy folder by
COPY . .
and add .dockerignore file from preventing not build related files and folders
RUN --mount=type=cache,target=/root/.m2/repository mvn -e -B clean package -
Dmaven.test.skip=true
change
ENTRYPOINT ["java","-jar","app.jar"]
to
CMD ["java","-Djava.security.egd=file:/dev/./urandom", "-jar", "/app.jar"]
maven output will be helpfull to detect what exactly is wrong.

DockerFile for maven build which have custom Jar

WE are creating maven java application which uses custom jar.How can i install custom jar in local maven in docker build context?
Docker file is given below:
FROM maven:3-jdk-8-alpine AS build
COPY Fix43_INTL.jar /app/src
RUN mvn install:install-file -Dfile=/app/src/Fix43_INTL.jar -DgroupId=com.neo -DartifactId=fix43 -Dversion=1.0 -Dpackaging=jar -DgeneratePom=true
RUN mvn -f /app/pom.xml clean install
COPY ./web-integral-fix-initiator/src /app/src
COPY ./web-integral-fix-initiator/pom.xml /app
RUN mvn -f /app/pom.xml clean package
FROM openjdk:8
COPY --from=build /app/target/web-integral-fix-initiator-0.0.1-SNAPSHOT.jar /app/web-integral-fix-initiator-0.0.1-SNAPSHOT.jar
Based on your comments, error
/app/src : "no such file or directory"
is straight forward. You are trying to copy the jar in a path inside the container that does not exist. You should first create the target directory(/app/src) and then copy the jar into it.
I edited your dockerfile and made some optimizations(mainly chaining commands in single layer, removed unnecessary mvn clean install).
FROM maven:3-jdk-8-alpine AS build
RUN mkdir -p /app/src
COPY Fix43_INTL.jar /app/src
COPY ./web-integral-fix-initiator/src /app/src
COPY ./web-integral-fix-initiator/pom.xml /app
WORKDIR /app
RUN mvn install:install-file -Dfile=/app/src/Fix43_INTL.jar -DgroupId=com.neo -DartifactId=fix43 -Dversion=1.0 -Dpackaging=jar -DgeneratePom=true \
&& mvn clean package
FROM openjdk:8
COPY --from=build /app/target/web-integral-fix-initiator-0.0.1-SNAPSHOT.jar /app/web-integral-fix-initiator-0.0.1-SNAPSHOT.jar

unable to run my docker image in my local

I have a maven web service project run with jetty maven plugin.
My Dockerfile is:
FROM openjdk:8
# maven
From maven:3.3.9
ENV MAVEN_VERSION 3.3.9
COPY pom.xml /home/pusher-docker/pusher-test/pom.xml
COPY src /home/pusher-docker/pusher-test/src
WORKDIR /home/pusher-docker/pusher-test
RUN mvn package
CMD ["java","-cp","target/pusher-test.jar","mvn jetty:run"]
I am starting docker container with
sudo docker run docker-pusher-test
but I am getting the following error:
no main manifest attribute, in pusher-test.war
How I can successfully start the container?

Caching downloaded dependencies with Docker

I'm building a Docker image to build and run tests on Java application trying to allow Docker to cache downloaded dependencies the same way that works with Ruby and Node.js applications with the following Dockerfile
FROM maven:latest
ENV APP_DIR=/app
RUN mkdir -p $APP_DIR
WORKDIR $APP_DIR
COPY pom.xml $APP_DIR
RUN mvn dependency:resolve
COPY . $APP_DIR
CMD mvn test
But when a image is run, mvn test still downloads dependencies, any ideas on how to fix this?
You can force maven not to download any artificates by running it in offline mode. However, you need first to have all dependencies locally available.
...
COPY pom.xml $APP_DIR
RUN mvn -Dmaven.repo.local=./maven-repo test
COPY . $APP_DIR
CMD mvn -Dmaven.repo.local=./maven-repo test -o

Resources