0% found this document useful (0 votes)
542 views

Spring Boot With Docker Container

The document discusses deploying a Spring Boot application in a Docker container. It provides steps to create a Spring Boot project, configure the docker-maven-plugin, build a Docker image with Maven, and run the Docker container exposing port 8080. The sample application is a simple web controller that returns a success message.

Uploaded by

ksrinivas9999
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
542 views

Spring Boot With Docker Container

The document discusses deploying a Spring Boot application in a Docker container. It provides steps to create a Spring Boot project, configure the docker-maven-plugin, build a Docker image with Maven, and run the Docker container exposing port 8080. The sample application is a simple web controller that returns a success message.

Uploaded by

ksrinivas9999
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Spring Boot with Docker Container

Docker is a new revolution for Linux container and it is a powerful tool for
MicroService development.
The tutorial will guild you how to build a Spring Boot application and deploy it
with Docker container.

Contents [hide]
 I. Technology with Spring Boot Docker tutorial
 II. Overview Spring Boot with Docker
 1. Structure of Project
 2. Step to do
 III. Practices
 1. Create Spring Boot project
 2. Configure Docker-Maven-Plugin
 3. Create a Simple Web Controller
 4. Build Spring Boot project with Docker
 5. Run docker container
 6. Result
 IV. Source code

I. Technology with Spring Boot Docker tutorial


Java – 8
Maven 3.3.9
Editor: Spring Tool Suite – Version 3.7.3.RELEASE
Docker Container

II. Overview Spring Boot with Docker

1. Structure of Project
2. Step to do

– Create Spring Boot Project


– Configure docker-maven-plugin
– Create a Simple Web Controller
– Build Spring Boot with Docker
– Run Docker container
– Result.

III. Practices

1. Create Spring Boot project

– Open Spring Too Suite, File->New->Spring Starter Project, input project


info:
Press Next button, add Spring MVC Web dependency:

Press Finish, Spring Boot Project is created. Open pom.xml file for details:

1<dependency>
2<groupId>org.springframework.boot</groupId>
3<artifactId>spring-boot-starter-web</artifactId>
4</dependency>

2. Configure Docker-Maven-Plugin

– Review project info in pom.xml file for info to build Dockerfile


1<groupId>com.springjava4dev.docker</groupId>
2<artifactId>sample-docker</artifactId>
3<version>0.0.1</version>
4<packaging>jar</packaging>
– Build Dockerfile

1FROM frolvlad/alpine-oraclejdk8:slim
2ADD sample-docker-0.0.1.jar app.jar
3ENTRYPOINT ["java","-jar","/app.jar"]
Place it at /src/main folder of Project

– Add properties in pom.xml

1<properties>
2<docker.image.prefix>spring-java4dev</docker.image.prefix>
3</properties>
– Add Docker Maven Plugin

1 <build>
2 <plugins>
3 <plugin>
4 <groupId>com.spotify</groupId>
5 <artifactId>docker-maven-plugin</artifactId>
6 <version>0.4.10</version>
7 <configuration>
8 <mainClass>com.springjava4dev.docker.SpringbootDockerStarterApplication
9 </mainClass>
1 <imageName>${docker.image.prefix}/${project.artifactId}</imageName>
0 <dockerDirectory>src/main</dockerDirectory>
1 <resources>
1 <resource>
1 <directory>${project.build.directory}</directory>
2 <include>${project.build.finalName}.jar</include>
1 </resource>
3 </resources>
1 </configuration>
4 </plugin>
1 <plugin>
5 <groupId>org.springframework.boot</groupId>
1 <artifactId>spring-boot-maven-plugin</artifactId>
6 </plugin>
1 </plugins>
7 </build>
1
8
1
9
2
0
2
1
2
2
2
3
2
4

3. Create a Simple Web Controller

1 import org.apache.log4j.LogManager;
2 import org.apache.log4j.Logger;
3 import org.springframework.web.bind.annotation.RequestMapping;
4 import org.springframework.web.bind.annotation.RestController;
5
6 @RestController
7 public class SimpleController {
8
9 private Logger logger = LogManager.getLogger(this.getClass());
10@RequestMapping(value="/")
11public String home(){
12String info ="Congratulations! You had delpoy your app on Docker
13successfully!";
14logger.info(info);
15return info;
16}
}

4. Build Spring Boot project with Docker

Set goals for Maven build:


package docker:build

Log when building Spring Boot with Docker


1 Step 1 : FROM frolvlad/alpine-oraclejdk8:slim
2 ---> 3f6e317fc0fb
3 Step 2 : ADD sample-docker-0.0.1.jar app.jar
4 ---> 2868a381cbad
5 Removing intermediate container 8259d8e43567
6 Step 3 : ENTRYPOINT java -jar /app.jar
7 ---> Running in fede972b5fb6
8 ---> e25968b9eae0
9 Removing intermediate container fede972b5fb6
10Successfully built e25968b9eae0
– Check Docker images:

5. Run docker container

Run docker commandline

1docker run -p 8080:8080 -t spring-java4dev/sample-docker

6. Result
Log from container:

IV. Source code


Spring Boot with Docker

You might also like