Build & Deploy a Spring Boot application in Docker container

Dhruv Saksena
3 min readApr 26, 2021

It’s best to learn technology by example. In this article, we will be deploying a sample Spring Boot based application on a docker container.

Our application is a fairly normal Spring Boot app exposing a REST api

package com.example.demo;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}

DockerMessageController.java

package com.example.demo.controller;import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DockerMessageController
{

@GetMapping("/messages")
public String getMessages()
{
return "hello";
}

}

Now, we will create a DockerFile script and place it in the root folder

Now, we will create the DockerFile in step by step manner-

  1. Get a ubuntu container

We will create a Ubuntu container as our base image and update it-

FROM ubuntu:16.04
RUN apt-get update
RUN apt-get -y install software-properties-common

2. Install JAVA-8 onto the system-

RUN apt-get update && \
apt-get install -y openjdk-8-jdk && \
apt-get install -y ant && \
apt-get clean && \
rm -rf /var/lib/apt/lists/* && \
rm -rf /var/cache/oracle-jdk8-installer;
RUN apt-get update && \
apt-get install -y ca-certificates-java && \
apt-get clean && \
update-ca-certificates -f && \
rm -rf /var/lib/apt/lists/* && \
rm -rf /var/cache/oracle-jdk8-installer;
ENV JAVA_HOME /usr/lib/jvm/java-8-openjdk-amd64/
RUN export JAVA_HOME

3. Install Maven

RUN apt-get update && \
apt-get install -y curl && \
apt-get install -y tar && \
apt-get install -y bash;
ARG MAVEN_VERSION=3.3.9
ARG USER_HOME_DIR="/root"
RUN mkdir -p /usr/share/maven && \
curl -fsSL http://apache.osuosl.org/maven/maven-3/$MAVEN_VERSION/binaries/apache-maven-$MAVEN_VERSION-bin.tar.gz | tar -xzC /usr/share/maven --strip-components=1 && \ln -s /usr/share/maven/bin/mvn /usr/bin/mvn
ENV MAVEN_HOME /usr/share/maven
ENV MAVEN_CONFIG "$USER_HOME_DIR/.m2"
ENV MAVEN_OPTS="-XX:+TieredCompilation -XX:TieredStopAtLevel=1"

4. Copy source code into the container folder

Here, we are creating a folder in /usr/local/app to contain our source code-

RUN mkdir /usr/local/app
ADD src /usr/local/app/src
ADD pom.xml /usr/local/app

Now, we have created our Dockerfile-

Go to the docker-app folder and execute the below command to create a Docker Image

docker build .

Goto folder /usr/local/app in the container and execute the command

mvn clean install

This will create the executable jar file in the container.

Now, to execute the jar file into the container, we need to add the following command-

WORKDIR /usr/local/app/target
ENTRYPOINT ["java","-jar","demo-0.0.1-SNAPSHOT.jar"]

Create Docker container from image with below command and your app is up and running.

docker run -i -t <<image-id>> bash

--

--