一、整合步骤
1、开启Docker远程连接
既然要整合到IDEA,那么IDEA就需要连接Docker,所以我们需要开启Docker的远程连接。

使用以下命令编辑docker.service文件

1
vim /usr/lib/systemd/system/docker.service

添加如下内容

-H unix:///var/run/docker.sock -H tcp://0.0.0.0:2375

测试命令

1
2
netstat -nlpt   #查看端口占用否
curl 192.168.33.11:2375/version #如果能返回json数据,则证明成功

2、关闭防火墙(linux,自己测试用的)

  1. idea中配置docker

![image-20230611094407926](/Users/huage/Library/Application Support/typora-user-images/image-20230611094407926.png)

pom文件修改几处

  1. 镜像前缀
1
2
3
<properties>
<docker.image.prefix>ngit</docker.image.prefix>
</properties>
  1. docker-build插件
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
<build>
<plugins>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.0.0</version>
<configuration>
<imageName>${docker.image.prefix}/${project.artifactId}</imageName>
<imageTags>
<imageTag>latest</imageTag>
</imageTags>
<baseImage>anapsix/alpine-java:8_server-jre_unlimited</baseImage>
<maintainer>huage</maintainer>
<workdir>/ROOT</workdir>
<cmd>["java", "-version"]</cmd>
<entryPoint>["java", "-jar", "${project.build.finalName}.jar"]</entryPoint>
<dockerHost>http://192.168.33.11:2375</dockerHost>
<resources>
<resource>
<targetPath>/ROOT</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
</plugins>

</build>

另一版本

创建Dockerfile

1
2
3
4
5
6
FROM anapsix/alpine-java
MAINTAINER huage
VOLUME /tmp
ADD target/*.jar /app.jar
EXPOSE 8080
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom", "-jar", "/app.jar"]
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
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<!-- 此版本打包时没有报错 -->
<version>1.2.1</version>
<executions>
<execution>
<id>build-image</id>
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>

<configuration>
<imageName>ngit/${project.artifactId}</imageName>
<imageTags>
<imageTag>latest</imageTag>
</imageTags>

<dockerHost>http://192.168.33.11:2375</dockerHost>
<dockerDirectory>${project.basedir}</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>