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:

docker-_1

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

Finally define the name of the project and click Finish:

docker-_2

The project with the following structure is created:

docker-_3

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“

xmlns:xsi=„http://www.w3.org/2001/XMLSchema-instance“

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

<modelVersion>4.0.0</modelVersion>

<groupId>com.example</groupId>

<artifactId>SpringBootWithDocker</artifactId>

<packaging>jar</packaging>

<version>0.0.1-SNAPSHOT</version>

<name>SpringBootWithDocker Maven Webapp</name>

<url>http://maven.apache.org</url>

<properties>

<java.version>1.8</java.version>

</properties>

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>1.4.1.RELEASE</version>

</parent>

<dependencies>

<dependency>

<groupId>org.springframework.boot</groupId>

          <artifactId>spring-boot-starter-web</artifactId>

</dependency>

</dependencies>

<build>

<plugins>

<plugin>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-mavenplugin</artifactId>

</plugin>

</plugins>

<finalName>SpringBootWithDocker</finalName>

</build>

</project>

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:

docker-_4

Right-click on the created package > New > Class

docker-_5

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;

@SpringBootApplication
@RestController
public class Application {

public static void main(String[] args) {

SpringApplication.run(Application.class, args);

}

@RequestMapping(value=„/“)

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):

docker-_6

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

EXPOSE 8080

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:

docker-_7

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:

docker-_8