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“
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-maven–plugin</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:
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;
@SpringBootApplication
@RestController
public class Application {
public static void main(String
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):
The docker file below:
- Defines the java version.
- Defines the port where the container listens at runtime
- ADD <src> <dest> copies the files from <src> and adds them to the filesystem of the container at the path <dest>.
- An ENTRYPOINT allows you to configure a container that will run as an executable.
- 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:
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: