How to Dockerize Springboot Web App

0

This blog represents instructions and code samples on how to Dockerize a Springboot Web app. The source code be found from this git repository.

Following are key aspects of this blog:

  • Dockerfile & image
  • Install the image
  • Run the container and access the app
Dockerfile & Image

Download the code from this page and put them all in a folder. Before we go one to create the image, lets know a little more about the Dockerfile. Following points need to be noted in the Dockerfile:

  • The image created is a Centos6 image
  • The Java is installed in a predefined path “/opt/jdk”
  • Then, Maven is installed and JAVA_HOMEĀ is set.
  • The source file is then copied to image.
FROM centos:centos6

# Setup Java
RUN mkdir /opt/jdk
RUN cd /opt

RUN yum -y install wget tar

RUN wget --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u5-b13/jdk-8u5-linux-x64.tar.gz

RUN tar -zxf jdk-8u5-linux-x64.tar.gz -C /opt/jdk

RUN update-alternatives --install /usr/bin/java java /opt/jdk/jdk1.8.0_05/bin/java 100
RUN update-alternatives --install /usr/bin/javac javac /opt/jdk/jdk1.8.0_05/bin/javac 100

# Setup Maven

WORKDIR /
RUN wget http://mirror.olnevhost.net/pub/apache/maven/maven-3/3.0.5/binaries/apache-maven-3.0.5-bin.tar.gz
RUN tar xvf apache-maven-3.0.5-bin.tar.gz

RUN mv apache-maven-3.0.5  /usr/local/apache-maven
ENV M2_HOME=/usr/local/apache-maven
ENV M2=$M2_HOME/bin
ENV PATH=$M2:$PATH
ENV PATH=$PATH:/usr/bin

ENV JAVA_HOME=/opt/jdk/jdk1.8.0_05

# Copy Source files

RUN mkdir src
ADD src /src
ADD pom.xml /

RUN mvn package

 

Install the Image

Following command is used to install the image:

docker build -f springboot.df -t springboot .

 

Run the container and Access the App

Following command can be used to start the container.

docker run -ti -d -p 8080:8080 --name demoapp -v /c/Users:/mnt springboot:latest /bin/bash

Once the container is started, access the container using following command:

docker attach demoapp

Go to “/” folder and execute following command. This would start the app on 8080 port. You could then access the app using the following URL: http://192.168.99.100:8080/. It will display “Hello World”

java -jar target/SpringBootSampleWeb-0.0.1.jar
Ajitesh Kumar

Ajitesh Kumar

Ajitesh has been recently working in the area of AI and machine learning. Currently, his research area includes Safe & Quality AI. In addition, he is also passionate about various different technologies including programming languages such as Java/JEE, Javascript and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data etc.

He has also authored the book, Building Web Apps with Spring 5 and Angular.
Ajitesh Kumar

Leave A Reply

Time limit is exhausted. Please reload the CAPTCHA.