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

Several ways to implement CSS height changing with width ratio

[Solution 1: padding implementation] principle: I...

Summary of problems encountered when installing docker on win10 home version

Docker download address: http://get.daocloud.io/#...

CSS example code to hide the scroll bar and scroll the content

Preface When the HTML structure of a page contain...

Can asynchrony in JavaScript save await?

I knew before that to synchronously obtain the re...

Alibaba Cloud Server Domain Name Resolution Steps (Tutorial for Beginners)

For novices who have just started to build a webs...

A brief discussion on the lock range of MySQL next-key lock

Preface One day, I was suddenly asked about MySQL...

Detailed explanation of mysql user variables and set statement examples

Table of contents 1 Introduction to user variable...

What is the function of !-- -- in HTML page style?

Mainly for low version browsers <!-- --> is ...

Two implementation codes of Vue-router programmatic navigation

Two ways to navigate the page Declarative navigat...

Solution to MySQL root password error number 1045

Stop MySQL Service Windows can right-click My Com...

Talk about the 8 user instincts behind user experience in design

Editor's note: This article is contributed by...

Docker time zone issue and data migration issue

Latest solution: -v /usr/share/zoneinfo/Asia/Shan...

Pure CSS code to achieve drag effect

Table of contents 1. Drag effect example 2. CSS I...

Differences between MySQL CHAR and VARCHAR when storing and reading

Introduction Do you really know the difference be...

Detailed explanation of mktemp, a basic Linux command

mktemp Create temporary files or directories in a...