美文网首页
4.docker-maven-plugin

4.docker-maven-plugin

作者: 元代码 | 来源:发表于2018-04-21 22:49 被阅读0次

    在前两篇文章2.Docker-resigtry23.SpringCloud-Eureka-Docker基础上进行配置。

    例子使用com.spotify的maven插件,后续会对比研究io.fabric3的maven插件。

    1.将插件配置到pom文件中

    1.1 定义变量

    配置hosts文件,加入59.110.14.120 docker.registry.server

    <properties>
    
    <docker.registry.server>docker.registry.server:5000</docker.registry.server>
    
    </properties>
    

    1.2 配置插件

    <plugin>
    
    <groupId>com.spotify</groupId>
    
    <artifactId>docker-maven-plugin</artifactId>
    
    <version>3.7.0</version>
    
    <configuration>
    
    <!--setting.xml的id-->
    
    <serverId>docker.registry.server</serverId>
    
    <!--推送到私有仓库-->
    
    <imageName>${docker.registry.server}/${project.artifactId}</imageName>
    
    <!--推送到本地仓库-->
    
    <!--<imageName>${project.artifactId}</imageName>-->
    
    <!--使用dockerfile构建镜像-->
    
    <!--<dockerDirectory>docker</dockerDirectory>-->
    
    <!--标签定义命令-->
    
    <baseImage>openjdk:8u131-jdk-alpine</baseImage>
    
    <entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>
    
    <resources>
    
    <resource>
    
    <targetPath>/</targetPath>
    
    <directory>${project.build.directory}</directory>
    
    <include>${project.build.finalName}.jar</include>
    
    </resource>
    
    </resources>
    
    </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>${docker.registry.server}/${project.artifactId}</image>
    
    <newName>${docker.registry.server}/${project.artifactId}</newName>
    
    </configuration>
    
    </execution>
    
    </executions>
    
    </plugin>
    

    1.3 setting.xml配置

    <server>
    
    <id>docker.registry.server</id>
    
    <username>gaojingyuan</username>
    
    <password>testpwd</password>
    
    <configuration>
    
    <email>gaojingyuanyuan@126.com</email>
    
    </configuration>
    
    </server>
    

    2.打包并推送image

    mvn clean package -DpushImage
    

    3. 服务器下载并启动服务

    登录私服

    docker login docker.registry.server:5000
    

    下载镜像

    docker pull docker.registry.server:5000/eurekaserverdocker
    

    启动服务

    docker stack deploy -c docker-compose.yml eurekaserverdocker
    

    停止服务

    docker stack rm eurekaserverdocker
    

    常见错误:

    1.执行mvn clean package -DpushImage5次retry后报错:

    Failed to push docker.registry.server:5000/eurekaserverdocker, retrying in 10 seconds (5/5).
    
    com.spotify.docker.client.exceptions.DockerException: no basic auth credentials
    

    解决方法:

    需要配置 vim .docker/config.json

    把"docker.registry.server:5000": {},改为"https://docker.registry.server:5000": {},

    "auths": {
    
    "https://docker.registry.server:5000": {},
    
    "https://index.docker.io/v1/": {}
    
    },
    

    https://github.com/spotify/docker-client/issues/804

    2.执行mvn clean package -DpushImage报错:

    Failed to execute goal com.spotify:docker-maven-plugin:1.0.0:tag (tag-image) on project eurekaserverdocker: Exception caught: Image not found: eurekaserverdocker: Request error: POST unix://localhost:80/images/eurekaserverdocker/tag?repo=docker.registry.server%3A5000%2Feurekaserverdocker: 404, body: {"message":"No such image: eurekaserverdocker:latest"}: HTTP 404 Not Found -> [Help 1]
    

    解决方法:

    需要正确配置<image>和<newName>

    <execution>
    
    <id>tag-image</id>
    
    <phase>package</phase>
    
    <goals>
    
    <goal>tag</goal>
    
    </goals>
    
    <configuration>
    
    <image>${docker.registry.server}/${project.artifactId}</image>
    
    <newName>${docker.registry.server}/${project.artifactId}</newName>
    
    </configuration>
    
    </execution>
    

    参考

    https://github.com/spotify/docker-maven-plugin

    https://www.jianshu.com/p/3b91b8958c3e

    相关文章

      网友评论

          本文标题:4.docker-maven-plugin

          本文链接:https://www.haomeiwen.com/subject/vugflftx.html