14Docekr整合idea

目录

一、手动实现 

编写代码

打包 

编写Dockerfile文件

上传至服务器 

 docker build

查看镜像 docker images

运行和查看容器

访问测试 

二、idea整合docker实现镜像打包一件部署

1、Docker开启远程访问

2、IDEA安装Docker插件

3、IDEA配置docker

配置docker

连接成功后,在IDEA工具中即可 操作Docker:

删除镜像/容器​ 

创建容器 

4、docker-maven-plugin

pom.xml

Dockerfile

5、执行命令

6、IDEA 操作Docker

7、扩展配置

拓展 

完整pom.xml 

总结:


一、手动实现 

编写代码

  

打包 

编写Dockerfile文件

上传至服务器 

[root@gh mydocker-test]# ls
docker-1.0.jar  Dockerfile
[root@gh mydocker-test]# cat Dockerfile 
FROM java:8
MAINTAINER gh 2495140780@qq.com

COPY *.jar /docker-1.0.jar

CMD ["------server.port=8080"]

EXPOSE 8080

ENTRYPOINT ["java", "-jar", "docker-1.0.jar"]


 docker build

[root@gh mydocker-test]# docker build -t mydocker .
Sending build context to Docker daemon  17.54MB
Step 1/6 : FROM java:8
 ---> d23bdf5b1b1b
Step 2/6 : MAINTAINER gh 2495140780@qq.com
 ---> Running in 5bb835215683
Removing intermediate container 5bb835215683
 ---> 0631690b0d57
Step 3/6 : COPY *.jar /docker-1.0.jar
 ---> 77d98e65849e
Step 4/6 : CMD ["------server.port=8080"]
 ---> Running in ed34a450f338
Removing intermediate container ed34a450f338
 ---> dac15e5a61d9
Step 5/6 : EXPOSE 8080
 ---> Running in 09a3f3bcb397
Removing intermediate container 09a3f3bcb397
 ---> 6c9ae1f01727
Step 6/6 : ENTRYPOINT ["java", "-jar", "docker-1.0.jar"]
 ---> Running in 8c7f0e2f0e9c
Removing intermediate container 8c7f0e2f0e9c
 ---> 502d34a537fb
Successfully built 502d34a537fb
Successfully tagged mydocker:latest

查看镜像 docker images

[root@gh mydocker-test]# docker images
REPOSITORY      TAG       IMAGE ID       CREATED         SIZE
mydocker        latest    502d34a537fb   2 seconds ago   661MB

运行和查看容器

[root@gh mydocker-test]# docker run -d -p 8080:8080 --name docker-idea mydocker 
dfca284726b366e724e9e426793ffc7c7e412b92d2999f3f29a8311ec691ab21

[root@gh mydocker-test]# docker ps
CONTAINER ID   IMAGE      COMMAND                  CREATED         STATUS         PORTS                    NAMES
dfca284726b3   mydocker   "java -jar docker-1.…"   7 seconds ago   Up 6 seconds   0.0.0.0:8080->8080/tcp   docker-idea

访问测试 

二、idea整合docker实现镜像打包一件部署

1、Docker开启远程访问

 #修改该Docker服务文件
 vi /lib/systemd/system/docker.service 
 #修改ExecStart这行
 ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock

将文件内的 ExecStart注释。 新增如上行。

# ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375-H unix:///var/run/docker.sock

 #重新加载配置文件
 systemctl daemon-reload
 #重启服务
 systemctl restart docker.service
 #查看端口是否开启
 netstat -nlpt   #如果找不到netstat命令,可进行安装。yum install net-tools
 #直接curl看是否生效
 curl http://127.0.0.1:2375/info

2、IDEA安装Docker插件

3、IDEA配置docker

配置docker

连接到远程docker服务。 从File->Settings->Build,Execution,Deployment->Docker打开配置界面

https://gxeo3yz7.mirror.aliyuncs.com T

 出现表示链接成功

温馨提示:要开放防火墙端口 否则链接不成功

如何开放端口查看  :07Linux--环境安装_gh_xiaohe的博客-CSDN博客   本篇博客

个人建议使用 宝塔  命令 bt default

连接成功后,在IDEA工具中即可 操作Docker:

删除镜像/容器 

创建容器 

4、docker-maven-plugin

        传统过程中,打包、部署、等。 而在持续集成过程中,项目工程一般使用 Maven 编译打包,然后生成镜像,通过镜像上线,能够大大提供上线效 率,同时能够快速动态扩容,快速回滚,着实很方便。docker-maven-plugin 插件就是为了帮助我们在Maven工程中,通过简单的配置,自动生成镜像并推送到仓库中。

pom.xml

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.3.7.RELEASE</spring-boot.version>
        <!--        <docker.image.prefix></docker.image.prefix>-->
    </properties>

    <build>
        <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>
                <!-- 镜像名称 guoweixin/exam-->
                <imageName>${project.artifactId}</imageName>  <!--${docker.image.prefix}/-->
                <!--指定标签-->
                <imageTags>
                  <imageTag>${project.version}</imageTag>
                </imageTags>
                <!-- 基础镜像jdk 1.8-->
                <baseImage>java</baseImage>
                <!-- 制作者提供本人信息 -->
                <maintainer>2495140780@qq.com</maintainer>
                    <!--切换到/ROOT目录 -->
                    <workdir>/ROOT</workdir>
                    <cmd>["java", "-version"]</cmd>
                    <entryPoint>["java", "-jar", "${project.build.finalName}.jar"]</entryPoint>

                    <!-- 指定 Dockerfile 路径 -->
                    <dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory>

                    <!--指定远程 docker api地址-->
                    <dockerHost>http://xxx.xxx.xxx.xxx:2375</dockerHost>

                    <!-- 这里是复制 jar 包到 docker 容器指定目录配置 -->
                    <resources>
                        <resource>
                            <targetPath>/ROOT</targetPath>
                            <!--用于指定需要复制的根目录,${project.build.directory}表示target目录-->
                            <directory>${project.build.directory}</directory>
                            <!--用于指定需要复制的文件。${project.build.finalName}.jar指的是打包后的jar包文件。-->
                            <include>${project.build.finalName}.jar</include>
                        </resource>
                    </resources>
                </configuration>
            </plugin>

        </plugins>

    </build>

