這幾天研究了一下將spring boot應用打入到docker中運行,先前有一個maven插件,可以直接在src/main中建一個docker文件夾,新建一個dockerfile文件,在進行編譯打包之后,可以直接運行docker插件,相當于在對應的docker目錄中執行 docker build .
命令,會直接將當前應用打成鏡像,然后運行,十分方便,但是在個人經過測試后發現,這個插件并不穩定,docker文件夾不一定每次都會打到target文件夾下,因此就會導致這個插件執行起來并沒有多大用處。
因此我在后來再將spring boot應用打成鏡像的時候,不再使用提供的docker maven插件,而是單獨在當前項目的根目錄下新建一個dockerfile文件,應用編寫完了之后,直接手動執行命令將應用打成鏡像,具體如下。
springboot應用
pom.xml
在這里的pom.xml中需要指定幾個倉庫,以及提供幾個插件,具體如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
< project xmlns = "http://maven.apache.org/pom/4.0.0" xmlns:xsi = "http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation = "http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelversion >4.0.0</ modelversion > < groupid >cn.com</ groupid > < artifactid >springbootweb</ artifactid > < version >1.0-snapshot</ version > < packaging >jar</ packaging > < name >spring :: boot :: web</ name > < parent > < groupid >org.springframework.boot</ groupid > < artifactid >spring-boot-starter-parent</ artifactid > < version >1.4.1.release</ version > < relativepath /> </ parent > < properties > < project.build.sourceencoding >utf-8</ project.build.sourceencoding > < docker.image.prefix >springio</ docker.image.prefix > < docker.version >0.3.8</ docker.version > < java.version >1.8</ java.version > </ properties > < dependencies > < dependency > < groupid >org.springframework.boot</ groupid > < artifactid >spring-boot-starter-web</ artifactid > </ dependency > </ dependencies > < repositories > < repository > < id >spring-snapshots</ id > < url >http://repo.spring.io/snapshot</ url > < snapshots > < enabled >true</ enabled > </ snapshots > </ repository > < repository > < id >spring-milestones</ id > < url >http://repo.spring.io/milestone</ url > < snapshots > < enabled >true</ enabled > </ snapshots > </ repository > </ repositories > < pluginrepositories > < pluginrepository > < id >spring-snapshots</ id > < url >http://repo.spring.io/snapshot</ url > </ pluginrepository > < pluginrepository > < id >spring-milestones</ id > < url >http://repo.spring.io/milestone</ url > </ pluginrepository > </ pluginrepositories > < build > < plugins > < plugin > < groupid >org.apache.maven.plugins</ groupid > < artifactid >maven-compiler-plugin</ artifactid > < version >3.2</ version > < configuration > < compilerargument >-parameters</ compilerargument > < source >1.8</ source > < target >1.8</ target > < encoding >utf-8</ encoding > </ configuration > </ plugin > < plugin > < groupid >org.apache.maven.plugins</ groupid > < artifactid >maven-surefire-plugin</ artifactid > < version >2.18.1</ version > < configuration > < skiptests >true</ skiptests > </ configuration > </ plugin > < plugin > < groupid >org.apache.maven.plugins</ groupid > < artifactid >maven-resources-plugin</ artifactid > < version >2.6</ version > < configuration > < encoding >utf-8</ encoding > </ configuration > </ plugin > < plugin > < groupid >org.springframework.boot</ groupid > < artifactid >spring-boot-maven-plugin</ artifactid > <!--<version>${spring.boot.version}</version>--> < configuration > < mainclass >cn.com.springbootwebapplication</ mainclass > < layout >zip</ layout > </ configuration > < executions > < execution > < goals > < goal > repackage </ goal > </ goals > </ execution > </ executions > </ plugin > </ plugins > </ build > < profiles > < profile > < id >jdk1.8</ id > < activation > < activebydefault >true</ activebydefault > </ activation > < properties > < maven.compiler.source >1.8</ maven.compiler.source > < maven.compiler.target >1.8</ maven.compiler.target > < encoding >utf-8</ encoding > </ properties > </ profile > </ profiles > </ project > |
這里的提供了幾個倉庫的地址,原因是因為本文中將springboot應用打進docker的時候,是直接將源碼一起打進去,然后在里面進行編譯打包之后進行運行,如果不提供倉庫地址下載jar包,那么就會從中央倉庫拉取依賴,那么速度會非常慢并且會出現拉取超時導致應用使用不了的情況,因此提供幾個其他倉庫地址下載依賴,另外這里有一個插件,使用這個插件后可以直接以 mvn spring-boot:run的形式運行應用,所以我也就沒決定使用java -jar xxx.jar的方式來運行應用。
application和controller
這個springboot應用相當簡單,提供一個簡單的controller,里面有一個類似與hello world的接口,如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
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. */ @restcontroller public class indexcontroller { @requestmapping (value = "/index" , produces = "application/json;charset=utf-8" ) public object index() { map<string, object> result = new hashmap<string, object>(); result.put( "msg" , "hello world" ); return result; } } |
提供一個簡單的helloworl的方法。
以下是application啟動類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
package cn.com; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication; /** * created by xiaxuan on 16/11/27. */ @springbootapplication public class springbootwebapplication { public static void main(string[] args) { springapplication.run(springbootwebapplication. class , args); } } |
正常的spring boot的啟動中,相當簡單,直接啟動springbootwebapplication啟動類即可,但是在docker容器中運行的話,則沒有這么簡單,看下下面的這個dockerfile文件。
dockerfile文件
dockerfile文件也比較簡單,如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
# base image from java:8 # maintainer maintainer bingwenwuhen bingwenwuhen@163.com # update packages and install maven run \ export debian_frontend=noninteractive && \ sed -i 's/# \(.*multiverse$\)/\1/g' /etc/apt/sources .list && \ apt-get update && \ apt-get -y upgrade && \ apt-get install -y vim wget curl maven # attach volumes volume /vol/development # create working directory run mkdir -p /vol/development run mkdir -p /vol/development/src workdir /vol/development copy . /src /vol/development/src/ copy . /pom .xml /vol/development/ # maven exec cmd [ "mvn" , "clean" , "install" , "spring-boot:run" ] |
dockerfile中以java8為基礎鏡像,同時在基礎鏡像中還需要單獨安裝maven,因為在我們的dockerfile文件中,是將整個源碼都打進鏡像之中,在這里沒有只將生成的jar打進鏡像中,所以這就是之前所說的需要在pom.xml中指定倉庫,如果不指定倉庫,則在鏡像中拉取依賴的時候,會從中央倉庫拉取依賴,那么會非常慢,我之前試過幾次,基本拉取過程中,都超時失敗了,所以在這里指定倉庫拉取依賴。
構建鏡像
現在在目錄下面執行命令,docker build -t="bingwenwuhen/springboot01" .
構建鏡像,如下:
在打成鏡像之后,運行
docker run -d --name springboot01 -p 8080:8080 bingwenwuhe/spingboot01
以上命令為運行該鏡像生成一個容器,映射端口為8080,名字為springboot01,
現在docker logs xxxxx
查看容器日志:
現在這個容器已經運行起來了。
請求接口
在請求接口前,需要先查看docker虛擬機的ip為多少,本機為192.168.99.100,請求接口命令為:
1
|
curl http: //192 .168.99.100:8080 /index |
響應為:
1
2
3
|
{ "msg" : "hello world" } |
請求成功,以上,springboot應用打進docker中運行也就成功了。
問題
- 在將源碼打進鏡像中,mvn clean install 編譯運行的時候,下載的jar包實在太多,等的時間太長,很容易中斷,所以十分不推薦這種方式的運行。
- 源碼本質上不應該打進鏡像之中,只需要將運行的jar包打進鏡像之中就可以了。
源碼
我將源碼上傳到github上,有需要的可以自己下載。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/u012734441/article/details/53462308