探索学习SpringBoot使用IDEA-Docker远程部署过程,已在虚拟机上安装好了Docker,此篇文章从配置docker远程连接端口写起。
一、配置docker远程连接端口
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、重新加载配置文件,重启服务
#重新加载配置文件
systemctl daemon-reload
# 重启服务
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
#临时打开端口
firewall-cmd --add-port=2375/tcp
#永久打开端口
firewall-cmd --zone=public --add-port=2375/tcp --permanent
二、IDEA配置
1、安装插件Docker integration,重启
2、连接远程Docker
配置好后页面下方会出现Docker工具栏(如IDEA没有点击view-Tool Windows可调出来)
3、新建Springboot项目(步骤不赘述)
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目录下创建docker目录,并创建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 : 绑定宿主机端口到容器内部端口。格式为[宿主机端口]:[容器内部端口]
- Bind mounts : 将宿主机目录挂到到容器内部目录中。格式为[宿主机目录]:[容器内部目录]。这个springboot项目会将日志打印在容器 /home/developer/app/logs/ 目录下,将宿主机目录挂载到容器内部目录后,那么日志就会持久化容器外部的宿主机目录中。
7、使用Maven打包
8、运行Docker
过程:先pull基础镜像,然后再打包镜像,并将镜像部署到远程docker运行
8、启动成功
可以访问到接口
9、日志查看
三、使用Nginx反向代理请求Docker容器里运行的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配置宿主机端口到容器内部端口
情况一:没用到nginx反向代理,直接请求宿主机8081端口
如果是下图,HoastIP不填,其映射结果为 0.0.0.0:8081->8081/tcp,此时宿主机的8081端口开启,需要这样访问 http://192.168.197.90:8081/api/member/get/113
情况二:用nginx反向代理,请求宿主机80端口,代理到8081端口
如果是下图,HoastIP填127.0.0.1,其映射结果为 127.0.0.1:8081->8081/tcp,此时宿主机的8081端口未对外开放,访问 http://192.168.197.90/api/member/get/113,会被nginx反向代理到 http://127.0.0.1:8081/api/member/get/113,即可访问接口API
四、遇到的问题
启动容器时,控制台报错如下:
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)
解决:打开服务器3306端口
#临时打开端口
firewall-cmd --add-port=3306/tcp
#永久打开端口
firewall-cmd --zone=public --add-port=3306/tcp --permanent