idea连接docker实现一键部署的方法

时间:2021-11-22 00:45:46

1.修改docker配置文件,打开2375端口

?
1
2
3
4
5
6
7
8
9
10
11
[root@s162 docker]# vim /usr/lib/systemd/system/docker.service
#查找 execstart,在末尾添加
#后面加上-h tcp://0.0.0.0:2375
 
[root@s162 docker]# systemctl daemon-reload
[root@s162 docker]# systemctl start docker
 
## 查看2375端口是否启用
[root@s162 docker]# lsof -i:2375
command pid user fd type device size/off node name
dockerd 27021 root 5u ipv6 352598799  0t0 tcp *:2375 (listen)

2. idea安装配置docker插件

2.1. idea-plugins市场安装docker插件

略…

2.2. 配置docker

idea连接docker实现一键部署的方法

3.springboot项目部署到docker服务器

3.1. 编写docker/dockerfile

idea连接docker实现一键部署的方法

3.2.maven添加docker-maven-plugin插件

?
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
<plugin>
  <groupid>com.spotify</groupid>
  <artifactid>docker-maven-plugin</artifactid>
  <version>1.0.0</version>
  <configuration>
   <!--指定生成的镜像名,如果不指定tag,默认会使用latest-->
   <imagename>jhs/${project.artifactid}:${project.version}</imagename>
   <!--添加额外的指定标签, 非必须-->
   <!--
   <imagetags>
    <imagetag>${project.version}</imagetag>
   </imagetags>
   -->
 
   <!-- 指定 dockerfile 路径 :项目根路径下-->
   <dockerdirectory>${project.basedir}/docker</dockerdirectory>
   <!--指定远程 docker api地址-->
   <dockerhost>http://192.168.129.162:2375</dockerhost>
 
 
   <!-- copy资源 -->
   <resources>
    <resource>
     <targetpath>/</targetpath>
     <directory>${project.build.directory}</directory>
     <include>${project.build.finalname}.jar</include>
    </resource>
   </resources>
 
 
   <!--docker build dockerfile时:设置镜像创建时的变量 -->
   <buildargs>
    <jar_file>target/${project.build.finalname}.jar</jar_file>
   </buildargs>
  </configuration>
 </plugin>

3.3. docker:build

使用命令$ mvn clean package docker:build -dmaven.test.skip=true构建镜像,在docker服务器上查看镜像是否上传成功:

idea连接docker实现一键部署的方法

3.4 docker:tag

docker命令行格式为#docker tag <imageid or imagename> <nexus-hostname>:<repository-port>/<image>:<tag>

插件配置
<configuration>补充配置:

?
1
2
3
4
5
<configuration>
     <image>jhs/${project.artifactid}:${project.version}</image>
  <!-- docker tag 打标签-->
  <newname>192.168.129.160:5000/${project.artifactid}:${project.version}</newname>
</configuration>

为镜像打上tag标签,为后续的push做准备:mvn clean docker:tag -dmaven.test.skip=true -dskipdockerbuild

idea连接docker实现一键部署的方法

3.5 docker:push

插件配置
<configuration>补充配置:

?
1
2
3
4
5
6
7
8
9
<configuration>
     <!-- docker push 推送到远程镜像仓库-->
  <!-- serverid: 为在maven setting.xml配置的server信息id-->
  <serverid>nexus-docker-registry</serverid>
  <registryurl>192.168.129.160:5000</registryurl>
    
    <!-- 打上tag的新镜像 push 到nexus-->
        <imagename>192.168.129.160:5000/${project.artifactid}</imagename>
</configuration>

将上文打上tag标签的镜像,推送到私服nexus:mvn clean docker:push -dmaven.test.skip=true -dskipdockerbuild -dskipdockertag

idea连接docker实现一键部署的方法

3.6 docker插件参数

  • -dskipdockerbuild to skip image build
  • -dskipdockertag to skip image tag
  • -dskipdockerpush to skip image push
  • -dskipdockerto skip any docker goals

