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.
Related
I reference an HTML file in my code, and access it with:
Path filePath1 = Path.of("./email.html");
When I run the project locally, the project works fine, and the file loads normally. However, when running the project in a Docker container, I get the following error:
java.nio.file.NoSuchFileException: ./email.html
Here is my Docker file for reference
FROM openjdk:11.0-jdk-slim as builder
VOLUME /tmp
COPY . .
RUN apt-get update && apt-get install -y dos2unix
RUN dos2unix gradlew
RUN ./gradlew build
# Phase 2 - Build container with runtime only to use .jar file within
FROM openjdk:11.0-jre-slim
WORKDIR /app
# Copy .jar file (aka, builder)
COPY --from=builder build/libs/*.jar app.jar
ENTRYPOINT ["java", "-Xmx300m", "-Xss512k", "-jar", "app.jar"]
EXPOSE 8080
Thank you for the answers. So this is a Java project, so there is no index.html to add. I tried changing the work directory to /src, but it is still not picking it up
Docker has no access to the filesystem fromm the host OS.
You need to put it in there as well:
COPY ./index.html index.html
There's a couple of options:
Copy the index.html in the docker image (solution by ~dominik-lovetinsky)
Mount the directory with your index.html file as a volume in your docker instance.
Include the index.html as a resource in your app.jar, and access it as a classpath resource.
The last option: including resources as classpath resource, is the normal way webapps work, but I'm not sure if it works for you.
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.
I use docker-compose to launch different Spring Boot apps.
My docker images are defined with this kind of Dockerfile:
FROM openjdk:8-jdk-alpine
ADD app.jar app.jar
ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/app.jar"]
However, I would like to benefit from debugging and hot-reload features using something like mvn spring-boot:run without being dependent of a particular IDE.
What is the best way to accomplish debugging and hot-reloading with Spring Boot in a Docker container without being dependent of a particular IDE?
Notes:
my source files are build into a jar (with Maven) which is copied to a different location containing the definition of my Docker images ; meaning my sources files are not in the docker image.
the reason I want to develop in the Docker container is that my apps depend on each other, and are configured in the docker-compose environment, so I cannot easily run one app alone outside the docker network and environment.
I thought of mounting a volume containing my spring boot projects in the docker containers, and then use mvn spring-boot:run in the container ; but I can't prevent maven to download all dependencies from the internet (I tried specifying a local repository containing all my dependencies without success). I would like to know if this a decent solution and how to make it work.
You have to follow the following steps to build and run spring boot application in docker.
Step-1 : Create a File called Dockerfile in your Project.
Step-2 : Write the Following Code on you Dockerfile
# Use the official maven/Java 8 image to create a build artifact.
# https://hub.docker.com/_/maven
FROM maven:3.6-jdk-11 as builder
# Copy local code to the container image.
WORKDIR /app
COPY pom.xml .
COPY src ./src
# Build a release artifact.
RUN mvn package -DskipTests
# Use AdoptOpenJDK for base image.
# It's important to use OpenJDK 8u191 or above that has container support enabled.
# https://hub.docker.com/r/adoptopenjdk/openjdk8
# https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds
FROM adoptopenjdk/openjdk11:alpine-slim
# Copy the jar to the production image from the builder stage.
COPY --from=builder /app/target/your-app-name*.jar /your-app-name.jar
# Run the web service on container startup.
CMD ["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/your-app-name.jar"]
Step-3 : Start Your Docker Desktop Application
Step-4 : Open Your Terminal or Windows PowerShell. Then go to Project Directory.
Step-6 : Write the Following Command to create image for your application (You must have internet connection to download all dependencies).
docker build -f Dockerfile -t your-app-name .
Step-7 : After image creation success. Write the following code to run the image in Docker container.
docker run -p docker-port:app-port image-name
Following your line of thinking you can try to copy your dependencies from a volume into the project container and then use the offline mode in something like this:
FROM openjdk:8-jdk-alpine
WORKDIR /app
# copy the Project Object Model file
COPY ./pom.xml ./pom.xml
# copy your dependencies
COPY app.jar app.jar
# copy your other files
COPY ./src ./src
# Set fetch mode to offline to avoid downloading them from the internet
RUN mvn dependency:go-offline
Apparently it's also possible to configure the offline mode globally by setting the offline property in the ~/.m2/settings.xml file, you can setup that and copy your m2 file and reference it when running the container
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
https://maven.apache.org/xsd/settings-1.0.0.xsd">
<offline>true</offline>
</settings>
mvn -Dmaven.repo.local=~/.m2/settings.xml ...
You can find more information here:
https://www.baeldung.com/maven-offline
Specifying Maven's local repository location as a CLI parameter
I have a working app using Spring Framework + AngularJs front-end.
I do deploy it on amazon AWS before by simply packaging mvn package into .war file.
Now I need to setup a linux env in docker locally to debug some additional functionality (Using windows as the main OS) and preferably to deploy this docker container in future.
I do seen some articles on dockerizing the Spring Boot app.
This is the example dockerfile from spring.io
FROM frolvlad/alpine-oraclejdk8:slim
VOLUME /tmp
ADD gs-spring-boot-docker-0.1.0.jar app.jar
RUN sh -c 'touch /app.jar'
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
My question is - how do I run my .war file on docker container?
If you're looking to run this application on your local Linux machine, then you can create this Dockerfile in the same directory where the WAR file exists.
FROM tomcat:8.0.21-jre8
RUN [“rm”, “-rf”, “/usr/local/tomcat/webapps/ROOT”]
COPY dbconnect.war /usr/local/tomcat/webapps/ROOT.war
CMD [“catalina.sh”, “run”]
You can then build the Docker image and provide your custom tag:
docker build -t <your-username>/tomcat-example:latest .
Finally you can run this container.
docker run -p 8080:8080 -d --name tomcat <your-username>/tomcat-example:latest
You can check out these detailed examples here if need to run this application with a database or a web server.
https://github.com/dchqinc/dchq-docker-java-example
https://dzone.com/refcardz/java-containerization
You don't deploy a .war file in docker just like you deploy it into a tomcat server. You need to have a main(String args[]) entry point which is also specified in your jar's manifest file. Your app.jar specified as ENTRYPOINT will then run your main(). Inside the main() you can run an embedded web server which runs your application.
If you configure/package your Spring Boot app as an executable .jar instead of a .war (Spring Boot Initializr will do this for you if you select the .jar option), then running it in your container is a matter of adding an entrypoint to run java with -jar and your jar name, like
ENTRYPOINT ["java", "-jar", "/opt/yourapp-0.0.1-SNAPSHOT.jar"]
I have a java application (jar file) that I want to be able to run from a docker image.
I have created a Dockerfile to create an image using centos as base and install java as such:
Dockerfile
FROM centos
RUN yum install -y java-1.7.0-openjdk
I ran docker build -t me/java7 after to obtain the image me/java7
however I am stuck at some dead ends.
How do I copy the jar file from the host into the image/container
I require 2 parameters. 1 is a file, which needs to be copied into a directory into the container at runtime. The other is a number which needs to be passed to the jar file in the java -jar command automatically when the user runs docker run with the parameters
Extra Notes:
The jar file is a local file. Not hosted anywhere accessible via wget or anything. The closest I have at the moment is a windows share containing it. I could also access the source from a git repository but that would involve compiling everything and installing maven and git on the image so I'd rather avoid that.
any help is much appreciated.
In the Dockerfile, add a local file using ADD, e g
ADD your-local.jar /some-container-location
You could use volumes to put a file in the container in runtime, e g
VOLUME /copy-into-this-dir
And then you run using
docker run -v=/location/of/file/locally:/copy-into-this-dir -t me/java7
You can use ENTRYPOINT and CMD to pass arguments, e g
ENTRYPOINT ["java", "-jar", "/whatever/your.jar"]
CMD [""]
And then again run using
docker run -v=/location/of/file/locally:/copy-into-this-dir -t me/java7 --myNumber 42
(Have a look at the Dockerfile documentation.)
Suppose your file structure is as follow :
DockerTest
└── Dockerfile
└── local.jar
Dockerfile content will be :
FROM centos
RUN yum install -y java-1.7.0-openjdk
EXPOSE 8080
ADD /local.jar fatJar.jar
ENTRYPOINT ["java","-jar","fatJar.jar"]
Use following command :
$ cd DockerTest
$ docker build -f Dockerfile -t demo .