springboot maven docker 插件部署
一、环境配置
windows环境
1. 设置环境变量
默认情况下,执行mvn docker:build -DpushImage命令时,将尝试连接到localhost:2375上的docker。将DOCKER_HOST环境变量设置为在其他位置连接。
DOCKER_HOST=tcp://:2375
2. docker开启远程服务
接下来要想在windows中操作远程linux中的docker,那前提是必须开启docker远程API。
修改docker配置文件#vi /usr/lib/systemd/system/docker.service ,进入编辑模式后,将ExecStart这一行后面加上 -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock ,改完后如下所示
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock
这里就写4个0,你可别改成自己的ip哦,unix:///var/run/docker.sock 保存linux机器docker 命令。保存后退出,重新加载配置文件#systemctl daemon-reload ,启动docker #systemctl start docker ,
输入#netstat -anp|grep 2375 显示docker正在监听2375端口,输入#curl 127.0.0.1:2375/info 显示一大堆信息,证明远程api就弄好了
二、配置maven pom
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!-- 没有该配置,devtools 不生效 -->
<fork>true</fork>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<configuration>
<imageName>${project.artifactId}:${project.version}</imageName>
<dockerDirectory>${project.basedir}</dockerDirectory>
<skipDockerBuild>false</skipDockerBuild>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
</plugins>
</build>
对于多模块项目 对依赖的项目不需要生成image <skipDockerBuild>true</skipDockerBuild> 设置为true
项目根目录下加入dockerfile
FROM openjdk:8-jre-slim
MAINTAINER Rubble
ENV PARAMS=""
#中国标准时间
ENV TZ=PRC
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
ADD target/paw-uaa-*.jar /app.jar
ENTRYPOINT ["sh","-c","java -jar $JAVA_OPTS /app.jar $PARAMS"]
运行命令 mvn clean package docker:build
多个项目只生成一个 mvn clean package -pl paw-zuul -am docker:build
mvn clean package docker:build
服务端 docker images 既可以看到生成的镜像,docker run 运行镜像,查看日志 docker log myapp 或docker log -f myapp
docker run --name myapp -p 8080:8080 app:version
idea docker 插件,非常方便,配置docker远程服务后使用

##坑=======================
有依赖的项目 <skipDockerBuild>true</skipDockerBuild>
没有主函数 ,只能加在有main的模块
<goals>
<goal>repackage</goal>
</goals>
docker 会虚拟一些网卡作为内网,注册到注册中心nacos时内网地址, nacos在同一虚拟机可不做处理,直接访问即可。
本文详细介绍如何在Windows环境下配置Maven与Docker插件,实现SpringBoot项目的Docker镜像构建与部署。涵盖环境变量设置、Docker远程服务启用、Maven POM配置、Dockerfile编写及常见问题解决方案。
423

被折叠的 条评论
为什么被折叠?



