Professional Documents
Culture Documents
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
1. Structure of Project
2. Step to do
III. Practices
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
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
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
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}
}
6. Result
Log from container: