Home  >  Article  >  Java  >  How to deploy SpringBoot project using Docker

How to deploy SpringBoot project using Docker

WBOY
WBOYforward
2023-05-22 23:58:042943browse

First build a simple spring boot project, then add docker support to the project, and finally deploy the project.

A simple spring boot project

In pom.xml, use spring boot 2.0 related dependencies

<parent>
 <groupid>org.springframework.boot</groupid>
 <artifactid>spring-boot-starter-parent</artifactid>
 <version>2.0.0.release</version>
</parent>

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>

Create a dockercontroller with an index() method in it. When accessed, it returns: hello docker!

@restcontroller
public class dockercontroller {
 
  @requestmapping("/")
  public string index() {
    return "hello docker!";
  }
}

Startup class

@springbootapplication
public class dockerapplication {
 
 public static void main(string[] args) {
 springapplication.run(dockerapplication.class, args);
 }
}

After adding it, start the project and start After success, the browser accesses: http://localhost:8080/, and the page returns: hello docker!, indicating that the spring boot project configuration is normal.

Spring boot project adds docker support

Add docker image name in pom.xml-properties

<properties>
 <docker.image.prefix>springboot</docker.image.prefix>
</properties>

plugins Add docker build Plug-in:

<build>
 <plugins>
 <plugin>
  <groupid>org.springframework.boot</groupid>
  <artifactid>spring-boot-maven-plugin</artifactid>
 </plugin>
 <!-- docker maven 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>
 <!-- docker maven plugin -->
 </plugins>
</build>

Create a dockerfile file in the directory src/main/docker. The dockerfile file is used to describe how to build the image.

from openjdk:8-jdk-alpine
volume /tmp
add spring-boot-docker-1.0.jar app.jar
entrypoint ["java","-djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

This dockerfile file is very simple. Build the jdk basic environment, add spring boot jar to the image, briefly explain:

  • from, which means using the jdk8 environment as the basis Mirror, if the mirror is not local, it will be downloaded from dockerhub

  • volume, volume points to a /tmp directory, since spring boot uses the built-in tomcat container, tomcat uses /tmp as the default Work list. The effect of this command is: create a temporary file in the /var/lib/docker directory of the host and link it to the /tmp directory in the container

  • add, copy the file and Rename

  • entrypoint. In order to shorten the startup time of tomcat, add the system property of java.security.egd to point to /dev/urandom as entrypoint

In this way, adding docker dependency to the spring boot project is completed.

Building the packaging environment

We need a docker environment to package the spring boot project. It is very troublesome to build a docker environment in windows, so I use centos 7 here. For example.

Install docker environment

Installation

yum install docker

After the installation is complete, use the following command to start the docker service and set it to Start up:

service docker start
chkconfig docker on
 
#lctt 译注:此处采用了旧式的 sysv 语法,如采用centos 7中支持的新式 systemd 语法,如下:
systemctl start docker.service
systemctl enable docker.service

Use docker China accelerator

vi /etc/docker/daemon.json
 
#添加后:
{
  "registry-mirrors": ["https://registry.docker-cn.com"],
  "live-restore": true
}

Restart docker

systemctl restart docker

Enter docker version and return the version information, then the installation is normal.

Install jdk

yum -y install java-1.8.0-openjdk*

Configure environment variables, open vim /etc/profile and add some content

export java_home=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.161-0.b14.el7_4.x86_64
export path=$path:$java_home/bin

After the modification is completed, make it effective

source /etc/profile

Enter java -version and if the version information is returned, the installation is normal.

Install maven

Download:

## 解压
tar vxf apache-maven-3.5.2-bin.tar.gz
## 移动
mv apache-maven-3.5.2 /usr/local/maven3

Modify the environment variables and add the following lines to /etc/profile

maven_home=/usr/local/maven3
export maven_home
export path=${path}:${maven_home}/bin

Remember to execute source /etc/profile to make the environment variables take effect.

Enter mvn -version to return the version information and the installation is normal.

In this way, the entire build environment is configured.

Use docker to deploy the spring boot project

Copy the project spring-boot-docker to the server and enter the project path for packaging testing.

#打包
mvn package
#启动
java -jar target/spring-boot-docker-1.0.jar

After seeing the startup log of spring boot, it shows that there is no problem with the environment configuration. Next, we use dockerfile to build the image.

mvn package docker:build

The first build may be a bit slow. When you see the following content, the build is successful:

...
step 1 : from openjdk:8-jdk-alpine
 ---> 224765a6bdbe
step 2 : volume /tmp
 ---> using cache
 ---> b4e86cc8654e
step 3 : add spring-boot-docker-1.0.jar app.jar
 ---> a20fe75963ab
removing intermediate container 593ee5e1ea51
step 4 : entrypoint java -djava.security.egd=file:/dev/./urandom -jar /app.jar
 ---> running in 85d558a10cd4
 ---> 7102f08b5e95
removing intermediate container 85d558a10cd4
successfully built 7102f08b5e95
[info] built springboot/spring-boot-docker
[info] ------------------------------------------------------------------------
[info] build success
[info] ------------------------------------------------------------------------
[info] total time: 54.346 s
[info] finished at: 2018-03-13t16:20:15+08:00
[info] final memory: 42m/182m
[info] ------------------------------------------------------------------------

Use the docker images command to view the built image:

docker images
repository           tag         image id      created       size
springboot/spring-boot-docker  latest       99ce9468da74    6 seconds ago    117.5 mb

springboot/spring-boot-docker is the image we built. The next step is to run the image

docker run -p 8080:8080 -t springboot/spring-boot-docker

After the startup is completed, we use docker ps to view the running image:

docker ps
container id    image              command         created       status       ports          names
049570da86a9    springboot/spring-boot-docker  "java -djava.security"  30 seconds ago   up 27 seconds    0.0.0.0:8080->8080/tcp  determined_mahavira

You can see The container we built is running. Visit the browser: http://192.168.0.x:8080/, and return

hello docker!

, indicating that the spring boot project was successfully deployed using docker!

The above is the detailed content of How to deploy SpringBoot project using Docker. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete