探索學(xué)習(xí)SpringBoot使用IDEA-Docker遠(yuǎn)程部署過程,已在虛擬機(jī)上安裝好了Docker,此篇文章從配置docker遠(yuǎn)程連接端口寫起。
一、配置docker遠(yuǎn)程連接端口
1苟翻、修改docker.service文件
vi /usr/lib/systemd/system/docker.service
找到 ExecStart,在最后面添加 -H tcp://0.0.0.0:2375骗污,如下圖所示
[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock -H tcp://0.0.0.0:2375
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutSec=0
RestartSec=2
Restart=always
2崇猫、重新加載配置文件,重啟服務(wù)
#重新加載配置文件
systemctl daemon-reload
# 重啟服務(wù)
systemctl restart docker.service
# 查看端口是否開啟
netstat -nlpt
查看端口是否開啟需忿,如下圖所示:
[root@localhost ~]# vi /usr/lib/systemd/system/docker.service
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl restart docker.service
[root@localhost ~]# netstat -nlpt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1016/nginx: master
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 993/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1087/master
tcp6 0 0 :::2375 :::* LISTEN 1487/dockerd
tcp6 0 0 :::22 :::* LISTEN 993/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1087/master
3诅炉、開放端口
#檢查端口是否打開
firewall-cmd --query-port=2375/tcp
#臨時(shí)打開端口
firewall-cmd --add-port=2375/tcp
#永久打開端口
firewall-cmd --zone=public --add-port=2375/tcp --permanent
二蜡歹、IDEA配置
1、安裝插件Docker integration涕烧,重啟
2月而、連接遠(yuǎn)程Docker
配置好后頁面下方會(huì)出現(xiàn)Docker工具欄(如IDEA沒有點(diǎn)擊view-Tool Windows可調(diào)出來)
3、新建Springboot項(xiàng)目(步驟不贅述)
4议纯、配置pom文件
<build>
<finalName>${project.parent.artifactId}-${project.parent.version}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.0.0</version>
<configuration>
<dockerDirectory>src/main/docker</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<configuration>
<tasks>
<copy todir="src/main/docker" file="target/${project.artifactId}-${project.version}.${project.packaging}"></copy>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
5父款、在src/main目錄下創(chuàng)建docker目錄,并創(chuàng)建Dockerfile文件
FROM openjdk:8-jdk-alpine
ADD *.jar app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
6瞻凤、增加配置
命令解釋:
- Image tag : 指定鏡像名稱和tag憨攒,鏡像名稱為 docker-workapi,tag為1.1
- Bind ports : 綁定宿主機(jī)端口到容器內(nèi)部端口阀参。格式為[宿主機(jī)端口]:[容器內(nèi)部端口]
- Bind mounts : 將宿主機(jī)目錄掛到到容器內(nèi)部目錄中肝集。格式為[宿主機(jī)目錄]:[容器內(nèi)部目錄]。這個(gè)springboot項(xiàng)目會(huì)將日志打印在容器 /home/developer/app/logs/ 目錄下结笨,將宿主機(jī)目錄掛載到容器內(nèi)部目錄后,那么日志就會(huì)持久化容器外部的宿主機(jī)目錄中湿镀。
7炕吸、使用Maven打包
8、運(yùn)行Docker
過程:先pull基礎(chǔ)鏡像勉痴,然后再打包鏡像赫模,并將鏡像部署到遠(yuǎn)程docker運(yùn)行
8、啟動(dòng)成功
可以訪問到接口
9蒸矛、日志查看
三瀑罗、使用Nginx反向代理請求Docker容器里運(yùn)行的jar
1、nginx添加配置后reload
server {
listen 80;
server_name 192.168.197.90;
charset utf-8;
location /yimall {
alias /www/web/www.yimall.pro/dist;
try_files $uri $uri/ /index.html last;
index index.html;
}
location /api {
proxy_pass http://127.0.0.1:8081;
}
access_log /www/logs/www.yimall.log;
error_log /www/logs/www.yimall.error.log;
}
2雏掠、docker配置宿主機(jī)端口到容器內(nèi)部端口
情況一:沒用到nginx反向代理斩祭,直接請求宿主機(jī)8081端口
如果是下圖,HoastIP不填乡话,其映射結(jié)果為 0.0.0.0:8081->8081/tcp摧玫,此時(shí)宿主機(jī)的8081端口開啟,需要這樣訪問 http://192.168.197.90:8081/api/member/get/113
情況二:用nginx反向代理绑青,請求宿主機(jī)80端口诬像,代理到8081端口
如果是下圖,HoastIP填127.0.0.1闸婴,其映射結(jié)果為 127.0.0.1:8081->8081/tcp坏挠,此時(shí)宿主機(jī)的8081端口未對外開放,訪問 http://192.168.197.90/api/member/get/113邪乍,會(huì)被nginx反向代理到 http://127.0.0.1:8081/api/member/get/113降狠,即可訪問接口API
四对竣、遇到的問題
啟動(dòng)容器時(shí),控制臺報(bào)錯(cuò)如下:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource'
defined in class path resource [com/alibaba/druid/spring/boot/autoconfigure/DruidDataSourceAutoConfigure.class]: Invocation of init method failed; nested exception is com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
Caused by: java.net.NoRouteToHostException: Host is unreachable (Host unreachable)
解決:打開服務(wù)器3306端口
#臨時(shí)打開端口
firewall-cmd --add-port=3306/tcp
#永久打開端口
firewall-cmd --zone=public --add-port=3306/tcp --permanent