In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
In the past few days, I have studied how to run spring boot applications into docker. There was a maven plug-in. You can directly build a docker folder in src/main and create a new Dockerfile file. After compiling and packaging, you can directly run the docker plug-in, which is equivalent to executing docker build in the corresponding docker directory. Command, will directly mirror the current application, and then run, very convenient, but after personal testing, found that the plug-in is not stable, docker folder may not always be called under the target folder, so this plug-in is not very useful.
Therefore, when I mirror the spring boot application later, I no longer use the provided docker maven plug-in, but create a new Dockerfile file under the root directory of the current project. After the application is written, I directly execute the command manually to mirror the application, as shown below.
Springboot application
Pom.xml
In the pom.xml here, you need to specify several repositories and provide several plug-ins, as follows:
4.0.0 cn.com springbootweb 1.0-SNAPSHOT jar spring:: boot:: web org.springframework.boot spring-boot-starter-parent 1.4.1.RELEASE UTF-8 springio 0.3.8 1.8 org.springframework.boot spring-boot-starter-web spring-snapshots http://repo.spring.io/snapshot true spring-milestones http://repo.spring.io/milestone true Spring-snapshots http://repo.spring.io/snapshot spring-milestones http://repo.spring.io/milestone org.apache.maven.plugins maven-compiler-plugin 3.2-parameters 1.81.8 UTF-8 org.apache.maven.plugins maven-surefire-plugin 2.18.1 true org.apache. Maven.plugins maven-resources-plugin 2.6 UTF-8 org.springframework.boot spring-boot-maven-plugin cn.com.SpringBootWebApplication ZIP repackage JDK1.8 true 1.8 1.8 UTF-8
The address of several repositories is provided here, because when the springboot application is plugged into docker in this article, the source code is directly typed together, and then it is compiled and packaged inside to run. If you do not provide the warehouse address to download the jar package, the dependency will be pulled from the central warehouse, so the speed will be very slow and the pull timeout will cause the application to fail to use. So provide several other warehouse address download dependencies, and there is a plug-in, after using this plug-in can directly run the application in the form of mvn spring-boot:run, so I did not decide to use java-jar xxx.jar to run the application.
Application and controller
This springboot application is quite simple, providing a simple controller with an interface similar to hello world, as follows:
Package cn.com.controllers;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.util.HashMap;import java.util.Map;/** * Created by xiaxuan on 16-11-27. * / @ RestControllerpublic class IndexController {@ RequestMapping (value = "/ index", produces = "application/json;charset=utf-8") public Object index () {Map result = new HashMap (); result.put ("msg", "hello world"); return result;}}
Provides a simple method of helloworl.
The following is the Application startup class:
Package cn.com;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;/** * Created by xiaxuan on 16-11-27. * / @ SpringBootApplicationpublic class SpringBootWebApplication {public static void main (String [] args) {SpringApplication.run (SpringBootWebApplication.class, args);}}
In normal spring boot startup, it is quite simple to start the SpringBootWebApplication startup class directly, but it is not so simple to run in the docker container, take a look at the Dockerfile file below.
Dockerfile file
The Dockerfile file is also relatively simple, as follows:
# base imageFROM java:8# maintainerMAINTAINER bingwenwuhen bingwenwuhen@163.com# update packages and install mavenRUN\ export DEBIAN_FRONTEND=noninteractive & &\ sed-I's vol/development# create working directoryRUN mkdir #\ (. * multiverse$\) /\ etc/apt/sources.list & &\ apt-get update & &\ apt-get-y upgrade & &\ apt-get install-y vim wget curl maven# attach volumesVOLUME / vol/development# create working directoryRUN mkdir-p / vol/developmentRUN mkdir-p / vol/development/srcWORKDIR / vol / developmentCOPY. / src/ vol/development/src/COPY. / pom.xml / vol/development/# maven execCMD ["mvn" "clean", "install", "spring-boot:run"]
Java8 is used as the basic image in dockerfile, and maven needs to be installed separately in the basic image, because in our dockerfile file, the entire source code is entered into the image. Here, we do not only type the generated jar into the image, so this is the need to specify a repository in the pom.xml. If you do not specify a repository, when you pull the dependency in the image, you will pull the dependency from the central repository, which will be very slow. I have tried several times before, and the timeout failed in the basic pull process, so I specify the warehouse pull dependency here.
Build an image
Now execute the command under the directory, docker build-t = "bingwenwuhen/springboot01". Build an image, as follows:
After being mirrored, run
Docker run-d-name springboot01-p 8080 bingwenwuhe/spingboot01
The above command generates a container for running the image with the mapping port 8080 and the name springboot01
Now docker logs xxxxx looks at the container log:
Now the container is running.
Request interface
Before requesting the interface, you need to check the ip of the docker virtual machine. The native value is 192.168.99.100. The command for the request interface is:
Curl http://192.168.99.100:8080/index
The response is:
{"msg": "hello world"}
The request is successful. Above, the springboot application runs successfully in docker.
problem
When typing the source code into the image, when mvn clean install compiles and runs, too many jar packages are downloaded, and the waiting time is too long, so it is easy to interrupt, so it is not recommended to run in this way. In essence, the source code should not be typed into the image, just need to type the running jar package into the image.
Source code
I upload the source code to github, and you can download it yourself if you need it.
Source code download
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.