Docker deployment springboot project example analysis

Docker deployment springboot project example analysis

This article mainly introduces the example analysis of docker deployment springboot project. The example code is introduced in great detail in this article, which has certain reference value for everyone's study or work. Friends in need can refer to it.

Create a project

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.9.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>
  <groupId>com.topcheer</groupId>
  <artifactId>docker</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>docker</name>
  <description>Demo project for Spring Boot</description>
​
  <properties>
    <java.version>1.8</java.version>
    <docker.image.prefix>topcheer</docker.image.prefix>
  </properties>
​
  <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>
​
  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
       <plugin>
      <groupId>com.spotify</groupId>
      <artifactId>docker-maven-plugin</artifactId>
      <version>1.0.0</version>
      <configuration>
        <imageName>${docker.image.prefix}/${project.artifactId}</imageName>
        <dockerDirectory>src/main/docker</dockerDirectory>
        <resources>
          <resource>
            <targetPath>/</targetPath>
            <directory>${project.build.directory}</directory>
            <include>${project.build.finalName}.jar</include>
          </resource>
        </resources>
      </configuration>
    </plugin>
    </plugins>
  </build>
​
</project>

Startup Class

@SpringBootApplication
@Controller
public class DockerApplication {
  public static void main(String[] args) {
    SpringApplication.run(DockerApplication.class, args);
  }
  @RequestMapping("/user/find")
  @ResponseBody
  public Object findUser(){
    Map<String, String > map = new HashMap<>();
    map.put("name", "xdclass.net");
    map.put("age","28");
    return map;
  }
}

Since Harbor is not installed, the image cannot be pushed to the private image repository, so it is executed manually first.

mvn install, then put the dockerfile and jar package together

[root@topcheer docker]# ll
Total usage 16452
-rw-r--r-- 1 root root 168 Oct 28 14:24 Dockerfile
-rw-r--r-- 1 root root 16842487 Oct 28 14:17 docker.jar
[root@topcheer docker]# cat Dockerfile
FROM java:8
VOLUME /tmp
ADD docker.jar app.jar
RUN bash -c 'touch /app.jar'
EXPOSE 8080
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
[root@topcheer docker]#

Compile into image and start

[root@topcheer docker]# docker build -f Dockerfile -t boot .
Sending build context to Docker daemon 16.85 MB
Step 1/6: FROM java:8
 ---> d23bdf5b1b1b
Step 2/6: VOLUME /tmp
 ---> Running in 35037b5a7791
 ---> e96b96457c78
Removing intermediate container 35037b5a7791
Step 3/6 : ADD docker.jar app.jar
 ---> 06bcfdcff437
Removing intermediate container 2e2e5e559ae4
Step 4/6 : RUN bash -c 'touch /app.jar'
 ---> Running in 16441febc271
​
 ---> 1779caa23f77
Removing intermediate container 16441febc271
Step 5/6: EXPOSE 8080
 ---> Running in 14dd752ce247
 ---> 505044f5cdf8
Removing intermediate container 14dd752ce247
Step 6/6 : ENTRYPOINT java -Djava.security.egd=file:/dev/./urandom -jar /app.jar
 ---> Running in 832e4ca95dd2
 ---> 1cb7bd139478
Removing intermediate container 832e4ca95dd2
Successfully built 1cb7bd139478
[root@topcheer docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
boot latest 1cb7bd139478 About a minute ago 677 MB
registry.cn-hangzhou.aliyuncs.com/dalianpai/topcheer zipkin 17c2bb09f482 6 days ago 154 MB
docker.io/mysql latest c8ee894bd2bd 11 days ago 456 MB
elasticsearch latest 874179f19603 4 weeks ago 771 MB
docker.io/nacos/nacos-server latest a4229ac5cc19 4 weeks ago 710 MB
springbootdemo4docker latest cd13bc7f56a0 5 weeks ago 678 MB
docker.io/tomcat latest ee48881b3e82 6 weeks ago 506 MB
docker.io/rabbitmq latest a00bc560660a 6 weeks ago 147 MB
docker.io/centos-latest 67fa590cfc1c 2 months ago 202 MB
docker.io/redis latest f7302e4ab3a8 2 months ago 98.2 MB
docker.io/rabbitmq 3.7.16-management 3f92e6354d11 3 months ago 177 MB
docker.io/elasticsearch 6.8.0 d0b291d7093b 5 months ago 895 MB
docker.io/hello-world latest fce289e99eb9 10 months ago 1.84 kB
docker.io/java 8 d23bdf5b1b1b 2 years ago 643 MB
[root@topcheer docker]# docker run -d -p 8080:8080 boot
882ff5209aa2f40972a914b901750a50320faea65100b33e57b9c8a41533ca0b

test

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • How to deploy SpringBoot project using Dockerfile
  • How to deploy SpringBoot project using Docker
  • Detailed explanation of the docker deployment practice of the springboot project
  • Java remote one-click deployment of springboot to Docker through Idea
  • Detailed steps to deploy SpringBoot projects using Docker in Idea
  • Deploy the springboot project to docker based on idea
  • Detailed steps for one-click automated deployment of Linux+Docker+SpringBoot+IDEA
  • Implementation of Springboot packaging as Docker image and deployment
  • How to implement automatic deployment of Springboot service Docker
  • idea uses docker plug-in to achieve one-click automated deployment

<<:  JavaScript to achieve simple tab bar switching case

>>:  Combining insert and select to implement the method of "inserting the maximum value of a field in the database + 1"

Recommend

Detailed explanation of destructuring assignment syntax in Javascript

Preface The "destructuring assignment syntax...

Implementation of TypeScript in React project

Table of contents 1. Introduction 2. Usage Statel...

Example of using JSX to build component Parser development

Table of contents JSX environment construction Se...

View the command to modify the MySQL table structure

Brief description The editor often encounters som...

JavaScript to achieve a simple countdown effect

This article example shares the specific code of ...

Solution to the problem that Centos8 cannot install docker

Problem [root@zh ~]# [root@zh ~]# [root@zh ~]# yu...

How to view MySQL links and kill abnormal links

Preface: During database operation and maintenanc...

Binary Search Tree Algorithm Tutorial for JavaScript Beginners

Table of contents What is a Binary Search Tree (B...

Html/Css (the first must-read guide for beginners)

1. Understanding the meaning of web standards-Why...

How to write the style of CSS3 Tianzi grid list

In many projects, it is necessary to implement th...

Detailed use of Echarts in vue2 vue3

Table of contents 1. Installation 2. Use Echarts ...

Mysql solves the database N+1 query problem

Introduction In orm frameworks, such as hibernate...

How to query duplicate data in mysql table

INSERT INTO hk_test(username, passwd) VALUES (...

The perfect solution for highlighting keywords in HTML

I recently encountered a feature while working on...