Fork me on GitHub

SpringBoot应用docker化并发布到远程服务器

目录

docker开启远程访问接口

docker得环境搭建这里就不重复了,还不会得可以自行去百度,也就几个命令。

想要将本地镜像推送到阿里云得docker容器上,需要开启远程访问权限

首先编辑docker的宿主机文件/lib/systemd/system/docker.service

1
vi /lib/systemd/system/docker.service

修改以ExecStart开头的行,我这里是腾讯云服务器center os 7 ,修改后为:

SpringBoot应用docker化并发布到远程服务器

修改后保存文件,然后通知和重启服务

1
2
systemctl daemon-reload
service docker restart

可是执行 service docker restart 重启docker会报错查看:https://blog.csdn.net/hdu09075340/article/details/101060655

重启完成以后可以在本机验证,通过curl命令可以查看版本信息

1
curl http://localhost:2375/version

SpringBoot应用docker化并发布到远程服务器

开启白名单,让客户端能够远程访问

我们在远程机器开启了端口,但是一般得云服务器都需要设置端口白名单才可以访问,具体设置端口白名单这里就不介绍了,设置完成以后,可以通过外网ip在windows机器浏览器进行访问

SpringBoot应用docker化并发布到远程服务器

本地docker项目发布镜像到远程服务器

不以实战为目的得技术就是耍流氓,假设现在有个需求:作为码农得我开发了一个博客系统,现在开发完毕之后想部署到远程服务器上去。

传统部署方案

服务器上装jdk,装tomcat等—>项目打包->ftp上传->启动项目

docker部署方案

docker部署方案:项目集成docker插件->本地打包->项目构建镜像到远程机器->远程服务器下载镜像并启动

通过两套发布流程比较我们就能知道各自得优劣了,这里不再累赘,开始进行第二种方案实战!

首先进行本地环境变量配置

SpringBoot应用docker化并发布到远程服务器

如图,配置DOCKER_HOST得远程tcp连接。

将项目导入到idea里面,我们看看主要得docker相关配置

Dockerfile文件

1
2
3
4
FROM openjdk:8-jdk-alpine
VOLUME /tmp
ADD demo-1.0.0-SNAPSHOT.jar app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

maven依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.0.0</version>
<configuration>
<imageName>${docker.image.prefix}/${project.artifactId}</imageName>
<dockerDirectory>src/main/docker</dockerDirectory>
<dockerHost>http://10.254.193.119:2375</dockerHost>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>

使用idea自带得docker插件进行构建镜像

首先对项目进行打包

SpringBoot应用docker化并发布到远程服务器

SpringBoot应用docker化并发布到远程服务器

打包完成以后,点击docker:build

SpringBoot应用docker化并发布到远程服务器

构建成功后日志如下:

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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
D:\devsoft\Java\jdk1.8.0_121\bin\java.exe -Dmaven.multiModuleProjectDirectory=D:\Learning-materials\microservices\springcloud-greenwich-github\MicroservicesGreenwich\demo -Dmaven.home=D:\devsoft\apache-maven-3.5.0 -Dclassworlds.conf=D:\devsoft\apache-maven-3.5.0\bin\m2.conf "-Dmaven.ext.class.path=D:\Program Files\JetBrains\IntelliJ IDEA 2019.2\plugins\maven\lib\maven-event-listener.jar" "-javaagent:D:\Program Files\JetBrains\IntelliJ IDEA 2019.2\lib\idea_rt.jar=53271:D:\Program Files\JetBrains\IntelliJ IDEA 2019.2\bin" -Dfile.encoding=UTF-8 -classpath D:\devsoft\apache-maven-3.5.0\boot\plexus-classworlds-2.5.2.jar org.codehaus.classworlds.Launcher -Didea.version2019.2 -s D:\devsoft\apache-maven-3.5.0\conf\settings.xml com.spotify:docker-maven-plugin:1.0.0:build
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building demo 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- docker-maven-plugin:1.0.0:build (default-cli) @ demo ---
[INFO] Using authentication suppliers: [ConfigFileRegistryAuthSupplier]
[INFO] Copying D:\Learning-materials\microservices\springcloud-greenwich-github\MicroservicesGreenwich\demo\target\demo-1.0.0-SNAPSHOT.jar -> D:\Learning-materials\microservices\springcloud-greenwich-github\MicroservicesGreenwich\demo\target\docker\demo-1.0.0-SNAPSHOT.jar
[INFO] Copying src\main\docker\Dockerfile -> D:\Learning-materials\microservices\springcloud-greenwich-github\MicroservicesGreenwich\demo\target\docker\Dockerfile
[INFO] Building image demo-docker/demo
Step 1/4 : FROM openjdk:8-jdk-alpine

