dockerfile-maven-pluging 远程docker服务打包到docker私服

531 阅读2分钟

dockerfile-maven-pluging 远程docker服务打包到docker私服

构建镜像的两种方式

[方式一] 远程docker打包镜像,上传私服

此种方式在开发环境没有安装docker软件,本地只有要打包发布的版本和dockerfile作为构建镜像的配置文件,具体配置流程如下:

1.配置springboot项目的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 http://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.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.taihehospital</groupId>
    <artifactId>helloworld</artifactId>
    <version>1.0.0</version>
    <name>helloworld</name>
    <description>这是一个Spingboot的Web示例</description>

    <properties>
        <java.version>1.8</java.version>
        <!--docker私服地址-->
        <docker.repostory>x.x.x.x:90031</docker.repostory>
    </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>org.apache.maven.plugins</groupId>
                <artifactId>maven-deploy-plugin</artifactId>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>dockerfile-maven-plugin</artifactId>
                <version>1.4.10</version>
                <executions>
                    <execution>
                        <id>default</id>
                        <goals>
                            <!--如果package时不想docker打包,就注释掉这个goal-->
                            <goal>build</goal>
                            <goal>push</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <repository>${docker.repostory}/${project.artifactId}</repository>
                    <tag>${project.version}</tag>
                    <buildArgs>
                        <!--提供参数向Dockerfile传递-->
                        <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
                    </buildArgs>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
<docker.repostory>x.x.x.x:90031</docker.repostory>   
这个地址需要跟 远程docker的私有仓库地址保持一致,并且需要在远程服务器下 /etc/docker/daemon.json这个配置文件中 的
{
   "registry-mirrors": [
     "https://qpzfzury.mirror.aliyuncs.com",
     "https://mirror.ccs.tencentyun.com",
     "http://hub-mirror.c.163.com"
   ],
   "insecure-registries":["<docker.repostory>"]
}
这个insecure-registries中的地址要跟上述标主的保持一致,如果还有别的地址要加进去
此配置的作用就是让私服信任这个地址,并且默认采用Http的方式进行上传镜像

2. 开启docker 的远程访问端口
cd /lib/systemd/system/docker.service

此处端口作为对外提供访问入口 ExecStart=/usr/bin/dockerd -H unix://var/run/docker.sock -H tcp://0.0.0.0:90032


3.开发环境配置添加环境变量
DOCKER_HOST:tcp://0.0.0.0:90032
这个需要跟docker的远程开启端口匹配上   

注意: 此选项作为打包后传递到 docker私服的地址,这里要跟上文deamon.json中的配置一样
开发环境的配置要跟远程docker的远程端口保持一致

开发环境变量和dokcer远程端口是作为构建镜像的,pom配置文件中的地址是具体docker私服的指向

<configuration>
    <repository>${docker.repostory}/${project.artifactId}</repository>
    <tag>${project.version}</tag>
    <buildArgs>
        <!--提供参数向Dockerfile传递-->
        <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
    </buildArgs>
</configuration>

参考资料:blog.csdn.net/zsx1314love…

[方式二] 本地docker打包镜像,上传私服

    此种方式主要在于通过 maven私服作为镜像的存储点,但是需要开发环境进行docker-desktop的安装,还需要安装WSL,在于本地构建镜像,maven私服存储镜像,好处就是便捷,不占用服务器资源
    

此种方式 就是通过 docker 安装Nexus私服,然后通过Nexus 作为docker镜像的存储点

个人理解:
方式一:比较简便,开发环境配置少,方式二:开发环境配置少,但是存储的更加可控 方式二需要开发环境和Nexus的端口进行匹配,有些配置默认参考universal项目

参考资料:www.jianshu.com/p/86e573f18… 我使用的是第一种打包方式