当前位置: 移动技术网 > IT编程>开发语言>Java > idea连接docker实现一键部署的方法

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

2020年10月23日  | 移动技术网IT编程  | 我要评论
1.修改docker配置文件,打开2375端口[root@s162 docker]# vim /usr/lib/systemd/system/docker.service#查找 execstart,在

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

[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

在这里插入图片描述

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

3.1. 编写docker/dockerfile

在这里插入图片描述

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

 <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服务器上查看镜像是否上传成功:

在这里插入图片描述

3.4 docker:tag

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

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

 <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

在这里插入图片描述

3.5 docker:push

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

<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

在这里插入图片描述

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

<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 最佳实践

 <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安装的步骤详解(补充)

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

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网