Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to deploy a Spring Boot project with Docker

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the knowledge of "how to deploy a Spring Boot project with Docker". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The development of Docker technology provides a more convenient environment for the landing of micro-services. It is actually very easy to deploy Spring Boot using Docker. Let's learn about this article.

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

Org.springframework.boot

Spring-boot-starter-parent

2.0.0.RELEASE

Add web and test dependencies

Org.springframework.boot

Spring-boot-starter-web

Org.springframework.boot

Spring-boot-starter-test

Test

Create a DockerController with an index () method that 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 the project is added, start the project. After starting successfully, the browser visits: http://localhost:8080/, and the page returns: Hello docking, indicating that the Spring Boot project configuration is normal.

Add Docker support for Spring Boot project

Add the Docker image name to the pom.xml-properties

Springboot

Add the Docker build plug-in to plugins:

Org.springframework.boot

Spring-boot-maven-plugin

Com.spotify

Docker-maven-plugin

1.0.0

${docker.image.prefix} / ${project.artifactId}

Src/main/docker

/

${project.build.directory}

${project.build.finalName} .jar

Create a Dockerfile file under the directory src/main/docker, and the Dockerfile file is used to explain 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"]

The Dockerfile file is very simple. Build the Jdk basic environment, add Spring Boot Jar to the image, and briefly explain:

FROM means to use the Jdk8 environment as the basic image. If the image is not local, VOLUME will be downloaded from DockerHub. VOLUME points to a directory of / tmp. Because Spring Boot uses the built-in Tomcat container, Tomcat uses / tmp as the working directory by default. The effect of this command is to create a temporary file under the / var/lib/docker directory of the host and link it to the / tmp directory ADD in the container, copy the file and rename ENTRYPOINT. To shorten the startup time of Tomcat, add the system attribute of java.security.egd pointing to / dev/urandom as ENTRYPOINT

This completes adding Docker dependencies to the Spring Boot project.

Build a packaging environment

We need a Docker environment to package the Spring Boot project, and it's troublesome to build a Docker environment in Windows, so I'll take Centos 7 as an example.

Install the Docker environment

Installation

Yum install docker

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

Service docker start

Chkconfig docker on

# LCTT translation note: the old sysv syntax is used here, such as the new systemd syntax supported in CentOS 7, as follows:

Systemctl start docker.service

Systemctl enable docker.service

Use Docker China Accelerator

Vi / etc/docker/daemon.json

# after adding:

{

"registry-mirrors": ["https://registry.docker-cn.com"],"

"live-restore": true

}

Restart docker

Systemctl restart docker

Enter docker version to return version information and the installation is normal.

Install JDKyum-y install java-1.8.0-openjdk*

Configure the environment variable and open vim / etc/profile to add something.

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 to return the version information and the installation is normal.

Install MAVEN

Download: http://mirrors.shu.edu.cn/apache/maven/maven-3/3.5.2/binaries/apache-maven-3.5.2-bin.tar.gz

# # decompression

Tar vxf apache-maven-3.5.2-bin.tar.gz

# # Mobile

Mv apache-maven-3.5.2 / usr/local/maven3

Modify the environment variable to add the following lines in / 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 variable take effect.

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

In this way, the entire build environment is configured.

Deploy a Spring Boot project using Docker

Put the project spring-boot-docker in the copy server and go to the project path for packaging and testing.

# packing

Mvn package

# start

Java-jar target/spring-boot-docker-1.0.jar

Seeing the startup log of Spring Boot shows that there is nothing wrong with the environment configuration, so let's use DockerFile to build the image.

Mvn package docker:build

The first build may be a bit slow, which indicates a successful build when you see the following:

...

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 have built. The next step is to run the image.

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

After the startup is complete, we use docker ps to view the running images:

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 Djava.security 8080-> 8080/tcp determined_mahavira

You can see that the container we built is running. Visit the browser: http://192.168.0.x:8080/, returns

Hello Docker!

It indicates that using Docker to deploy the Spring Boot project is successful!

Sample code: https://github.com/ityouknow/spring-boot-examples

This is the end of "how to deploy a Spring Boot project with Docker". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report