Dockerfile

如上用docker-maven插件 自动生成如下文件:

FROM java
MAINTAINER gh 2495140780@qq.com
WORKDIR /ROOT

COPY *.jar /docker-1.0.jar

ENTRYPOINT ["java", "-jar", "docker-1.0.jar"]

CMD ["java", "-version"]

5、执行命令

对项目进行 打包。并构建镜像 到Docker 上。

 mvn clean package docker:build

6、IDEA 操作Docker

7、扩展配置

绑定Docker 命令到 Maven 各个阶段

我们可以绑定 Docker 命令到 Maven 各个阶段, 我们可以把 Docker 分为 build、tag、push,然后分别绑定 Maven 的 package、deploy 阶段, 我们只需要执行 mvn deploy 就可以完成整个 build、tag、push操作了,当我们执行 mvn build 就只完成 build、tag 操作。

    特别注意目录要同级 建议手敲

               <!--当执行mvn package 时,执行: mvn clean package docker:build -->
                <executions>
                    <execution>
                        <id>build-image</id>
                        <phase>package</phase>
                        <goals>
                            <goal>build</goal>
                        </goals>
                    </execution>
                </executions>

拓展 

 <executions>
   <!--当执行mvn package 时,执行: mvn clean package docker:build -->
   <execution>
     <id>build-image</id>
     <phase>package</phase>
     <goals>
       <goal>build</goal>
     </goals>
   </execution>
  <!--当执行mvn package 时,会对镜像进行 标签设定-->
   <execution>
     <id>tag-image</id>
     <phase>package</phase>
     <goals>
       <goal>tag</goal>
     </goals>
     <configuration>
       <image>${docker.image.prefix}/${project.artifactId}:latest</image>
    <newName>docker.io/${docker.image.prefix}/${project.artifactId}:${project.version}
   </newName>
 ​
     </configuration>
   </execution>
   <execution>
     <id>push-image</id>
     <phase>deploy</phase>
     <goals>
       <goal>push</goal>
     </goals>
     <configuration>
       <imageName>docker.io/${docker.image.prefix}/${project.artifactId}:${project.version}
  </imageName>
     </configuration>
   </execution>
  
 </executions>

完整pom.xml 

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.5</version>
        <relativePath/>
    </parent>

    <groupId>com.gh</groupId>
    <artifactId>docker</artifactId>
    <version>1.0</version>
    <name>docker</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <docker.image.prefix>gh</docker.image.prefix>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <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>
                <!-- 镜像名称 guoweixin/exam-->
                <imageName>${docker.image.prefix}/${project.artifactId}</imageName>  <!--${docker.image.prefix}/-->
                <!--指定标签-->
                <imageTags>
                  <imageTag>${project.version}</imageTag>
                </imageTags>
                <!-- 基础镜像jdk 1.8-->
                <baseImage>java</baseImage>
                <!-- 制作者提供本人信息 -->
                <maintainer>2495140780@qq.com</maintainer>
                    <!--切换到/ROOT目录 -->
                    <workdir>/ROOT</workdir>
                    <cmd>["java", "-version"]</cmd>
                    <entryPoint>["java", "-jar", "${project.build.finalName}.jar"]</entryPoint>

                    <!-- 指定 Dockerfile 路径 -->
                    <dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory>

                    <!--指定远程 docker api地址-->
                    <dockerHost>http://101.201.234.58:2375</dockerHost>

                    <!-- 这里是复制 jar 包到 docker 容器指定目录配置 -->
                    <resources>
                        <resource>
                            <targetPath>/ROOT</targetPath>
                            <!--用于指定需要复制的根目录,${project.build.directory}表示target目录-->
                            <directory>${project.build.directory}</directory>
                            <!--用于指定需要复制的文件。${project.build.finalName}.jar指的是打包后的jar包文件。-->
                            <include>${project.build.finalName}.jar</include>
                        </resource>
                    </resources>
                 </configuration>
                <!--当执行mvn package 时,执行: mvn clean package docker:build -->
                <executions>
                    <execution>
                        <id>build-image</id>
                        <phase>package</phase>
                        <goals>
                            <goal>build</goal>
                        </goals>
                    </execution>
                </executions>

            </plugin>

        </plugins>

    </build>


</project>

总结:

当我们执行 mvn package 时,执行 build、tag 操作, 当执行 mvn deploy 时,执行build、tag、push 操作。 如果我们想跳过 docker 某个过程时,只需要:

DskipDockerBuild 跳过 build 镜像 -DskipDockerTag 跳过 tag 镜像 -DskipDockerPush 跳过 push 镜像 -DskipDocker跳过整个阶段 例如:我们想执行 package 时,跳过 tag 过程,那么就需要 mvn package -DskipDockerTag

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

gh-xiaohe

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值