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
Related
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 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"]
I have seen lots of tutorials and tips around running tomcat on centos/ubuntu/linux.
I am using centos image in aws to run tomcat.
I did NOT do sudo yum install tomcat7*. This would have created a tomcat service account under with tomcat run will run. The reason I did not do this is because I need to copy (scp) the war file into webapps directory and I cannot scp directly as tomcat user.
Instead, I created a tomcat user with password.
I downloaded the tar.gz, unzipped and moved tomcat into /usr/share/tomcat7.
Following the instructions by this blog, I edited /etc/rc.d/init.d/tomcat as follows.
!/bin/sh
# Tomcat init script for Linux.
#
# chkconfig: 2345 96 14
# description: The Apache Tomcat servlet/JSP container.
JAVA_HOME=/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.79.x86_64
CATALINA_HOME=/usr/share/tomcat7
export JAVA_HOME CATALINA_HOME
exec $CATALINA_HOME/bin/catalina.sh $*
Then I executed the following commands
sudo chmod 755 /etc/rc.d/init.d/tomcat
sudo chkconfig --level 2345 tomcat on
I rebooted the machine and on startup tomcat is fired by root user.
I could also see the tomcat page showing corectly.
Now, I scp'ed my war into webapps directory (as tomcat user).
stopped the tomcat and restarted
sudo /etc/init.d/tomcat stop
sudo /etc/init.d/tomcat start
I get permission denied error when trying to write to catalina.out. This is because the catalina.out file is touched by root after centos reboot and now when I restarted tomcat as "tomcat" user, it does not have permission to write to it.
I changed the ownership on catalina.out (chown tomcat:tomcat catalina.out). After this change, I see tomcat starting and logging correctly.
Now the question is what is the right way to do it.
1) I will have jenkins jobs copying the war file to tomcat. so to restart the service, I need to sudo, which I cant from jenkins.
2) changing ownership of catalina.out is not right thing to do. if machine reboots, catalina.out is owned by tomcat and root cannot write to it. so there are problems of this catalina.out owned by either root or tomcat user.
Thanks for suggestions
I am trying to deploy an application WAR file using Jenkins ANT to build the WAR and restart the server.
Once the WAR is built, I have written a shell script to stop the application server, deploy the WAR file and then restart the application server.
#Shutdown Tomcat
ssh tomcat#<servername> "cd /home/tomcat/app/bin/;exec bash ./shutdown.sh"
#waiting period
ssh tomcat#<servername> "sleep 10"
ssh tomcat#<servername> "cd /home/tomcat/app/webapps/;rm -r *"
#Copy the WAR file to webapps
#Start the tomcat server
ssh tomcat#<servername> "cd /home/tomcat/app/bin/;exec bash ./startup.sh"
It does shutdown the server, clear some temporary files but fails to start the tomcat server. Any idea why this would be happening?
I have tried to see if there are other processes or ports still in use guessing maybe the shutdown was not clean. However there doesnt seem to be any issue like that. When I manually start the tomcat server for the application by going to the tomcat/bin directory, Tomcat starts without any issues.
Thanks in advance.