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 8.0.20 installation and configuration method graphic tutorial

MySQL download and installation (version 8.0.20) ...

Nginx's practical method for solving cross-domain problems

Separate the front and back ends and use nginx to...

Detailed explanation of MySQL deadlock and database and table sharding issues

Record the problem points of MySQL production. Bu...

Mysql query database capacity method steps

Query the total size of all databases Here’s how:...

How to expand the disk size of a virtual machine

After Vmvare sets the disk size of the virtual ma...

Fixed a bug caused by scrollbar occupying space

background This bug was caused by滾動條占據空間. I check...

Example of using CSS3 to create Pikachu animated wallpaper

text OK, next it’s time to show the renderings. O...

CSS implements 0.5px lines to solve mobile compatibility issues (recommended)

【content】: 1. Use background-image gradient style...

Basic installation tutorial of mysql decompression package

Since I have changed to a new computer, all the e...

Analysis of the implementation process of three modes of VMWare network adapter

Three modes Bridged (bridge mode), NAT (network a...

Detailed explanation of fetch network request encapsulation example

export default ({ url, method = 'GET', da...

7 Best VSCode Extensions for Vue Developers

Adding the right VS Code extension to Visual Stud...