Pulling from library/openjdk
e7c96db7181b: Already exists
f910a506b6cb: Pulling fs layer
c2274a1a0e27: Pulling fs layer
f910a506b6cb: Downloading [==================================================>] 238B/238B
f910a506b6cb: Verifying Checksum
f910a506b6cb: Download complete
f910a506b6cb: Extracting [==================================================>] 238B/238B
f910a506b6cb: Extracting [==================================================>] 238B/238B
f910a506b6cb: Pull complete
c2274a1a0e27: Downloading [> ] 524.5kB/70.73MB
c2274a1a0e27: Downloading [=> ] 2.1MB/70.73MB
c2274a1a0e27: Downloading [==> ] 3.675MB/70.73MB
c2274a1a0e27: Downloading [===> ] 4.725MB/70.73MB
c2274a1a0e27: Downloading [====> ] 6.825MB/70.73MB
c2274a1a0e27: Downloading [=====> ] 8.4MB/70.73MB
c2274a1a0e27: Downloading [=======> ] 9.975MB/70.73MB
c2274a1a0e27: Downloading [========> ] 11.55MB/70.73MB
c2274a1a0e27: Downloading [=========> ] 13.12MB/70.73MB
c2274a1a0e27: Downloading [==========> ] 14.7MB/70.73MB
c2274a1a0e27: Downloading [===========> ] 16.27MB/70.73MB
c2274a1a0e27: Downloading [============> ] 17.85MB/70.73MB
c2274a1a0e27: Downloading [=============> ] 19.42MB/70.73MB
c2274a1a0e27: Downloading [==============> ] 20.47MB/70.73MB
c2274a1a0e27: Downloading [===============> ] 22.05MB/70.73MB
c2274a1a0e27: Downloading [================> ] 23.62MB/70.73MB
c2274a1a0e27: Downloading [=================> ] 25.2MB/70.73MB
c2274a1a0e27: Downloading [==================> ] 26.77MB/70.73MB
c2274a1a0e27: Downloading [====================> ] 28.35MB/70.73MB
c2274a1a0e27: Downloading [=====================> ] 29.92MB/70.73MB
c2274a1a0e27: Downloading [=====================> ] 30.97MB/70.73MB
c2274a1a0e27: Downloading [=======================> ] 32.55MB/70.73MB
c2274a1a0e27: Downloading [=======================> ] 33.6MB/70.73MB
c2274a1a0e27: Downloading [========================> ] 34.12MB/70.73MB
c2274a1a0e27: Downloading [========================> ] 35.17MB/70.73MB
c2274a1a0e27: Downloading [=========================> ] 35.7MB/70.73MB
c2274a1a0e27: Downloading [=========================> ] 36.22MB/70.73MB
c2274a1a0e27: Downloading [=========================> ] 36.75MB/70.73MB
c2274a1a0e27: Downloading [==========================> ] 37.27MB/70.73MB
c2274a1a0e27: Downloading [==========================> ] 37.8MB/70.73MB
c2274a1a0e27: Downloading [===========================> ] 38.32MB/70.73MB
c2274a1a0e27: Downloading [===========================> ] 38.85MB/70.73MB
c2274a1a0e27: Downloading [===========================> ] 39.37MB/70.73MB
c2274a1a0e27: Downloading [============================> ] 39.9MB/70.73MB
c2274a1a0e27: Downloading [============================> ] 40.42MB/70.73MB
c2274a1a0e27: Downloading [============================> ] 40.95MB/70.73MB
c2274a1a0e27: Downloading [=============================> ] 41.47MB/70.73MB
c2274a1a0e27: Downloading [=============================> ] 42MB/70.73MB
c2274a1a0e27: Downloading [==============================> ] 42.52MB/70.73MB
c2274a1a0e27: Downloading [==============================> ] 43.05MB/70.73MB
c2274a1a0e27: Downloading [==============================> ] 43.57MB/70.73MB
c2274a1a0e27: Downloading [===============================> ] 44.1MB/70.73MB
c2274a1a0e27: Downloading [===============================> ] 44.62MB/70.73MB
c2274a1a0e27: Downloading [===============================> ] 45.15MB/70.73MB
c2274a1a0e27: Downloading [================================> ] 45.67MB/70.73MB
c2274a1a0e27: Downloading [================================> ] 46.2MB/70.73MB
c2274a1a0e27: Downloading [=================================> ] 46.72MB/70.73MB
c2274a1a0e27: Downloading [=================================> ] 47.25MB/70.73MB
c2274a1a0e27: Downloading [=================================> ] 47.77MB/70.73MB
c2274a1a0e27: Downloading [==================================> ] 48.3MB/70.73MB
c2274a1a0e27: Downloading [==================================> ] 48.82MB/70.73MB
c2274a1a0e27: Downloading [==================================> ] 49.35MB/70.73MB
c2274a1a0e27: Downloading [===================================> ] 49.87MB/70.73MB
c2274a1a0e27: Downloading [===================================> ] 50.4MB/70.73MB
c2274a1a0e27: Downloading [===================================> ] 50.92MB/70.73MB
c2274a1a0e27: Downloading [====================================> ] 51.45MB/70.73MB
c2274a1a0e27: Downloading [====================================> ] 51.97MB/70.73MB
c2274a1a0e27: Downloading [=====================================> ] 52.5MB/70.73MB
c2274a1a0e27: Downloading [=====================================> ] 53.02MB/70.73MB
c2274a1a0e27: Downloading [=====================================> ] 53.55MB/70.73MB
c2274a1a0e27: Downloading [======================================> ] 54.07MB/70.73MB
c2274a1a0e27: Downloading [======================================> ] 54.6MB/70.73MB
c2274a1a0e27: Downloading [======================================> ] 55.12MB/70.73MB
c2274a1a0e27: Downloading [=======================================> ] 55.65MB/70.73MB
c2274a1a0e27: Downloading [=======================================> ] 56.17MB/70.73MB
c2274a1a0e27: Downloading [========================================> ] 56.7MB/70.73MB
c2274a1a0e27: Downloading [========================================> ] 57.22MB/70.73MB
c2274a1a0e27: Downloading [========================================> ] 57.75MB/70.73MB
c2274a1a0e27: Downloading [=========================================> ] 58.27MB/70.73MB
c2274a1a0e27: Downloading [=========================================> ] 58.8MB/70.73MB
c2274a1a0e27: Downloading [=========================================> ] 59.32MB/70.73MB
c2274a1a0e27: Downloading [==========================================> ] 59.85MB/70.73MB
c2274a1a0e27: Downloading [==========================================> ] 60.37MB/70.73MB
c2274a1a0e27: Downloading [===========================================> ] 60.9MB/70.73MB
c2274a1a0e27: Downloading [===========================================> ] 61.42MB/70.73MB
c2274a1a0e27: Downloading [===========================================> ] 61.95MB/70.73MB
c2274a1a0e27: Downloading [============================================> ] 62.47MB/70.73MB
c2274a1a0e27: Downloading [============================================> ] 63MB/70.73MB
c2274a1a0e27: Downloading [============================================> ] 63.52MB/70.73MB
c2274a1a0e27: Downloading [=============================================> ] 64.05MB/70.73MB
c2274a1a0e27: Downloading [=============================================> ] 64.57MB/70.73MB
c2274a1a0e27: Downloading [==============================================> ] 65.1MB/70.73MB
c2274a1a0e27: Downloading [==============================================> ] 65.62MB/70.73MB
c2274a1a0e27: Downloading [==============================================> ] 66.15MB/70.73MB
c2274a1a0e27: Downloading [===============================================> ] 66.67MB/70.73MB
c2274a1a0e27: Downloading [===============================================> ] 67.2MB/70.73MB
c2274a1a0e27: Downloading [===============================================> ] 67.72MB/70.73MB
c2274a1a0e27: Downloading [================================================> ] 68.25MB/70.73MB
c2274a1a0e27: Downloading [================================================> ] 68.77MB/70.73MB
c2274a1a0e27: Downloading [================================================> ] 69.3MB/70.73MB
c2274a1a0e27: Downloading [=================================================> ] 69.82MB/70.73MB
c2274a1a0e27: Downloading [=================================================> ] 70.35MB/70.73MB
c2274a1a0e27: Verifying Checksum
c2274a1a0e27: Download complete
c2274a1a0e27: Extracting [> ] 557.1kB/70.73MB
c2274a1a0e27: Extracting [=> ] 1.671MB/70.73MB
c2274a1a0e27: Extracting [=> ] 2.785MB/70.73MB
c2274a1a0e27: Extracting [===> ] 4.456MB/70.73MB
c2274a1a0e27: Extracting [====> ] 6.128MB/70.73MB
c2274a1a0e27: Extracting [======> ] 8.913MB/70.73MB
c2274a1a0e27: Extracting [=======> ] 11.14MB/70.73MB
c2274a1a0e27: Extracting [=========> ] 13.93MB/70.73MB
c2274a1a0e27: Extracting [===========> ] 16.15MB/70.73MB
c2274a1a0e27: Extracting [=============> ] 18.94MB/70.73MB
c2274a1a0e27: Extracting [===============> ] 21.73MB/70.73MB
c2274a1a0e27: Extracting [================> ] 23.95MB/70.73MB
c2274a1a0e27: Extracting [==================> ] 26.18MB/70.73MB
c2274a1a0e27: Extracting [====================> ] 28.41MB/70.73MB
c2274a1a0e27: Extracting [=====================> ] 30.64MB/70.73MB
c2274a1a0e27: Extracting [=======================> ] 32.87MB/70.73MB
c2274a1a0e27: Extracting [========================> ] 35.09MB/70.73MB
c2274a1a0e27: Extracting [==========================> ] 37.32MB/70.73MB
c2274a1a0e27: Extracting [===========================> ] 39.55MB/70.73MB
c2274a1a0e27: Extracting [=============================> ] 42.34MB/70.73MB
c2274a1a0e27: Extracting [===============================> ] 45.12MB/70.73MB
c2274a1a0e27: Extracting [=================================> ] 47.91MB/70.73MB
c2274a1a0e27: Extracting [===================================> ] 49.58MB/70.73MB
c2274a1a0e27: Extracting [====================================> ] 51.81MB/70.73MB
c2274a1a0e27: Extracting [======================================> ] 54.03MB/70.73MB
c2274a1a0e27: Extracting [=======================================> ] 56.26MB/70.73MB
c2274a1a0e27: Extracting [=========================================> ] 58.49MB/70.73MB
c2274a1a0e27: Extracting [==========================================> ] 60.72MB/70.73MB
c2274a1a0e27: Extracting [============================================> ] 62.95MB/70.73MB
c2274a1a0e27: Extracting [=============================================> ] 64.62MB/70.73MB
c2274a1a0e27: Extracting [==============================================> ] 65.73MB/70.73MB
c2274a1a0e27: Extracting [===============================================> ] 66.85MB/70.73MB
c2274a1a0e27: Extracting [================================================> ] 68.52MB/70.73MB
c2274a1a0e27: Extracting [=================================================> ] 70.19MB/70.73MB
c2274a1a0e27: Extracting [==================================================>] 70.73MB/70.73MB
c2274a1a0e27: Pull complete
Digest: sha256:94792824df2df33402f201713f932b58cb9de94a0cd524164a0f2283343547b3
Status: Downloaded newer image for openjdk:8-jdk-alpine
---> a3562aa0b991
Step 2/4 : VOLUME /tmp

