Docker Maven Tomcat

 

Jul 30, 2020 In the end, Pushing a Java Tomcat application inside Docker is just a matter of the Docker file. In this Java Docker Tutorial, we saw how to do that by going step-by-step on our Dockerfile. For your convenience, check the entire Dockerfile with comments on GitHub.com. I am trying to write dockerfile with complete installation of git, maven, java, apache tomcat, Jenkins war file & Jenkins & assign path variables for maven, java May 1, 2020 in Docker by anonymous. 120 points. 951 views. Jun 20, 2018 A tomcat Docker image with the WAR file will be built The Docker image will be launched locally and will expose port 8080 Maven will wait until the container is actually up and can serve requests Integration tests will run and will hit localhost:8080.

In Docker, one of the main issues is the size of the final image. It’s not uncommon to end up with images over 1 GB even for simple Java applications.

Since version 17.05 of Docker, it’s possible to have multiple builds in a single Dockerfile, and to access the output of the previous build into the current one.

Those are called multi-stage builds. The final image will be based on the last build stage.

Now, we will create two docker images: one without multi-stage builds and another with multi-stage builds and compare the size of both.

Let’s consider one maven project, which is hosted on GitHub and we have to build that project and deploy it on Tomcat server.

1.So, First we will install JAVA as below
Run apt-get update
Run apt-get install -y openjdk-8-jdk wget
2. then Install Git and clone repository
#Install git
RUN apt-get install git -y
RUN git clone https://github.com/webmagicinformatica/SimpleMavenProject.git
RUN cp -r $PWD/SimpleMavenProject/* $PWD
3. Now, install maven and build maven project to create WAR file i.e webapp.war
#Install maven
RUN apt-get install maven -y
RUN mvn clean install package
4. Finally, we have to install tomcat and after configuration, copy WAR file into webapps folder and start service.
#Install apache tomcat
Run groupadd tomcat
Run useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat
Run cd /tmp
RUN wget https://archive.apache.org/dist/tomcat/tomcat-8/v8.5.35/bin/apache-tomcat-8.5.35.tar.gz
Run mkdir /opt/tomcat
Run tar xzvf apache-tomcat-8*tar.gz -C /opt/tomcat –strip-components=1
Run cd /opt/tomcat
Run chgrp -R tomcat /opt/tomcat
Run chmod -R g+r /opt/tomcat/conf
Run chmod g+x /opt/tomcat/conf
Run chown -R tomcat /opt/tomcat/webapps /opt/tomcat/work /opt/tomcat/temp
Run cd /opt/tomcat/bin
COPY ./webapp.war /opt/tomcat/webapps
Expose 8080
CMD /opt/tomcat/bin/catalina.sh run && tail -f /opt/tomcat/logs/catalina.out
Complete dockerfile will look like this:
FROM ubuntu:latest
RUN mkdir /app
WORKDIR /app
Run apt-get update
Run apt-get install -y openjdk-8-jdk wget
#Install git
RUN apt-get install git -y
RUN git clone https://github.com/webmagicinformatica/SimpleMavenProject.git
RUN cp -r $PWD/SimpleMavenProject/* $PWD
#Install maven
RUN apt-get install maven -y
RUN mvn clean install package
#Install apache tomcat
Run groupadd tomcat
Run useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat
Run cd /tmp
RUN wget https://archive.apache.org/dist/tomcat/tomcat-8/v8.5.35/bin/apache-tomcat-8.5.35.tar.gz
Run mkdir /opt/tomcat
Run tar xzvf apache-tomcat-8*tar.gz -C /opt/tomcat –strip-components=1
Run cd /opt/tomcat
Run chgrp -R tomcat /opt/tomcat
Run chmod -R g+r /opt/tomcat/conf
Run chmod g+x /opt/tomcat/conf
Run chown -R tomcat /opt/tomcat/webapps /opt/tomcat/work /opt/tomcat/temp
Run cd /opt/tomcat/bin
COPY ./webapp.war /opt/tomcat/webapps
Expose 8080
CMD /opt/tomcat/bin/catalina.sh run && tail -f /opt/tomcat/logs/catalina.out

To create multi stage build image, follow these steps:

1. First, create a directory by name “with_MSB” and a dockerfile in it and add the above content.
“mkdir /without_MSB”
“cd /without_MSB”
“vi Dockerfile”
2. Build image by using below command
3. To create Container, run below command
“docker run -it -p 8080:8080 -d –name maven_app app_image”
4. Now, you can check your application on browser
5. Check image size by using below command
“docker images grep app_image”

Image size will be more than 600MB

6. Stop container and remove it by using below command
Tomcat docker container

Now create docker image with multi-stage builds for same application

So build stages would be as follows:

1. Clone the Maven project repository from Git.
FROM alpine/git as clone
WORKDIR /app
RUN git clone https://github.com/webmagicinformatica/SimpleMavenProject.git
2. Copy the files from the previous stage and build the app with Maven.
FROM maven:3.5-jdk-8-alpine as build
WORKDIR /app
COPY –from=clone /app/SimpleMavenProject /app
RUN mvn clean install package
3. Copy the WAR file from the previous stage deploys it on Tomcat i.e in webapp folder.
FROM tomcat:8-jre8-alpine
COPY –from=build /app/webapp/target/webapp.war $CATALINA_HOME/webapps
EXPOSE 8080
Complete dockerfile will look like this

# Dockerfile

FROM alpine/git as clone
WORKDIR /app
RUN git clone https://github.com/webmagicinformatica/SimpleMavenProject.git
FROM maven:3.5-jdk-8-alpine as build
WORKDIR /app
COPY –from=clone /app/SimpleMavenProject /app
RUN mvn clean install package
FROM tomcat:8-jre8-alpine
#WORKDIR /app
COPY –from=build /app/webapp/target/webapp.war $CATALINA_HOME/webapps
EXPOSE 8080

To create multi-stage builds image,

1. First, create directory “with_MSB” and dockerfile in it and add the above content.
2. Build image by using below command
“docker build -t app_image_msb .”
3. To create Container run below command
“docker run -it -p 8080:8080 -d –name maven_app_msb app_image_msb”
4. Now you can check your application on browser
5. Check image size by using below command
“docker images grep app_image_msb”
Image size will be 6 times less than earlier size (without multi-stage build image)
6. Stop container and remove it by using below command

Conclusion:

We have created two images here, one without multi-stage i.e app_image and another one by using multi-stage i.e app_image_msb.

Docker maven tomcat login

We have noticed that app_image_msb size is 6 times lesser than app_image.

So by using multi-stage build we can reduce the size of the image.

Note: This tutorial requires you to run your app locally on your own computer

Pre-requisites

Getting Started

In IntelliJ, clone the repository. Click on Check out from Version Control > Github

If this the first time to use IntelliJ with Github, log into your Github account.

On the command line clone the docker/labs repository

Click on Import project from external model, select Maven. Click Next

Check Search for projects recursively. Click Next

Select the project and click Next

Select the JDK(set the JDK home path) and click Next

Click Finish

Click on Project View to open the project.

Building the application

The application is a basic Spring MVC application that receives user input from a form, writes the data to a database, and queries the database.

The application is built using Maven. To build the application click on icon on the bottom left of the IntelliJ window and select Maven Projects.

The Maven Projects window will open on the right side. Maven goals of clean and install need to be set to build the application.

Docker Maven Tomcat

To set the clean goal, click on Lifecycle to display the tree of goals. Right click on clean and select Create 'UserSignup [clean]'...

Click OK in the Create Run/Debug Configuration window.

Configure the install goal similarly. Click on install in the Lifecycle tree. Select Create 'UserSignup[install]'...

Click OK in the Create Run/Debug Configuration window.

To build the application run clean

Then run install

When the application builds, you will see a success message in the log window.

Running the application

Open a terminal and go to the application directory. Start the application with docker-compose

Docker will build the images for Apache Tomcat and MySQL then start the containers. It will also mount the application directory (./app/target/UserSignup) as a data volume on the host system to the Tomcat webapps directory in the web server container.

Open a browser window and go to:‘localhost:8080’; you should see the Tomcat home page

When the Tomcat image was built, the user roles were also configured. Click on the Manager App button to see the deployed applications. When prompted for username and password, enter system and manager respectively to log into the Tomcat Web Application Manager page.

You can use the Manager page to Start, Stop, Reload or Undeploy web applications.

To go to the application, Click on /UserSignup link.

Debugging the Application

In the application, click on Signup to create a new user. Fill out the registration form and click Submit

Click Yes to confirm.

Test out the login.

Oh no!

Configure Remote Debugging

Tomcat supports remote debugging the Java Platform Debugger Architecture (JPDA). Remote debugging was enabled when the tomcat image (registration-webserver) was built.

To configure remote debugging in IntelliJ, click on Run > Edit Configuration ...

Add a new remote configuration.

In the RunDebug Configurations window, set the Name of the configuration as docker tomcat and in Settings set the port to ‘8000’ as the default Tomcat JPDA debuging port. Click on OK to save the configuration.

Finding the Error

Since the problem is with the password, let’s see how the password is set in the User class. In the User class, the setter for password is scrambled using rot13 before it is saved to the database.

Try registering a new user using the debugger. In the menu click on Run > Debug...

Choose the remote Tomcat debug configuration. The Debugger console will be displayed at the bottom of the IntelliJ window.

Set a breakpoint in the User class where the password is set.

Register a new user with the username of ‘Moby’ and with ‘m0by’ as the password, click Submit, click yes

IntelliJ will display the code at the breakpoint and the value of password in the variables window. Note that the value is m0by

Click on Resume Program to let the code run or press F8 to step over the breakpoint.

Next, set a breakpoint on the getPassword in the User class to see the value returned for password. You can also toggle off the breakpoint for setPassword.

Try to log into the application. Look at the value for password in the debugging console, note that it is z0ol which is m0by using ROT13.

In this MVC application the UserController uses the findByLogin method in the UserServiceImpl class which uses the findByUsername method to retrieve the information from the database. It then checks to see if the password from the form matches the user password. Since the password from the login form is not scrambled using ROT13, it does not match the user password and you cannot log into the application.

To fix this, apply ROT13 to the password by adding

Set a breakpoint in UserServiceImpl on the findByLogin method. Log in again and look at the values for the breakpoint. The ‘passwd’ variable is z0ol which matches the password for the user moby.

Continue (F8) and you should successfully log in.

True or false: You have to restart a container after you make changes to the code or they won’t be reflected in the application

  • ( ) True
  • (x) False

How Do I Create A Docker Image With Java11, Tomcat 9 And ...

Docker tomcat tutorial

True or false: Debugging a Java app running in a container requires a special plugin for the IDE

Using Docker From Maven And Maven From Docker By Codefresh ...

  • ( ) True
  • (x) False