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 Docker deploys Spring-boot projects

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

Share

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

This article mainly introduces the relevant knowledge of Docker how to deploy the Spring-boot project, the content is detailed and easy to understand, the operation is simple and quick, and has a certain reference value. I believe you will gain something after reading this article on how to deploy the Spring-boot project in Docker. Let's take a look.

1. Fast start of basic spring-boot

1.1 Quick launch pom.xml adds the following dependencies

Org.springframework.boot spring-boot-starter-parent 2.0.5.release 1.8 utf-8 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test spring-docker org.springframework.boot spring-boot-maven-plugin

Spring-boot startup class

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

Test api

@ restcontrollerpublic class dockerstarterapi {@ getmapping ("/ api/docker/hello") public string hello () {return "hello docker";}}

Configure the startup configuration file application.yml

Server: port: 9090 # to show the effect, the default port 8080 has been changed here

Check spring startup

. _ _ _ / / _ _ _ (() _ _ _ (()\ _ _ _ |'_ | |'_ / _ _ `|\ / _ _ _) | | | (_ | |) '| _ |. _ _ | _ | | _ | _ | | _\ _ _ | / = | _ | = | _ _ /:: spring boot:: (v2.0.2.release). 2018-12-17 17 info 26 info 48740-[main] o.s.j.e.a.annotationmbeanexporter: registering beans for jmx exposure on startup2018-12-17 17 info 26 13. 448 info 48740-- [main] o.s.b.w. Embedded.tomcat.tomcatwebserver: tomcat started on port (s): 9090 (http) with context path''2018-12-17 17 http 26 http 13.453 info 48740-[main] pers.study.docker.dockerapplication: started dockerapplication in 1.982 seconds (jvm running for 2.602)

Check whether api is in effect

$curl-xget 'http://localhost:9090/api/docker/hello'hello docker

Browser check

Http://localhost:9090/api/docker/hello

1.2 Packaging start

Project packaging

After completing the above steps, execute the packaging command:

$mvn clean-u-dmaven.test.skip compile package

Because finalname is defined in the pom file above, you will see that spring-docker.jar will be generated in the target directory after compilation and packaging.

Spring-docker

Test run

$java-jar target/spring-docker.jar

No accident (leave a message) the running result is the same as above and check whether api is effective or not.

II. Quick installation of docker

Next, start preparing for docker.

Installation

Check installation, view help

$docker-- versiondocker version 18.06.0-ce, build 0ffa825 $docker-- helpusage: docker [options] commanda self-sufficient runtime for containers...

Mirror acceleration

3. Configure spring-boot + docker

Pom.xml add docker plugin

Springboot com.spotify docker-maven-plugin 1.0.0 ${docker.image.prefix} / ${project.build.finalname} src/main/docker / ${project.build.directory} ${project.build.finalname} .jar

Create a dockerfile file

Configure src/main/docker according to the above pom.xml file, where the directory of the docker configuration file is configured, so you need to create a docker folder under src/main and a dockerfile file at the same time.

The catalogue structure is shown in the figure:

Docker profile structure .png

Edit dockerfile

From openjdk:8-jdk-alpinevolume / tmpadd spring-docker.jar app.jarentrypoint ["java", "- djava.security.egd=file:/dev/./urandom", "- jar", "/ app.jar"]

From means image based on java8.

Volume represents the mount directory

Add copies the packaged file and renames it to app.jar

Entrypoint explains, according to the following official documentation, a system attribute that is roughly added to shorten the startup time of tomcat.

We added a volume pointing to / tmp because that is where a spring boot application creates working directories for tomcat by default. The effect is to create a temporary file on your host under / var/lib/docker and link it to the container under / tmp. This step is optional for the simple app that we wrote here but can be necessary for other spring boot applications if they need to actually write in the filesystem.

To reduce tomcat startup time we added a system property pointing to "/ dev/urandom" as a source of entropy. This is not necessary with more recent versions of spring boot, if you use the "standard" version of tomcat (or any other web server).

Configuration complete!

4. Docker starts spring-boot

Enter module to execute:

$mvn package docker:build [info] scanning for projects. -- > running in e1f8aba72bdfremoving intermediate container e1f8aba72bdf-> 36a61c09f09aprogressmessage {id=null, status=null, stream=null, error=null, progress=null Progressdetail=null} successfully built 36a61c09f09asuccessfully tagged springboot/spring-docker:latest [info] built springboot/spring-docker [info]-[info] build success [info]- -[info] total time: 6.367 s [info] finished at: 2018-12-17t20:48:21+08:00 [info]-- -

View Mirror

$docker imagesrepository tag image id created sizespringboot/spring-docker latest 36a61c09f09a 2 minutes ago 123mb

Run Mirror

Docker run-p 9090 9090-t springboot/spring-docker. _ _ _ / / _ _ _ (() _ _ _ (()\ _ _ _ |'_ | |'_ / _ _ `|\ / _ _ _) | | | (_ | |) '| _ |. _ _ | _ | | _ | _ | | _\ _ _ | | / = | _ | = | _ _ / = /: spring boot:: (v2.0.2.release) 2018-12-17 1253 info 21.502 info 1-[main] pers.study.docker.dockerapplication: starting dockerapplication v1.0-snapshot on 94991c04be5d with pid 1 (/ app.jar started by root in /) 2018-12-17 1253 info 21.509 info 1-- [main " ] pers.study.docker.dockerapplication: no active profile set Falling back to default profiles: default ·2018-12-17 12 main 53 info 25.255 info 1-[main] o.s.j.e.a.annotationmbeanexporter: registering beans for jmx exposure on startup2018-12-17 12 12 with context path 53 falling back to default profiles 25.337 info 1-[main] o.s.b.w.embedded.tomcat.tomcatwebserver: tomcat started on port (s): 9090 (http) with context path''2018-12-17 12:53:25. 353 info 1-[main] pers.study.docker.dockerapplication: started dockerapplication in 4.485 seconds (jvm running for 5.346)

View the container

$docker pscontainer id image command created status ports names94991c04be5d springboot/spring-docker "java-djava.securit..." 53 seconds ago up 52 seconds 0.0.0.0 9090/tcp quizzical_bhabha 9090-> 9090/tcp quizzical_bhabha

Verify startup and access api

$curl-xget 'http://localhost:9090/api/docker/hello'hello docker

At this point, the docker deployment spring-boot has been built.

5. Remove the image

Stop the container

$docker stop 94991c04be5d94991c04be5d

Delete Container

$docker rm 94991c04be5d94991c04be5d

Delete Mirror

$docker image rm springboot/spring-dockeruntagged: springboot/spring-docker:latestdeleted: sha256:36a61c09f09ab88cfe5a05f564deb57498682f4a6f3ec01d2a8c4fdc80ac1e41deleted: sha256:3f9aef70be6d4d43c205454d8874f10bc2f7280f70eb88cd1f04937b7965dd27deleted: sha256:9a5800e93615bb4c5128bb36d31ec494327c01f1a9a768c1ff538badf76628b9deleted: sha256:d9c66f907448fa9e61fd5f9267d7fcf8e1f4b52d0a20466414f2f45777261284

VI. Other configuration functions

Add environment properties

$docker run-e "spring_profiles_active=prod"-p 9090pur9090-t springbooot/spring-docker

Start running in the background

$docker run-p 9090 9090-d springboot/spring-docker

Open container debug and modify dockerfile

From openjdk:8-jdk-alpinevolume / tmpadd spring-docker.jar app.jarenv java_opts''cmd java-djava.security.egd=file:/dev/./urandom $java_opts-jar app.jar

Docker run

The copy code is as follows:

$docker run-e "java_opts=-agentlib:jdwp=transport=dt_socket,address=5005,server=y,suspend=n"-p 9090 springboot/spring-docker 9090-p 5005 springboot/spring-docker

This is the end of the article on "how Docker deploys the Spring-boot project". Thank you for reading! I believe you all have a certain understanding of "how Docker deploys the Spring-boot project". If you want to learn more, you are welcome to follow the industry information channel.

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