---> Running in 9866501a71d4
Removing intermediate container 9866501a71d4
---> f4247134db56
Step 3/4 : ADD demo-1.0.0-SNAPSHOT.jar app.jar

---> 9fe41d8b6c79
Step 4/4 : ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

---> Running in 619d8b8027ef
Removing intermediate container 619d8b8027ef
---> 5567045a3623
ProgressMessage{id=null, status=null, stream=null, error=null, progress=null, progressDetail=null}
Successfully built 5567045a3623
Successfully tagged demo-docker/demo:latest
[INFO] Built demo-docker/demo
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 58.843 s
[INFO] Finished at: 2020-01-16T15:58:07+08:00
[INFO] Final Memory: 27M/459M
[INFO] ------------------------------------------------------------------------

此时我们登录远程机器,查看镜像是否发布上来。

SpringBoot应用docker化并发布到远程服务器

可以看到已经构建成镜像并上传到镜像仓库了,并且image id 都一致。那么此时已经大功告成了!镜像都有了,直接一条命令构建容器并启动就可以了!激动人心得时刻即将到来!

1
docker run -p 8181:8080 --name demo -d 5567045a3623

正常启动,查看项目允许日志

1
docker logs -f demo

SpringBoot应用docker化并发布到远程服务器

至此大功告成!鼓掌!

相关文章

评论系统未开启,无法评论!