Docker build does not create an image but the application starts anyway - java

Currently I am running windows with docker for windows installed.
I have made a java application in Spring Boot that I want to build an image of and run in a docker container.
What am I doing wrong?
When I run the bellow and can see in my cmd prompt that the application starts and run. But there is no image in in docker and nothing running there.
#
# Build stage
#
FROM maven:3.8.4-openjdk-17 AS build
COPY src /test/src
COPY pom.xml /test
#RUN mvn -f /test/pom.xml clean package
RUN mvn -f /test/pom.xml clean package
#
# Package stage
#
FROM openjdk:17-alpine
COPY --from=build /test/target/test-0.0.1-SNAPSHOT.jar /usr/local/lib/test.jar
ENTRYPOINT ["java","-jar","/usr/local/lib/test.jar"]
What am I missing?
I have the application in a folder /test and the Dockerfile is also under /test. I go to this location in the cmd prompt and enter:
docker build -t testapp .

The command you are running, docker build -t testapp ., only creates the docker image that you can check with the command someone stated in the comments : docker images -a.
To run the image in a docker container you must use the run command : docker run testapp. Then you will see the container in the Docker app.

docker build will only build docker image
if you want to run the image in your docker try running the command
docker run testapp
docker build builds a new image from the source code.
docker create creates a writeable container from the image and prepares it for running.
docker run creates the container (same as docker create) and runs it.

I was doing everything correct except for that when I thought the image was ready/built I run test of my java application that looked as the application had started.
By setting <maven.test.skip>true</maven.test.skip> I skipped the test and the build was finished. I got the image which I now can start. Thank you all for your question and assistance.

Related

How do we mount a file as file instead of directory inside docke container?

I want to execute a file in new docker container lets say i want to execute Hello.java which prints Hello World. I want to execute this file inside new docker container.
docker run -it -v /Hello.class:/Hello.class alpine:latest ls -l
I would fire above command using Runtime class of java.
It shows that Hello.class is a directory.
I want it as a file so that i can execute it inside new docker container.
How to do this??
Hello I would suggest to make a small dockerfile for simplicity.
Step1: Create Dockerfile:
FROM openjdk:7
COPY . .
WORKDIR /usr/src/app
RUN javac HelloClass.java
CMD ["java", "HelloClass"]
Step 2: Building the Docker Container
After you have created both the JavaMain Class and the Dockerfile, you can now use the Docker build command to build your Docker Image.
docker build -t hello-demo .
Step 3: Running the Docker Container
Now, you can use the Docker run command to run your Docker Container.
docker run -it hello-demo

Docker Build command failing on windows 10 .Docker version used is 19.03.5

When running docker build command it returns an error message as shown in the screen shot .
Ppath of the docker file:
C:\Users\****\dockerfiles
DockerFile.txt
# Base Alpine Linux based image with OpenJDK JRE only
FROM openjdk:8-jre-alpine
# copy application WAR (with libraries inside)
COPY target/spring-boot-*.war /app.war
# specify default command
CMD ["/usr/bin/java", "-jar", "-Dspring.profiles.active=test","/app.war"]
Create a directory called 'dockerfiles' under C:\Users\username
Keep the {dockerfile without any extension} under the newly created directory.
run the command {from C:\Users\username directory}: docker build -t ./dockerfiles

Deploy SpringBoot Microservices in Docker

I have a microService jar and docker file at my windows dextop x folder.
I have installed docker in my windows PC.
I have the below docker.server file
FROM openjdk:8-jre
ADD target/shopfront-0.0.1-SNAPSHOT.jar app.jar
EXPOSE 8010
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
Now I want to build a docker image from this docker file, then what needs to be done.
I opened the docker terminal and paste the below command
docker build --file=Local location of Dockerfile.server
//(which is desktop x folder)\
--tag=microserviceName on my wish latest --rm=true
.
But it's not working.
As well as I get the current directory and I tried to copy the jar and the docker file in the current docker directory that is not happening.
Please guide what needs to be done.

Google Cloud Platform pipeline/container builder issue building docker image using COPY or ADD command for Spring Boot Java Application

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.

Building a docker image from dockerfile, can't get java to run correctly, but can run java from image as command line parameters in docker run command

I am new to docker and dockerfile files, having just started trying to write them. I have built a simple java console application and can successfully build a docker image from a dockerfile, but if I include
CMD ["java","-jar","app.jar"]
when I try to run the image I always get a bin/sh error, typically "java not found" or the like.
However, when I don't include the CMD line and just use this Dockerfile to build my image
FROM openjdk:8-jre-alpine
COPY app.jar /app.jar
and then run
docker run -it --rm my-container:tag
I can then run
java -jar app.jar
and the application runs as expected.
I can also run
docker run -it --rm my-container:tag java -jar app.jar
and the application runs as expected.
Every guide I read says I should be able to use CMD or ENTRYPOINT as written above, but nothing ever works.
What might I be missing in this simple example?
Thank you,
Trevor
EDIT: I am running docker version 18.06.1-ce-mac73 (26764) on MacOS Sierra. I am not positive that docker works this way, but I have two image versions in my public docker hub. The dockerfile for v1 is:
FROM openjdk:8-jre-alpine
COPY 454calendar.jar app.jar
The dockerfile for v2 is:
FROM openjdk:8-jre-alpine
ENV PROJECT_DIR=/app
WORKDIR $PROJECT_DIR
COPY 454calendar.jar $PROJECT_DIR
If I add
CMD [“java”,”-jar”,”454calendar.jar”]
to the v2 dockerfile and rebuild, I get this error with the docker run command.
/bin/sh: [“java”,”-jar”,”454calendar.jar”]: not found
Without the CMD line, I can run container and it starts right into the /app working directory where I can run the java command and execute the program.
The two versions of the container in my public docker repository do not have the CMD line in their respective dockerfiles.
The solution was maddeningly simple. Thanks to #Rakesh, I checked the configuration for TextEdit on MacOS and saw that Smart Quotes was turned on. Once I turned off that option and retyped the double quotes, then rebuilt and ran the docker container, the application started up just as expected.
I don't see any problem with your approach. I was able to make a HelloWorld application to run with the below Dockerfile.
FROM openjdk:8-jre-alpine
RUN mkdir /app
RUN cd /app
COPY HelloWorld.jar /app/HelloWorld.jar
WORKDIR /app
CMD ["java","-jar", "HelloWorld.jar"]
I'm on the following docker version
docker -v
Docker version 18.06.1-ce, build e68fc7a
docker-compose -v
docker-compose version 1.22.0, build f46880f

Categories

Resources