I am trying to put a piece of open source software in a docker container (https://github.com/att/XACML) but in this container I can not use maven. The documentation for running this service says to use mvn jetty, which does work fine, but in order to get this in a container I don't want to include a build step (maven).
Instead, I'd like a way to compile the a war, so I can copy just the war into the container and execute it from there.
I have tried many attempts to get the war running without maven jetty but none of them work.
java -jar /path/to/jar
no main manifest attribute error. There is no main class, it extends an HttpServlet
using jetty-runner
when I launch the war with jetty-runner through the command line I do not get any errors, but it boots up to a page showing the directory of files, and not the actual project.
Making an 'uber-jar' to package all deps
same issue as 1, get a no main manifest issue.
I can include more code if that would be helpful (pom files etc), but I don't want to add too much if it is unneeded. I am super unfamiliar with how java projects are packaged and deployed, so I am having a difficult time figuring out what needs to be done.
Thanks!
Minimal Dockerfile to work with your webapp / war file is ...
FROM jetty:9.4.18
ADD ROOT.war /var/lib/jetty/webapps/
This uses the official jetty docker image at
https://hub.docker.com/_/jetty
Managed at
https://github.com/eclipse/jetty.docker
The name ROOT.war is special, and will deploy your webapp in the "root" context path of "/"
Building Image
If you build it like this ...
$ docker build -t stackoverflow/jetty:latest .
Running Image
Interactively (so you can the logs)
$ docker run --interactive --tty --rm --publish 80:8080 stackoverflow/jetty:latest
As Daemon
$ docker run --detach --publish 80:8080 stackoverflow/jetty:latest
The server will be available on port 80 of the machine you ran the docker run command on.
Configuring Jetty Base
If you need to configure the jetty image you can use any of the standard start.jar commands.
Example:
FROM jetty:9.4.18
WORKDIR $JETTY_BASE
RUN java -jar $JETTY_HOME/start.jar --add-to-start=jsp
ADD ROOT.war /var/lib/jetty/webapps/
How This Works Without Maven
See the official image details ...
https://github.com/eclipse/jetty.docker/blob/master/9.4-jdk11/Dockerfile
The key commands are ...
WORKDIR $JETTY_BASE
ENTRYPOINT ["/docker-entrypoint.sh"]
CMD ["java","-jar","/usr/local/jetty/start.jar"]
Related
I want to deploy my java spring backend to AWS .
I figured the way to deploy jar file on aws. But the problem is as soon as I terminate the terminal deployment stops. I created a jar file on build and hosted on aws EC2 linux Instance by copying jar file there and running below command .
java -jar jarfile
I want this deployment to be persistent. Is there any tool similar to pm2 for nodeJs in JAVA so that I can run this jar file permanently. ?
Thanks for help.
You should use nohup to deploy jar file. It will run even though you close terminal
E.g:- nohup java -jar javaservice.jar > nohup.log &
I am building an image from a dockerfile for a spring boot sample app, using the COPY command to copy a local file (which i need to pass as a vm argument) from host into my container.
The docker is running on windows 10 and with linux container.
When I try to run the image it shows error same as
Could not find agent library test/libaegean.dll in absolute path, with error: Error loading shared library test/libaegean.dll: No such file or directory.
Here is my Dockerfile
FROM openjdk:8-jdk-alpine
RUN mkdir test
RUN chmod +x test
COPY libaegean.dll test
COPY dellicence.lic test
COPY application-0.0.1-SNAPSHOT.jar test
WORKDIR test
EXPOSE 6070
ENTRYPOINT ["java","-agentpath:test/libaegean.dll","-jar","application-0.0.1-SNAPSHOT.jar"]
This is how I am building the image
docker image build --file=Dockerfile --tag=sample .
This is how I am running the container
docker container run --name=sample sample
please look at the picture to have a clear idea.
Thank you for the help in advance.
docker build image success
docker run failure
I'm trying to implement the example from this tutorial:
https://spring.io/guides/gs/spring-boot-docker/
I successfully compiled the package:
C:\Users\Desktop\rest_api>docker build -t springio/gs-spring-boot-docker .
Sending build context to Docker daemon 105.6MB
Step 1/5 : FROM openjdk:13-alpine
---> c4b0433a01ac
Step 2/5 : EXPOSE 8080
---> Using cache
---> 010600c5a7d0
Step 3/5 : ARG JAR_FILE=target/rest_api.jar
---> Running in 8ba2e28e0870
Removing intermediate container 8ba2e28e0870
---> b453cd05cbd2
Step 4/5 : ADD ${JAR_FILE} app.jar
---> dade5dd3eff2
Step 5/5 : ENTRYPOINT ["java","-jar","/app.jar"]
---> Running in e8a1f985f0fd
Removing intermediate container e8a1f985f0fd
---> cfa353eb23c5
Successfully built cfa353eb23c5
Successfully tagged springio/gs-spring-boot-docker:latest
SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories.
C:\Users\Desktop\rest_api>
It's not clear for me where is the compiled package located? Can you guide me where it's located and how to mount it into Docker?
Docker file:
FROM openjdk:13-alpine
EXPOSE 8080
ARG JAR_FILE=target/rest_api.jar
ADD ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
In order to get the "compiled package" (spring boot jar) you need to invoke mvn package first.
This command will compile the sources and create a JAR in the target directory of your project.
Since you're working with spring boot, you'll need to configure spring boot maven plugin (make sure it appears in the pom.xml) This plugin creates a special jar of spring boot applications with all the dependencies inside. It stores it in the target directory
So, after invoking mvn package command and before running docker build command go to target directory and make sure that you have a relatively big JAR of the application.
This explanation, I assume, answers the part of the question "where is the compiled package located?"
Now as for "how to mount to Docker" part of the question:
In the Dockerfile you use ADD command. This command takes the jar and "adds" it to the docker image (like into the filesystem of the container), so It will be available under /app.jar (because you also rename the artifact on the way)
At build time, the jar file has been copied from your computer into the container and is since located at /app.jar. No need to mount it.
I'm trying to use Heroku with Java, I have this directory structure and I'm using IntelliJ:
target
com
x
y
Main.java
in the procfile:
web: java -cp target/classes/;target/dependency/* com.x.y.Main
When I try heroku local web I get " Could not find or load main class com.x.y.Main "
However, it is there. I'm using Windows.
Java classpath separator is platform dependent, ; for Windows and : for Unix.
If you are trying to run heroku local on Windows, your command will fail with exactly the message you posted. Replacing it with:
web: java -cp target/classes/;target/dependency/* com.x.y.Main
will make it run on Windows.
If you get this error with the app deployed in the cloud (which is Unix), then there is no com.x.y.Main class in the target/classes directory. You can verify that by running heroku run bash followed by ls -lR and checking the directory/files layout on the server.
If you run on Windows ensure to run mvn clean install to compile the sources into target/classes directory.
For Gradle based projects you would need to adjust the classpath since it uses the different layout (build/classes) or the jar file in build/libs, see the guide for the details:
web: java -jar build/libs/myprojectname-1.0-SNAPSHOT.jar
To make it work in cloud and on Windows without changing Procfile every time, create Procfile.windows with web: java -cp target/classes/;target/dependency/* com.x.y.Main and start the app using heroku local web -f Procfile.windows command locally.
I am pretty new to Tomcat and Docker - so I am probably missing a Tomcat fundamental somewhere in this question.
What I am trying to do is build a Docker container that runs a SpringBoot Restful web service that just returns some static data. This is all running on OSX so I am using Boot2Docker as well.
I've written my own Dockerfile to build the container that my app runs in:
FROM tomcat:8.0.20-jre8
RUN mkdir /usr/local/tomcat/webapps/myapp
COPY /1.0-SNAPSHOT/my-app-1.0-SNAPSHOT.war /usr/local/tomcat/webapps/myapp/
This Dockerfile works fine and I am able to start the container from the created image.
docker build -t myapp .
docker run -it --rm -p 8888:8080 myapp
This container starts correctly and outputs no errors and displays the message saying my app was deployed.
22-Mar-2015 23:07:21.217 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory
Deploying web application directory /usr/local/tomcat/webapps/myapp
The container also correctly has the myapp.war copied to the path described in the Dockerfile. Moreover I am able to navigate to Tomcat default page to confirm that Tomcat is running, I can also hit all the examples, etc.
To the problem, when I navigate to http://192.168.59.103:8888/myapp/getData I get a 404. I can't quite figure out why. Am I missing something regarding a .war deploy to Tomcat?
You are trying to copy the war file to a directory below webapps. The war file should be copied into the webapps directory.
Remove the mkdir command, and copy the war file like this:
COPY /1.0-SNAPSHOT/my-app-1.0-SNAPSHOT.war /usr/local/tomcat/webapps/myapp.war
Tomcat will extract the war if autodeploy is turned on.
There's a oneliner for this one.
You can simply run,
docker run -v /1.0-SNAPSHOT/my-app-1.0-SNAPSHOT.war:/usr/local/tomcat/webapps/myapp.war -it -p 8080:8080 tomcat
This will copy the war file to webapps directory and get your app running in no time.
Tomcat will only extract the war which is copied to webapps directory.
Change Dockerfile as below:
FROM tomcat:8.0.20-jre8
COPY /1.0-SNAPSHOT/my-app-1.0-SNAPSHOT.war /usr/local/tomcat/webapps/myapp.war
You might need to access the url as below unless you have specified the webroot
http://192.168.59.103:8888/myapp/getData