Build a simple Spring Boot app with Docker

In this tutorial we will building a simple Spring Boot application which will run inside a docker container.

Required Software

  • docker (1.6.0 or above)
  • jdk 1.8
  • Gradle 2.3+ or Maven 3.0+

Install the required software

$ sudo add-apt-repository ppa:webupd8team/java
$ sudo apt-get update
$ sudo apt-get install oracle-java8-installer
$ java -version
java version "1.8.0_45"
Java(TM) SE Runtime Environment (build 1.8.0_45-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)
$ sudo apt-get install gradle
$ sudo apt-get install maven

Setup a Spring Boot App

The following project can be cloned from official git repository in order to run the basic example of spring boot application inside a container.

$git clone https://github.com/spring-guides/gs-spring-boot-docker.git

Spring Boot application

The java file can be found at following location;

src/main/java/hello/Application.java

package hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class Application {

        @RequestMapping("/")
        public String home() {
                return "Hello Docker World";
        }


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

}

The class is flagged as a @SpringBootApplication and as a @RestController, meaning it’s ready for use by Spring MVC to handle web requests. @RequestMapping maps / to the home() method which just sends a response back. The main() method uses Spring Boot’s SpringApplication.run() method to launch an application.

Containerize the Spring Boot application

We will containerize the above application using the DockerFile. The project JAR file is added to the container as “app.jar” and then executed in the ENTRYPOINT.We have added a VOLUME pointing to /tmp because it is where a Spring Boot application creates working directories for Tomcat by default.

Dockerfile

This file is needed to create a docker image;

FROM java:8
VOLUME /tmp
ADD gs-spring-boot-docker-0.1.0.jar app.jar
RUN bash -c 'touch /app.jar'
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

Build a Docker Image with Maven

The maven supports the docker plugin in order to build the docker image. The following three configurations are required in building the docker image either with maven or gradle.

  • The image name (or tag)
  • The directory in which to find the Dockerfile
  • The resources (files) to copy from the target directory to the docker build (alongside the Dockerfile) - we only need the jar file in this example

The maven pom.xml should contain the following configuration;

pom.xml

<properties>
        <docker.image.prefix>springio</docker.image.prefix>
</properties>
<build>
    <plugins>
        <plugin>
            <groupId>com.spotify</groupId>
            <artifactId>docker-maven-plugin</artifactId>
            <version>0.2.3</version>
            <configuration>
                <imageName>${docker.image.prefix}/${project.artifactId}</imageName>
                <dockerDirectory>src/main/docker</dockerDirectory>
                <resources>
                    <resource>
                        <targetPath>/</targetPath>
                        <directory>${project.build.directory}</directory>
                        <include>${project.build.finalName}.jar</include>
                    </resource>
                </resources>
            </configuration>
        </plugin>
    </plugins>
</build>

Using the maven build the docker image

$ mvn package docker:build

Build a Dokcer image with Gradle

Even the gradle provides the plugin to build the docker image. The above mentioned 3 configurations are required to be provided in the build.gradle file

build.gradle

buildscript {
    ...
    dependencies {
        ...
        classpath('se.transmode.gradle:gradle-docker:1.2')
    }
}

group = 'springio'

...
apply plugin: 'docker'

task buildDocker(type: Docker, dependsOn: build) {
  push = true
  applicationName = jar.baseName
  dockerfile = file('src/main/docker/Dockerfile')
  doFirst {
    copy {
      from jar
      into stageDir
    }
  }
}

Using the gradle build the docker image

$ ./gradlew build buildDocker

Run the Spring Boot App container

Now just execute the following command in order to run the containerized application;

$ docker images
REPOSITORY                        TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
gregturn/gs-spring-boot-docker    latest              3e70f57df702        21 hours ago        841.4 MB
$ docker run -p 8080:8080 -t gregturn/gs-spring-boot-docker

The application is now available and can be accessed at the address http://localhost:8080.