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 analyze the process of publishing and deploying Spring Boot applications through Docker

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article focuses on "how to analyze the process of publishing and deploying Spring Boot applications through Docker". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn "how to analyze the process of Spring Boot application release and deployment through Docker".

Catalogue

Manual deployment

1. Idea creates a spring boot project

2. Package the project into a Jar package.

3. Build docker image

4. View and run the image

Plug-in deployment

Run the push command

There are two ways to deploy a Spring Boot project to docker, manual deployment and plug-in deployment

Manual deployment 1. Idea create spring boot project

Pom.xml file

Spring-cloud-examples org.example 1.0-SNAPSHOT 4.0.0 DockerDemo org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-maven-plugin

You must add the spring-boot-maven-plugin plug-in, which is used to introduce dependency packages when typing Jar packages. When you run "mvn package" to package, it will be packaged into a JAR file that can be run directly, which can be run directly using the "java-jar" command.

Startup class

Package dockerdemo;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController@SpringBootApplicationpublic class Application {public static void main (String [] args) {SpringApplication.run (Application.class, args);} @ RequestMapping ("/ hello") public String hello () {return "Hello Docker World!";}} 2, Jar package for the project

Then execute the maven command in the directory where the project pom.xml file is located to type the project into a Jar package

$mvn package

From the output log, we can see that Jar runs the Jar package directly under the target directory.

$java-jar DockerDemo-1.0-SNAPSHOT.jar

Then type http://localhost:8080/hello into the browser to test

3. Build docker image

Create a Dockerfile file

FROM java:8VOLUME / tmpADD DockerDemo-1.0-SNAPSHOT.jar DockerDemo.jarRUN bash-c "touch / DockerDemo.jar" ENTRYPOINT ["java", "- Djava.security.egd=file:/dev/./urandom", "- jar", "/ DockerDemo.jar"]

Parameter explanation:

FROM: creates a docker image based on JDK8

VOLUME: create a mount point, the container directory is / tmp, and the host directory is generated automatically. / tmp is created because the Tomcat container embedded in Spring Boot uses / tmp as the working directory by default

ADD: copy the DockerDemo-1.0-SNAPSHOT.jar outside the container into the container and rename it to DockerDemo.jar

RUN:RUN is followed by the bash command, and-c means to execute the following string as a command, that is, to execute touch / DockerDemo.jar, which modifies the access time and modification time of the DockerDemo.jar file to the current time

ENTRYPOINT: the command that runs when the container starts, which is equivalent to typing java-jar xxxx.jar on the command line. In order to shorten the startup time of Tomcat, add the system attribute of java.security.egd pointing to / dev/urandom as ENTRYPOINT

After creating the Dockerfile, put the packaged Spring Boot project jar package and Dockerfile file in any directory, and use the docker command to build the image file:

$docker image build-t DockerDemo:1.

Parameter explanation:

Build: means to make an image

-t: label the image, which is equivalent to docker tag image ID new image name: version number

.: indicates the location of the Dockerfile file. In the current directory

4. View and run the image # View the image: $docker images# run the image: $docker container run-- name DockerDemo-d-p 80 name DockerDemo 8080 DockerDemo:1

Parameter explanation:

Docker container run: indicates the running container

-name: give the container an alias. When operating the container, you can use the alias instead of the container ID to facilitate the management of the container.

-d: indicates that the container runs in the background after the container is opened.

-p: Port mapping. Map port 8080 inside the container to port 80 of the host

Plug-in deployment

Plug-in deployment to add the dockerfile-maven-plugin plug-in to the pom.xml file of the project

Pom.xml

Spring-cloud-docker org.example 1.0-SNAPSHOT 4.0.0 spring-cloud-eureka 1.8 1.8 UTF-8 UTF-8 registry.cn-huhehaote.aliyuncs.com/monkeybrain latest org.springframework. Cloud spring-cloud-starter-eureka-server install ${project.artifactId} src/main/resources true org.springframework. Boot spring-boot-maven-plugin true org.apache.maven.plugins maven-surefire-plugin true Com.spotify docker-maven-plugin 0.4.13 http://localhost:2375 ${docker.image.prefix} / ${project.build.finalName} Aliyun-docker-registry registry.cn-huhehaote.aliyuncs.com true latest java:8 ["java" "- jar" "/ ${project.build.finalName} .jar"] / ${project.build.directory} ${project.build.finalName} .jar Run the push command $mvn clean package docker:build-DpushImage here I believe you have a deeper understanding of "how to analyze the process of publishing and deploying Spring Boot applications through Docker". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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

Development

Wechat

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

12
Report