Spring Boot with Docker

//Spring Boot with Docker

The article presents the basic configuration required to run an elementary Spring Boot application in a Docker container.

Create the Java Web Application as Maven project

Open Eclipse and create a new Maven Project:
File > New > Maven Project
In the next window you can define the location of the project:


Click Next and in the next window select maven-archetype-webapp.

Finally define the name of the project and click Finish:


The project with the following structure is created:


Define pom.xml

Use the following pom.xml. Note that the packaging of the application is jar and not war.

<project xmlns=„http://maven.apache.org/POM/4.0.0“


xsi:schemaLocation=„http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd“>






<name>SpringBootWithDocker Maven Webapp</name>


























Then right click on the project > Maven > Update Project > OK.

Create the Java class

Right-click on src/main/java > New > package and type in the following attributes:


Right-click on the created package > New > Class


package com.example;

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

public class Application {

public static void main(String

[] args) {

SpringApplication.run(Application.class, args);



public String Example() {

return „Hello Spring boot and Docker“;



Create the Dockerfile

Right-click on the project > New > File create the Dockerfile (without an extension):


The docker file below:

  1. Defines the java version.
  2. Defines the port where the container listens at runtime
  3. ADD <src> <dest> copies the files from <src> and adds them to the filesystem of the container at the path <dest>.
  4. An ENTRYPOINT allows you to configure a container that will run as an executable.
  5. Note that the name of the jar is defined in the pom.xml file in project.build.finalName XML element.


Dockerfile contents:

FROM java:8


ADD /target/SpringBootWithDocker.jar SpringBootWithDocker.js

ENTRYPOINT [„java“, „-jar“, „SpringBootWithDocker.jar“]

Create the docker container

Then you need to create the Docker container.

Open the console, navigate to the project’s folder and execute following commands:

mvn clean install

docker build -f Dockerfile -t springbootwithdocker .

In the docker command:

  • docker build builds docker images from a Dockerfile and a context. A build’s contet is the files located in the specified PATH or URL. For example the build here uses ADD to reference the required file in the context.
  • -f is used to specify the location of the Dockerfile
  • -t is used to tag the image (here the tag is: springbootwithdocker)

After build completes you can verify that the image is created by typing the command:

docker images

and you get the following result:


Run the application

docker run -p 8080:8080 springbootwithdocker

-p defines the port mapping in the following form: ip:hostPort:containerPort

You can now go to the port you defined and access your application:


By | 2017-06-20T15:01:34+00:00 21. November 2016|TECHtalk|0 Comments

About the Author:

Leave A Comment