3.7 绑定命令到maven phases

?
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
<executions>
  <execution>
   <id>build-image</id>
   <phase>package</phase>
   <goals>
    <goal>build</goal>
   </goals>
  </execution>
 
  <execution>
   <id>tag-image</id>
   <phase>package</phase>
   <goals>
    <goal>tag</goal>
   </goals>
   <configuration>
    <image>jhs/${project.artifactid}:${project.version}</image>
    <newname>192.168.129.160:5000/${project.artifactid}:${project.version}</newname>
   </configuration>
  </execution>
 
 
  <execution>
   <id>push-image</id>
   <phase>deploy</phase>
   <goals>
    <goal>push</goal>
   </goals>
   <configuration>
    <!-- docker push 推送到远程镜像仓库-->
    <!-- serverid: 为在maven setting.xml配置的server信息id-->
    <serverid>nexus-docker-registry</serverid>
    <registryurl>192.168.129.160:5000</registryurl>
    <imagename>192.168.129.160:5000/${project.artifactid}</imagename>
   </configuration>
  </execution>
 
 </executions>

3.8 最佳实践

?
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
<properties>
  <docker.host>http://192.168.129.162:2375</docker.host>
  <docker.registry.url>192.168.129.160:5000</docker.registry.url>
 </properties>
 
<build>
 <plugins>
    <plugin>
  <groupid>com.spotify</groupid>
  <artifactid>docker-maven-plugin</artifactid>
  <version>1.0.0</version>
  <configuration>
   <imagename>dic/${project.artifactid}:${project.version}</imagename>
   <!--添加额外的指定标签(可配置多个), 若果没做指定,则为latest-->
   <!--
    <imagetags>
     <imagetag>${project.version}</imagetag>
    </imagetags>
    -->
 
 
   <!-- 指定 dockerfile 路径 :项目根路径下-->
   <dockerdirectory>${project.basedir}/docker</dockerdirectory>
   <!--指定远程 docker api地址-->
   <dockerhost>${docker.host}</dockerhost>
 
 
   <!-- copy资源 -->
   <resources>
    <resource>
     <targetpath>/</targetpath>
     <directory>${project.build.directory}</directory>
     <include>${project.build.finalname}.jar</include>
    </resource>
   </resources>
 
 
   <!--docker build dockerfile时:设置镜像创建时的变量 -->
   <buildargs>
    <jar_file>target/${project.build.finalname}.jar</jar_file>
   </buildargs>
  </configuration>
 
  <executions>
   <execution>
    <id>build-image</id>
    <phase>package</phase>
    <goals>
     <goal>build</goal>
    </goals>
   </execution>
 
   <execution>
    <id>tag-image</id>
    <phase>package</phase>
    <goals>
     <goal>tag</goal>
    </goals>
    <configuration>
     <image>dic/${project.artifactid}:${project.version}</image>
     <newname>${docker.registry.url}/${project.artifactid}:${project.version}</newname>
    </configuration>
   </execution>
 
 
   <execution>
    <id>push-image</id>
    <phase>deploy</phase>
    <goals>
     <goal>push</goal>
    </goals>
    <configuration>
     <!-- docker push 推送到远程镜像仓库-->
     <!-- serverid: 为在maven setting.xml配置的server信息id-->
     <serverid>nexus-docker-registry</serverid>
     <registryurl>${docker.registry.url}</registryurl>
     <imagename>${docker.registry.url}/${project.artifactid}</imagename>
    </configuration>
   </execution>
 
  </executions>
 </plugin>
 </plugins>
 </build>

4.docker私服仓库harbor安装的步骤详解(补充)

http://www.zzvips.com/article/151025.html

到此这篇关于idea连接docker实现一键部署的文章就介绍到这了,更多相关idea连接docker一键部署内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/it_freshman/article/details/109201610