Where to find produced Docker image - java

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.

Related

What is the general way to deploy jetty application in production?

I just take over a project which is a java servlet application, I just figured out the way running the application is mvn jetty:run by using history. The previous only developer just quit suddenly, and there's no document. I have to create production server and deploy the application.
I have no previous Java experiences, so have not idea how to do it. Should I install nginx or apache and run the application like PHP? Or I just do something like nodejs ?
Jetty is a Java Servlet container/server (with many more features).
There's no need for another server.
You have a few choices on how to start Jetty.
Standalone server using the jetty-home (or the older jetty-distribution) tarballs.
In this mode you unpack the jetty-home tarball (this will become the ${jetty.home} directory), create a new directory for your configuration / deployment (this will become the ${jetty.base} directory), and then run Jetty against this ${jetty.base} directory.
There are many many examples online of how to do this.
From the official Jetty documentation at https://www.eclipse.org/jetty/documentation/current/
To various examples here on stackoverflow.
# Grab the tarball
[~]$ curl -O https://repo1.maven.org/maven2/org/eclipse/jetty/jetty-home/9.4.30.v20200611/jetty-home-9.4.30.v20200611.tar.gz
# Unpack the tarball
[~]$ tar -zxvf jetty-home-9.4.30.v20200611.tar.gz
# Make a {jetty.base} directory to house your configuration
[~]$ mkdir myappbase
[~]$ cd myappbase
# Since this is a new {jetty.base}, lets initialize it with a
# few common modules
[myappbase]$ java -jar ../jetty-home-9.4.30.v20200611/start.jar \
--add-to-start=http,deploy
INFO : webapp transitively enabled, ini template available with --add-to-start=webapp
INFO : server transitively enabled, ini template available with --add-to-start=server
INFO : security transitively enabled
INFO : servlet transitively enabled
INFO : http initialized in ${jetty.base}/start.ini
...(snip)...
INFO : deploy initialized in ${jetty.base}/start.ini
MKDIR : ${jetty.base}/webapps
INFO : Base directory was modified
# Lets see what we have now
[myappbase]$ ls -F
start.ini webapps/
# Copy your webapp into place
[myappbase]$ cp ~/Projects/mywebapp.war webapps/
# See this Jetty Configuration
[myappbase]$ java -jar ../jetty-home-9.4.30.v20200611/start.jar --list-config
# Run Jetty
[myappbase]$ java -jar ../jetty-home-9.4.30.v20200611/start.jar
Embedded Jetty
This means you have initialized everything entirely in code, from the Server object, to the ServerConnector, to the WebAppContext (or the ServletContextHandler) and are issuing a Server.start(); to make the Server execute.
This is probably the more common usage of Jetty, we find far more users on this approach then the standalone approach.
For development using the jetty-maven-plugin
This is what you have discovered already.
In a maven project with <packaging>war</packaging> you can execute the various jetty-maven-plugin goals to do various things with that maven project during development time.
NOTE: This mode is not suitable for production!
jetty:run is the jetty-maven-plugin and the run goal.
See: https://www.eclipse.org/jetty/documentation/current/jetty-maven-plugin.html
Alternate environments where Jetty is embedded.
This mode is seeing increasing use, and if your application is using libraries like Spark or SpringBoot then you are essentially using Jetty under the covers, and have no need for a separate server installation / configuration, it's all done within the API/SDK of that library.

Error loading shared library test/libaegean.dll: No such file or directory

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

running war from command line without maven jetty

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"]

How to link custom script to spring boot jar?

I have created a symlink for a spring boot executable jar and i am able to start the application. I want to customize the logs path, pid folder etc.
Went through the customizing the startup of this script.
But I could not find where to store the custom script and how it can be linked to executable application jar. Could you please assist?
Create a script with name your-app.service, place this script in /etc/systemd/system directory.
Installation as a systemd Service, using Java System Properties (VM Arguments):
[Unit]
Description= Spring Boot App
After=syslog.target
[Service]
User=myapp
ExecStart=java -Dspring.application.name=example -Dlogging.file=/opt/spring-boot-app/log/app.log -Dspring.pid.file=/opt/spring-boot-app/app.pid -jar /opt/spring-boot-app/app.jar
SuccessExitStatus=200
[Install]
WantedBy=multi-user.target
Or pass throught via program arguments:
java -jar /opt/spring-boot-app/app.jar --spring.application.name=example --logging.file=/opt/spring-boot-app/log/app.log --spring.pid.file=/opt/spring-boot-app/app.pid
References in here and here.
logging.file= # Log file name (for instance, myapp.log). Names can
be an exact location or relative to the current directory.
spring.pid.file= # Location of the PID file to write (if
ApplicationPidFileWriter is used).

Deploying Java webapp to Tomcat 8 running in Docker container

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

Categories