Exception when running Spring Boot app inside of Docker - java

I have a problem connecting to the config-server. I am not sure what am I doing wrong. I have configured server running in a docker container named "config-server" on port 8888.
http://config-server:8888. Will be trying the next url if available
2020-08-10 17:38:35.196 ERROR 11052 --- [ main] o.s.boot.SpringApplication : Application run failed
java.lang.IllegalStateException: Could not locate PropertySource and the fail fast property is set, failing
at org.springframework.cloud.config.client.ConfigServicePropertySourceLocator.locate(ConfigServicePropertySourceLocator.java:148) ~[spring-cloud-config-client-2.2.3.RELEASE.jar:2.2.3.RELEASE]
discovery-server bootstrap.yml
spring:
application:
name: discovery-server
cloud:
config:
uri: http://config-server:8888
fail-fast: true
retry:
max-attempts: 20
EDIT
config-server Dockerfile
FROM openjdk:11.0-jre
ADD ./target/config-server-0.0.1-SNAPSHOT.jar config-server-0.0.1-SNAPSHOT.jar
ENTRYPOINT ["java", "-jar", "/config-server-0.0.1-SNAPSHOT.jar"]
EXPOSE 8888
docker run -p 8888:8888 --name config-server 3deb982c96fe
Discavery-server is not running in docker. First I want to create its .jar file

Original question already answered in comments, answering the last point here for better formatting:
Jar file will be built in /target folder of your application everytime you run mvn clean install or gradle build. In order to run this in Docker you have to copy the jar file from your /target directory to the Docker container inner files, and then run it (java -jar nameOfYourJar.jar).
Name of your jar can be defined in maven/gradle settings but to keep your Dockerfile generic I suggest following Dockerfile:
FROM openjdk:11.0-jre
ARG JAR_FILE=/target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","app.jar"]
with ARG JAR_FILE you save the path to any jar file (found in target) as JAR_FILE variable in docker and then you can copy it to your Docker inner files where it will be stored under the name app.jar.
ENTRYPOINT is the command that will be run on container start.
Place the Dockerfile next to the /target directory (so in root folder of your app) and run following command in terminal:
docker build -t springapp . && docker run --rm -d -p 8080:8080 springapp
Hope this clarifies everything.

Related

Spring Boot Bitbucket Pilpline to ECR

I try to build a CI/CD Pipeline with Bitbucket-Pipelines.
My dockerized Spring Boot Application should be pushed to my AWS ECR. But when the pipeline executes the docker commands it throws an error:
COPY failed: file not found in build context or excluded by .dockerignore: stat target/app.jar: file does not exist
Does anyone set up a similar pipeline and can help me out?
Here is my dockerfile:
FROM openjdk:11
ARG JAR_FILE
COPY target/app.jar app.jar
EXPOSE 80
ENTRYPOINT ["java", "-jar", "app.jar"]
Here is my pipeline yaml config:
image: maven:3.6.3
pipelines:
default:
- step:
name: Clean install
script:
- cd path.to.file
- mvn clean install -DskipTests
- step:
name: Push to ECR
services:
- docker # Enable Docker for your repository
script:
# build the image
- cd path.to.dockerfile
- docker build -t app-devtest .
# use the pipe to push the image to AWS ECR
- pipe: atlassian/aws-ecr-push-image:1.4.2
variables:
AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY
AWS_DEFAULT_REGION: $AWS_DEFAULT_REGION
IMAGE_NAME: app-devtest
TAGS: 'latest'
The issue is with your dockerfile. This COPY target/app.jar app.jar will work in your local workspace, because you already have the jar locally (it is created when you run your app). In the VM where the pipeline is running, there is no provision to build the jar file over there, so the app.jar is missing.
To fix the issue, first you will need to tell maven to name your jar file using the name you give it; in this case "app". In the pom file of your project, add the following.
<build>
<finalName>app</finalName>
</build>
Then you will need to build the project in the docker build process and then copy your jar file. So the docker file should look something like below.
# first stage: use a maven project to clean package the application
FROM maven:3.8.1-openjdk-11-slim AS MAVEN_BUILD
COPY / /home/myapp/
# Building the jar file using maven
RUN mvn -f /home/app/pom.xml clean package
# second stage: use open jdk 11.0.11 image to build the project docker image
FROM adoptopenjdk/openjdk11:jre-11.0.11_9-alpine
# copy jar needed from the first stage and discard the rest
COPY --from=MAVEN_BUILD /home/myapp/app.jar /usr/local/lib/app.jar
EXPOSE 80
# set the startup command to execute the jar
CMD java -jar /usr/local/lib/app.jar
The above uses a 2 stage docker build process which creates a lightweight docker image.
This should be enough to solve the issue.
COPY failed: file not found in build context or excluded by
.dockerignore: stat target/app.jar: file does not exist

