Running a Spring Boot application with Docker

In this tutorial we will learn how to run a containerized Spring Boot application using  Docker.

In order to complete this tutorial, you need to complete the following requirements:

1) Install Docker on your Machine

2) Install Spring Boot Command Line interface

Create a simple Spring Boot REST Project

Start by creating a new project which includes the web dependencies:

  spring init -dweb demo-rest 

Now import the Maven project in your favourite IDE and add a model class named Customer that will be returned by a GET Request:

package com.example.demorest;

public class Customer {
  private int id;
  private String name;

  public Customer(int id, String name) {
    super(); = id; = name;

  public String getName() {
    return name;

  public void setName(String name) { = name;

  public int getId() {
    return id;

  public void setId(int id) { = id;

All you need to build a Rest Service in Spring Boot is a RestController:

package com.example.demorest;

import java.util.ArrayList;
import java.util.List;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

public class CustomerController {
  public List<Customer> findAll() {
    List<Customer> customerList = new ArrayList<Customer>();
    customerList.add(new Customer(1, "frank"));
    customerList.add(new Customer(2, "john"));
    return customerList;

RestController : The @RestController annotation provides a combination of @ResponseBody and @Controller annotations. This is typically used to create REST Controllers.

RequestMapping: The method with @RequestMapping would handle a generic request to the “/” URI. If not specified, it maps a GET Request.

Now compile and package the application with:

$ mvn clean install 

Now you can run the application in any Spring Boot available way, f.e.

mvn spring-boot:run 

If you surf on http://localhost:8080, the list of Customer in JSON format will be displayed:

Here is an Hello World tutorial to create a simple REST Application using Spring Boot

Running the Spring Boot application on Docker

As Spring Boot JAR file can be executed on any environment, we will use the openjdk image which is an open-source implementation of the Java Platform, Standard Edition. Create a file named Dockerfile and include the following content in it:

FROM openjdk:8-jdk-alpine



ARG JAR_FILE=target/demo-docker-0.0.1-SNAPSHOT.jar

ADD ${JAR_FILE} demo-docker-0.0.1-SNAPSHOT.jar

ENTRYPOINT ["java","","-jar","/demo-docker-0.0.1-SNAPSHOT.jar"]

This Dockerfile is quite simple: let’s review it:

  • FROM: As said, weuse the openjdk:8-jdk-alpine image as our base image.

  • VOLUME: Volumes are used to persist data generated by the container on the Host, and share directories from the Host OS with the container.

    The VOLUME instruction creates a mount point on the container with the specified path. When you run the container, you can specify the directory on the Hot OS to which the given mount point will be mapped to. After that, anything that the container writes to the mounted path is written to the mapped directory on the Host OS.

    In the above Dockerfile, we created a mount point with path /tmp because this is where the spring boot application creates working directories for Tomcat by default.

  • EXPOSE: This instruction allows you to expose a certain port to the outside world.

  • ARG: The ARG instruction defines a variable with a default value. You can override the default value of the variable by passing it at build time.

    ARG <name>[=<default value>] 

    Once defined, the variable can be used by the instructions following it.

  • ADD: The ADD instruction is used to copy new files and directories to the docker image.

  • ENTRYPOINT: This is where you configure how the application is executed inside the container.

Save the Dockerfile in the current directory. Now let’s build and tag the application with:

docker build -t docker-demo .

Now you can finally run it with:

docker run -p 8080:8080 docker-demo

The application will start on port 8080:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 :: Spring Boot ::        (v2.1.8.RELEASE)

2019-09-17 13:33:39.308  INFO 1 --- [           main] com.example.demodocker.DemoApplication   : Starting DemoApplication v0.0.1-SNAPSHOT on a514397467a2 with PID 1 (/demo-docker-0.0.1-SNAPSHOT.jar started by root in /)
2019-09-17 13:33:39.313  INFO 1 --- [           main] com.example.demodocker.DemoApplication   : No active profile set, falling back to default profiles: default
2019-09-17 13:33:40.374  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2019-09-17 13:33:40.412  INFO 1 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-09-17 13:33:40.413  INFO 1 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.24]
2019-09-17 13:33:40.506  INFO 1 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-09-17 13:33:40.506  INFO 1 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1136 ms
2019-09-17 13:33:40.730  INFO 1 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-09-17 13:33:40.919  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context

Great! We just managed to run our first Spring Boot application on Docker!

If you want to learn how to get your Dockerfile and image automatically created for you, then check this tutorial: How to build Docker images of your Spring Boot applications

If you want to learn how to deploy your Spring boot application on OpenShift check : How to run a Spring Boot application on Openshift.