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

Spring Boot application

The java file can be found at following location;


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;

public class Application {

        public String home() {
                return "Hello Docker World";

        public static void main(String[] args) {
      , 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 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.


This file is needed to create a docker image;

FROM java:8
ADD gs-spring-boot-docker-0.1.0.jar app.jar
RUN bash -c 'touch /app.jar'
ENTRYPOINT ["java","","-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;



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


buildscript {
    dependencies {

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.