Docker unable to access jar file without docker-compose

as far as I know I only got this answer for my query, but unfortunately I do not have docker-compose.yml I am just trying with plain Dockerfile below is my dockerFile.
FROM openjdk:8-jre-alpine
VOLUME /tmp
ARG JAR_FILE
COPY ${JAR_FILE} ebiPorjectJava.jar
ENTRYPOINT ["java","-jar","/ebiProjectJava.jar"]
but build is getting successful, by below command.
docker build -t ebiproject .
but when I am trying to run the docker it says unable to access jar file
docker run -p 3000:3000 ebiproejct
but it says unable to access jar
I am running docker on windows and I have spring boot application to run
I updated with below one:
FROM openjdk:8-jre-alpine
VOLUME /tmp
ARG JAR_FILE
COPY ${JAR_FILE} /ebiProjectJava.jar
ENTRYPOINT ["java","-jar","/ebiProjectJava.jar"]
but now I am getting corrupted error:
ARG JAR_FILE
expects you to pass the path of the jar_file as docker run argument
Otherwise nothing will be copied to the place, thus java jar won't be able to run the jar.
Check this out: https://vsupalov.com/docker-env-vars/
In this situation you can go in your container with this command:
docker run --rm -it <container_name> <image_name> <command>
you can use bash, ash or sh as
and after entering the container, you can check, run, or any tests in that.

Unable to access jarfile from docker run command for java maven project

Build docker images using docker file for maven project
When try running docker run getting error saying Unable to access jarfile.
Can some one assist on whats wrong with .
FROM openjdk:8-jre-alpine as release
RUN addgroup -g 1001 -S user1 && user1 -u 1001 -S user1 -G user1
WORKDIR /home/app
COPY --from=builder /home/app/service-1.0.0.jar .
RUN chown -R user1:user1 /home/app
USER user1
ENTRYPOINT ["java", "-jar" ,"/home/app/service-1.0.0.jar"]
but when i go for docker run with local config files as build mount
docker run -it --mount type=bind,source=D:/Java/service/docker/config,target=/home/app services
Unable to access jarfile /home/app/service-1.0.0.jar
Can someone assist on this or is something wrong i did??
I am not sure if you are looking for multistage docker build. If not, you need not use "COPY --from=builder" as it tires to copy the build artifact from your previous stage to this new stage. If it is a single stage docker build, you might just use the copy as follows -
COPY /home/app/service-1.0.0.jar .
Able to resolve the same, Issue was due to mound of config files not happened properly.
instead of docker run -it --mount type=bind,source=D:/Java/service/docker/config,target=/home/app services changes it to docker run -it --mount type=bind,source=D:/Java/service/docker/config/app.config,target=/home/app services which made service to load and work

Add jars to Docker image from Plugin Directory

