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

Example of a method for deploying Spring Boot using Docker

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

Share

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

Here, spring-boot is mainly used out of the box, which can generate a program that runs independently and the plug-in docker-maven-plugin of maven

Here are the main steps

Build a simple springboot project to add docker-maven-plugin and write dockerfile practices to generate docker images

A simple Spring Boot project

Take spring boot 2.0 as an example

Add parament dependencies in pom.xml files

Org.springframework.boot spring-boot-starter-parent 2.0.0.RELEASE

Increase web and test dependencies

Org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test

Create a Controller with an index () method that returns: Hello Docker!

@ RestControllerpublic class Controller {@ RequestMapping ("/") public String index () {return "Hello Docker!";}}

Startup class

@ SpringBootApplicationpublic class Application {public static void main (String [] args) {SpringApplication.run (Application.class, args);}}

After adding, start the project. After starting successfully, the browser asks: http://localhost:8080/, the page returns: Hello docking, indicating that the configuration of the Spring Boot project is normal.

Add dcoker-maven-plugin

Add the Docker mirror prefix to the properties node of pom.xml

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.finalNmae} .jar

Write Dockerfile

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-alpineVOLUME / tmpADD spring-boot-docker-1.0.jar app.jarENTRYPOINT ["java", "- Djava.security.egd=file:/dev/./urandom", "- jar", "/ app.jar"] EXPOSE 8080

This Dockerfile file is very simple. Build the Jdk basic environment, add Spring Boot Jar to the image, and simply 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. In order to shorten the startup time of Tomcat, add the system attribute of java.security.egd to / dev/urandom as ENTRYPOINTEXPOSE indicates port 8080 to provide services.

Generate docekr image

You need a Docker environment to package your Spring Boot project.

Need three dependencies

Jdk environment mavendocker environment

Running java-version,mvn-version,docker version these commands does not report an error, which means the environment is ready.

Enter the project directory

Mvn package-Dmavne.test.skip=truejava-jar target/spring-boot-docker-1.0.jar

It works normally, which means there is nothing wrong with the jar package.

Then build the image

Mvn docker:build

The display of build success is successful.

Use docker images to view the built image

Run Mirror

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

Then curl http://127.0.0.1:8080 can see that Hello docking is returned, indicating success.

The above is the whole content of this article, I hope it will be helpful to your study, and I also hope that you will support it.

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