Containerize Your Spring Boot or WAS Liberty App

Meiyappan Kannappa
5 min readNov 7, 2018

In this story, I would like to provide a quick and maybe easy way to dockerize your Spring boot and WAS Liberty application.

For those who need to know about Docker, please refer to my story on medium and wikipedia.

To start with below are pre-requisites

  1. IDE to build your Spring boot/WAS Liberty App (IntellJ or Eclipse with gradle/maven)
  2. Docker runtime (Docker Toolbox for Windows < 10 and Docker for windows in Windows 10 with BIOS virtualization enabled)
  3. If you are using windows, you need Oracle VM Virtual box

Dockerize Spring Boot App

First create your Spring Boot App, for this story purpose I have created a simple Spring boot app with 2 REST APIs. Below is the controller code

@Component
@RestController
public class ProviderController {

@RequestMapping("/hello")
public String message(){

return "Hello World";
}

@RequestMapping("/ping")
public String ping(){

return "Hey I am from DOCKER";
}
}

And my Main class

@SpringBootApplication
public class SampleDockerBootApplication{

public static void main(String[] args) {
SpringApplication.run(SampleDockerBootApplication.class, args);
}
}

And then you need to generate a jar file out of your spring boot source code. For the same right click build.gradle and select create build. You will get pop up like below, fill in the information and click ok.

Then run the build configuration created above, from top of IntellJ

This will generate a jar file in your project build->libs folder.

Try executing the jar locally from command line using

java -jar JAR_FILE_NAME.jar

We are set for starting with docker now. For any application to get dockerized, we need to have a configuration file name “Dockerfile”. A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. For reference click here

Docker can build images automatically by reading the instructions from a Dockerfile

For our sample application the dockerfile looks like the below

# Base Image from which our application should start building, or inherit from.
# For each programming language and OS this will change. (Eg: For nodejs/go/python
# on ubuntu/centos). You have complete list in dockerhub.com
# I have used Java 8 from open jdk for our example
FROM openjdk:8

#Create a directory. This will create a directory in your container(Minature VM),
# which will be working directory and all your source code
# and runnables will be placed here
RUN mkdir /usr/src/docker-hello

# Make the new directory created above as working directory
WORKDIR /usr/src/docker-hello

# Copy the required files of your application, which are needed to # make application up and running. In our case its just the jar file
COPY /build/libs/pcf02-0.0.1-SNAPSHOT.jar /usr/src/docker-hello

# The Command that need to be executed when the container is made # running.
CMD ["java", "-jar","docker-hello-0.0.1-SNAPSHOT.jar"]

Once dockerfile is ready its time to build and create a docker image. Command to build docker image as below

$ docker build -t javasample/springboot-docker:v0.1 .

This will create a docker image with tag javasample/springboot-docker:v0.1.

To view the images that are built, issue the command

$ docker images

Alright, now we can run our container having java application , by executing the docker image. Make note of “-p 8080:8080”, this is port binding which will bind the container port to your host port.

$  docker run -p 8080:8080 javasample/springboot-docker:v0.1

Done!! Now your dockerized spring boot application is up and running. You can access it via http://192.168.99.101:8080/ping if your running in windows with docker toolbox or http://localhost:8080/ping.

Dockerize WAS-Liberty App

Most of the docker concept remains same of what we seen above. Except for the changes on what should go inside the docker image with WAS-Liberty app. In the spring boot app it was .jar , but with WAS liberty you need to pack a .zip file in to the docker image.

Prepare the application

  1. Download WAS liberty server from IBM Site
  2. Download Eclipse Oxygen or above
  3. Create a WAS Liberty server profile with name SERVER_NAME in Eclipse.
  4. Next for example purpose create application named hello-world and create a servlet as below
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class ProvideMessage
*/
@WebServlet(description = "Sample Message Provider Servlet", urlPatterns = { "/ProvideMessage" })
public class ProvideMessage extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ProvideMessage() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("Hey WAS Liberty in Docker");
out.close();
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}

Now you have your servlet, We need build our WAR and EAR file.

# Base Image from which our application should start building, or inherit from.# For each programming language and OS this will change. (Eg: For nodejs/go/python# on ubuntu/centos). You have complete list in dockerhub.com# For WAS liberty refer https://hub.docker.com/_/websphere-liberty/FROM websphere-liberty:webProfile8#Making Root userUSER root# copy the WAR and EAR files to the WAS Liberty Runtime.# apps and dropins are the respective folders. More info @#https://www.ibm.com/support/knowledgecenter/en/SSAW57_liberty/com.i#bm.websphere.wlp.nd.multiplatform.doc/ae/cwlp_about.htmlCOPY --chown=1001:0  /wlp/usr/servers/WAS-Liberty/apps/hello-worldEAR.ear /config/apps/COPY --chown=1001:0  /wlp/usr/servers/WAS-Liberty/dropins/helloworld.war /config/dropins/# CMD is not required, as we are not running any command to start #our application.# Once server started our application will be deployed in the #server. So we do not need any CMD commands

You are done, build your docker image and run it with below commands.

$ docker build -t java-was-web/was-web-liberty-example:v0.1 .
$ docker run -p 8080:8080 java-was-web/was-web-liberty-example:v0.1
$ docker container ls

WAS Liberty will startup as below

Now the application can be accessed at http://192.168.99.101:8080/helloworld/ProvideMessage

--

--

Meiyappan Kannappa

Technical enthusiast, daydreamer. Design of involute software architecture for digital transformation, sustainability and cost optimization.