I have a spring boot application which contains a Main Class. I have Docker File as below:
FROM docker.io/openjdk:11-jre-slim
EXPOSE 8082
EXPOSE 8443
ADD target/base-application.jar app.jar
ENV JAVA_OPTS=""
ENTRYPOINT exec java $JAVA_OPTS -Dspring.profiles.active=prod -jar /app.jar
I am creating a Docker image by using this Docker file. Let's consider that this docker file is Docker1.
I have another Spring Boot application (which doesn't have a Main class) with Docker file as below:
FROM Docker1:0.0.1
EXPOSE 8443
ADD target/child-application.jar child-application.jar
ENV JAVA_OPTS=""
ENTRYPOINT exec java $JAVA_OPTS -Dspring.profiles.active=dev -jar /app.jar
I am using Docker1 as a base image for the docker image of 2nd application. I am running the Main class of Docker1 by running 2nd Docker image. Now there are 2 jar files of another spring boot projects(messagepack1.jar & messagepack2.jar). I want to load these jars in the 2nd docker image. I am trying something like this:
FROM Docker1:0.0.1
EXPOSE 8443
COPY src/main/resources/message-packs/messagepack1.jar m1.jar
COPY src/main/resources/message-packs/messagepack2.jar m2.jar
ADD target/child-application.jar child-application.jar
ENV JAVA_OPTS=""
ENTRYPOINT exec java $JAVA_OPTS -Dspring.profiles.active=dev -jar /app.jar
But it is not loading the jars when I am running the 2nd Docker image. Can you please help me out?
Finally, I could find the solution to this problem:
ADD /src/main/resources/message-packs /external-jars
ENTRYPOINT exec java -cp app.jar -Dloader.path=external-jars/ -Dloader.main=com.example.MainClass org.springframework.boot.loader.PropertiesLauncher
Here, I am adding the External Jars from src/main/resources/message-packs directory to the "external-jars" folder of the Docker Image. Then I am loading these jars to the Classpath of my application by using "java -cp" command.

Automated Spring boot Docker Image failed to build on docker hub when connected to github/bitbucket repository

I have a simple Dockerfile in my spring boot as follow. I am able to build the image successfully locally, and can push using my credentials.
But my build keeps failing on every attempt to build automatically.
FROM openjdk:8-jdk-alpine
LABEL maintainer="xxxxx#xxx.com"
VOLUME /tmp
EXPOSE 8080
ARG JAR_FILE=target/jollof.jar
ADD ${JAR_FILE} jollof.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-
jar","/jollof.jar"]
From docker hub, I got this from the log.
Building in Docker Cloud's infrastructure...
Cloning into '.'...
Warning: Permanently added the RSA host key for IP address 'xxx.xx.xxx.xxx' to
the list of known hosts.
....
....
Step 6/7 : ADD ${JAR_FILE} jollof.jar
ADD failed: stat /var/lib/docker/tmp/docker-
builder674045875/target/jollof.jar:
no such file or directory
Unlike your local environment, Docker Hub fetches then builds your project in a fresh environment, so that the file target/jollof.jar that is intended to be copied is not available in the docker context. Hence the error you observe.
So I'd suggest refactoring your Dockerfile so that mvn package or so is done in the Dockerfile itself (which is a best practice to adopt, for the sake of reproducibility). Note that this configuration will be working for Docker Hub's automated builds as well as the builds in your local environment.
For example, below is an example Dockerfile that inspired by the that of this SO answer How to convert a Spring-Boot web service into a Docker image? as well as the Dockerfile of your post:
FROM maven:3.6-jdk-8 as maven
WORKDIR /app
COPY ./pom.xml ./pom.xml
RUN mvn dependency:go-offline -B
COPY ./src ./src
# TODO: jollof-* should be replaced with the proper prefix
RUN mvn package && cp target/jollof-*.jar app.jar
# Rely on Docker's multi-stage build to get a smaller image based on JRE
FROM openjdk:8-jre-alpine
LABEL maintainer="xxxxx#xxx.com"
WORKDIR /app
COPY --from=maven /app/app.jar ./app.jar
# VOLUME /tmp # optional
EXPOSE 8080 # also optional
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app/app.jar"]

Categories