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

How to deploy SpringBoot project using Docker

WBOY
Release: 2023-05-22 23:58:04
forward
3035 people have browsed it

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>
Copy after login

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>
Copy after login

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!";
  }
}
Copy after login

Startup class

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

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>
Copy after login

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>
Copy after login

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"]
Copy after login

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
Copy after login

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
Copy after login

Use docker China accelerator

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

Restart docker

systemctl restart docker
Copy after login

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

Install jdk

yum -y install java-1.8.0-openjdk*
Copy after login

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
Copy after login

After the modification is completed, make it effective

source /etc/profile
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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] ------------------------------------------------------------------------
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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

hello docker!
Copy after login

, 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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template