unable to run docker container as getting this error:Unable to access jarfile app.jar - spring-boot

I have created the image using below Docker file.Its working fine in local when I run it in the container using this image[docker run -p 80:8080 username/spring-boot-docker-aws-demo:tag
FROM openjdk:8-jdk-alpine
COPY target/spring-boot-docker-aws-demo.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","app.jar"]
I have pushed this image to Docker hub to run it in EC2 instance.when I run it its saying "Error: Unable to access jarfile app.jar"
https://hub.docker.com/layers/chaituu/spring-boot-docker-aws-demo/docker-aws-demo-tag/images/sha256-110363a016eb2250264d96c8890dede518d8e519fdc9a27174334fe1096a540e?context=repo
what could be the issue?

I had a similar issue today (although in my case it wouldn't even run locally). Here's what your Dockerfile would look like with my fix:
FROM openjdk:8-jdk-alpine
COPY target/spring-boot-docker-aws-demo.jar /app/app.jar
EXPOSE 8080
ENTRYPOINT ["sh"]
CMD ["-c", "java -jar /app/app.jar"]

Related

How to pass application.properties to SpringBoot app in Docker container?

Assume we have a simple Dockerfile where my_sb_app.jar is a SpringBoot application:
FROM bellsoft/liberica-openjdk-alpine:11.0.9-12
WORKDIR /app
COPY target/my_sb_app.jar /app
EXPOSE 8080
ENTRYPOINT java -jar my_sb_app.jar
I would like to pass custom app.properties on each run
docker run my_image_name /somewhere/on/my/host/app.properties
so the expected final command (inside the container) could be identical to the following
java -jar my_sb_app.jar --spring.config.location=%%file_with_contents_of_app.properties%%
What is the standard way of passing a file into a Docker container?
How the Docker image sould be changed?
The standard way to pass files from host to the container is with volumes:
https://docs.docker.com/storage/bind-mounts/
https://docs.docker.com/storage/volumes/
In your example:
docker run -v /somewhere/on/my/host/app.properties:/opt/app.properties my_image_name
java -jar my_sb_app.jar --spring.config.location=/opt/app.properties

Using a Bind Mount in Docker to have a folder on Windows Host Machine available to Container

I'm trying to get a setup where I can deploy a Docker Container with a java app installed on it where the app can write to a folder in the container and for those files to appear on the host machine.
I believe that a --mount with type=bind is the correct solution for this. However, I cannot seem to get the mount to show up when I run docker inspect MyContainer.
My full setup is as follows;
Docker File:
FROM openjdk:14.0.2-jdk-nanoserver
WORKDIR /monitor
COPY target/monitor.jar ./
ENTRYPOINT ["java", "-jar", "monitor.jar"] --restart unless-stopped
Build Command:
docker build -t monitor .
and my Run command:
docker run --restart unless-stopped --name MonitorContainer monitor --mount type=bind,source=C:/test,target=/monitor
With the above, I'm attempting to have the folder C:/test available for read and write inside the container at the path /monitor. However, when I run docker inspect StreamMonitorContainer I see that the mount section is empty so it appears the mount is not created.
I receive no errors, so it's all rather confusing.
I would appreciate any help, I'm completely novice at this so please be nice :)
p.s. This question differs from Docker bind mount usage because it's dealing with Volumes, not Bind Mounts (despite the title.)

Why does my Spring Boot App hang when started in a Docker Swarm

I created a docker image of a Spring Boot app and when I run it using
docker run -d -p 4000:8080 stanlick/stats:v1
It works great! However, when I try to run it as a swarm (even with replicas:1) it hangs on startup.
Running
docker service logs play-ball_web
reveals:
Dockerfile:
FROM openjdk:12-jdk-alpine
VOLUME /tmp ARG JAR_FILE
COPY target/*.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

docker image syntax error

When I run docker command on my image, it errors out. Any ideas on what should I include in my Dockerfile for CMD? Currently, I have java -jar xxxx.jar in it.
here is my Dockerfile:
FROM openjdk:8-jre-alpine
COPY ./target/hola-docker-1.0.0-SNAPSHOT.jar D:/docker-built-test
WORKDIR -p D:/docker-built-test
EXPOSE 8080
CMD ["java", "-jar", "hola-docker-1.0.0-SNAPSHOT.jar"]
My command docker build -t hello-manual-build1 .
click here Created Docker Image
When am trying to create a container using the new image then facing an error don't know how to resolve please help me to move forward
Thanks

Passing env variables to DOCKER Spring Boot

I have a SpringBoot application and its Dockerfile is as follows. I have application.properties for different environments like local/dev/qa/prod. When I run the application locally in IDE, I pass -Dspring.profiles.active=local in VM options so that it loads the application-local.properties. For running as docker containers, I build an image which comprises of all the application.properties. i.e. it's only SAME docker image for all the environments.
When I run the image in an environment, I want to somehow make the SpringBoot to understand that its dev env, so it has to load application-dev.properties. I am using AWS ECS for managing the containers.
FROM openjdk:8-jdk-alpine
VOLUME /tmp
ADD target/sample-test-sb-sample-app-1.0-exec.jar app.jar
EXPOSE 8080
ENV JAVA_OPTS=""
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ]
The easiest (and probably the best way) to do it via environment variable in a docker container:
SPRING_PROFILES_ACTIVE=dev,swagger
UPDATE:
In order to set environment variables to docker, you do not need to modify Dockerfile. Just build your docker image and then run it with the env variables set:
docker run your-docker-container -e SPRING_PROFILES_ACTIVE='dev,swagger' -p 8080:8080
In the .Dockerfile file:
ENTRYPOINT [ "sh", "-c", "java -Dspring.profiles.active=**${ENV}** -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ]
And while running the docker:
docker run --env ENV=*local* -d -p 8080:8080 <*image id*>
This way, the environment variable gets local as value and passes to Dockerfile when we bring up a container.
Update
You can also do like
ENTRYPOINT ["java","-jar", "-Dspring.profiles.active=${ENV} -Djava.security.egd=file:/dev/./urandom","app.jar"]
and while docker image
docker run --env ENV=local -d -p 8080:8080 <*image id*>

Resources