Steps to deploy Spring Boot project using Docker

Steps to deploy Spring Boot project using Docker

Create a simple springboot project

1. Use Spring Boot 2.2.10 related dependencies in pom.xml

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.10.RELEASE</version>
</parent>

2. Add web and test dependencies

<dependencies>
     <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
 <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-test</artifactId>
  <scope>test</scope>
 </dependency>
</dependencies>

3. Create a DockerController with a hello() method in it, which returns: hello,nihao when accessed

@RestController
public class DockerController {
 
    @RequestMapping("/hello")
    public String hello() {
        return "hello,nihao";
    }
}

4. Startup Class

@SpringBootApplication
public class DockerApplication {

 public static void main(String[] args) {
  SpringApplication.run(DockerApplication.class, args);
 }
}

After adding, start the project. After successful startup, visit http://localhost:8080/hello in the browser. The page returns: hello,nihao, indicating that the Spring Boot project is configured normally.

Deploy Spring Boot project using Docker

1. Package the project into a jar package, copy it to the server, and test it

[root@jiangwang springbootDemo]# ls
demo-0.0.1-SNAPSHOT.jar Dockerfile
[root@jiangwang springbootDemo]# java -jar demo-0.0.1-SNAPSHOT.jar 

  . ____ _ __ _ _
 /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/ ___)| |_)| | | | | || (_| | ) ) ) )
  ' |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|===============|___/=/_/_/_/
 :: Spring Boot :: (v2.2.10.RELEASE)

2021-03-18 14:49:18.241 INFO 12886 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication v0.0.1-SNAPSHOT on jiangwang with PID 12886 (/home/springbootDemo/demo-0.0.1-SNAPSHOT.jar started by root in /home/springbootDemo)
2021-03-18 14:49:18.244 INFO 12886 --- [ main] com.example.demo.DemoApplication : No active profile set, falling back to default profiles: default
2021-03-18 14:49:19.924 INFO 12886 --- [ main] osbwembedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2021-03-18 14:49:19.938 INFO 12886 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-03-18 14:49:19.938 INFO 12886 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.38]
2021-03-18 14:49:20.013 INFO 12886 --- [ main] oaccC[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-03-18 14:49:20.014 INFO 12886 --- [ main] wscServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1657 ms
2021-03-18 14:49:20.321 INFO 12886 --- [ main] ossconcurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2021-03-18 14:49:20.520 INFO 12886 --- [ main] osbwembedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2021-03-18 14:49:20.523 INFO 12886 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 2.899 seconds (JVM running for 3.369)

2. After seeing the Spring Boot startup log, it shows that there is no problem with the environment configuration. Edit the Dockerfile file:

FROM java:8
COPY *.jar /app.jar

CMD ["--server.port=8080"]

EXPOSE 8080

ENTRYPOINT ["java","-jar","/app.jar"]

3. Next we use Dockerfile to build the image:

## Build the image [root@jiangwang springbootDemo]# docker build -t springboot-demo .
Sending build context to Docker daemon 17.72MB
Step 1/5: FROM java:8
 ---> d23bdf5b1b1b
Step 2/5 : COPY *.jar /app.jar
 ---> f4d6aeabd3f0
Step 3/5 : CMD ["--server.port=8080"]
 ---> Running in a6311f7cf7b5
Removing intermediate container a6311f7cf7b5
 ---> d8117b10cefa
Step 4/5: EXPOSE 8080
 ---> Running in ae180be637bb
Removing intermediate container ae180be637bb
 ---> f16702c75ab6
Step 5/5 : ENTRYPOINT ["java","-jar","/app.jar"]
 ---> Running in fafa00625666
Removing intermediate container fafa00625666
 ---> d4c3e225699d
Successfully built d4c3e225699d
Successfully tagged springboot-demo:latest

4. Run the image:

# Run the image [root@jiangwang springbootDemo]# docker run -d -p 39005:8080 --name my-springboot springboot-demo
7ac35852cb91cb10612cd28fdbe7c50c7c59df4cccf19b2f1d30dcabbfe501f4
[root@jiangwang springbootDemo]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7ac35852cb91 springboot-demo "java -jar /app.jar …" 33 seconds ago Up 32 seconds 0.0.0.0:39005->8080/tcp my-springboot
[root@jiangwang springbootDemo]# curl localhost:39005/hello
hello,nihao[root@jiangwang springbootDemo]# 

5. Enter the external network URL in the browser to visit:

Here your external network port 39005 must be opened first, you can go to the security group settings

This shows that the Spring Boot project was successfully deployed using Docker!

This concludes this article on the implementation steps of deploying Spring Boot projects using Docker. For more information about deploying Spring Boot with Docker, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • How to deploy SpringBoot project using Dockerfile
  • Example of deploying Spring Boot application using Docker
  • How to deploy SpringBoot project using Docker
  • How to deploy spring-boot maven application using Docker
  • Deploy the springboot project to docker based on idea
  • Deploy springBoot project to Docker on Mac (demo)
  • How to deploy microservices using Spring Boot and Docker

<<:  Rainbow button style made with CSS3

>>:  Website front-end performance optimization: JavaScript and CSS

Recommend

MySQL master-slave configuration study notes

● I was planning to buy some cloud data to provid...

Vue implements multi-grid input box on mobile terminal

Recently, the company has put forward a requireme...

JavaScript to achieve a simple message board case

Use Javascript to implement a message board examp...

Sample code for making a drop-down menu using pure CSS

Introduction: When I looked at interview question...

Detailed explanation of the steps to create a web server with node.js

Preface It is very simple to create a server in n...

How to solve the element movement caused by hover-generated border

Preface Sometimes when hover pseudo-class adds a ...

25 CSS frameworks, tools, software and templates shared

Sprite Cow download CSS Lint download Prefixr dow...

How to implement the Vue mouse wheel scrolling switching routing effect

A root routing component (the root routing compon...

Understanding flex-grow, flex-shrink, flex-basis and nine-grid layout

1. flex-grow, flex-shrink, flex-basis properties ...