使用Maven 插件构建docker 镜像和推送仓库

版权声明:原创文章欢迎转载,不过要记得加出处哦 https://blog.csdn.net/wljk506/article/details/78528766

案例 spring cloud eureka server 构建 docker 镜像 和推送仓库
fox.风

项目 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>

    <groupId>com.foxwho</groupId>
    <artifactId>fox-eureka-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>fox-eureka-server</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.BUILD-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.BUILD-SNAPSHOT</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <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>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <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>
                <executions>
                    <!--执行 mvn package 时 自动 执行 mvn docker:build-->
                    <execution>
                        <id>build-image</id>
                        <phase>package</phase>
                        <goals>
                            <goal>build</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <!--推送到私有仓库-->
                    <!-- <imageName>localhost:5000/fox/fox-eureka-server</imageName> -->
                    <!--本地-->
                    <imageName>fox/fox-eureka-server</imageName>
                    <imageTags>
                        <imageTag>0.0.1</imageTag>
                        <!--可以指定多个标签-->
                        <imageTag>latest</imageTag>
                    </imageTags>
                    <!--覆盖已存在的标签 镜像-->
                    <forceTags>true</forceTags>
                    <!--使用pom 配置-->
                    <baseImage>java</baseImage>
                    <entryPoint>["java","-jar","/${project.build.finalName}.jar"]</entryPoint>
                    <!--使用pom 配置 END-->
                    <!--使用 Dockerfile-->
                    <!--<dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory>-->
                    <!--使用 Dockerfile  END-->
                    <resources>
                        <resource>
                            <targetPath>/</targetPath>
                            <directory>${project.build.directory}</directory>
                            <include>${project.build.finalName}.jar</include>
                        </resource>
                    </resources>
                </configuration>
            </plugin>
        </plugins>

    </build>

    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
        <pluginRepository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>
</project>

构建命令 及启动docker 镜像

mvn package
或
mvn clean package

启动docker 镜像

docker run -d -p 1111:1111  fox/fox-eureka-server:0.0.1 --name fox-eureka-server:0.0.1

1111 是 fox-eureka-server 内的端口

其他说明

自动构建docker 镜像配置主要在 plugin中的com.spotify
说明:

合并构建命令

                    <execution>
                        <id>build-image</id>
                        <phase>package</phase>
                        <goals>
                            <goal>build</goal>
                        </goals>
                    </execution>

此配置 主要是 执行 mvn package 时 自动 执行 mvn docker:build,如果没有此配置那么要执行

mvn pakage docker:buildmvn clean pakage docker:build

同时推送镜像到仓库

推送远程仓库

在MAVEN 的全局配置文件settings.xml加入或修改

<servers>
<server>
    <id>docker-hub</id>
    <username>docker Hub 的用户名</username>
    <password>docker Hub 的密码</password>
    <configuration>
        <email>docker Hub 的邮箱</email>
    </configuration>
</server>
</servers>

接着修改 pom.xml 中的 plugincom.spotifyconfiguration元素内增加

<!--就是settings.xml 中 server.id -->
<serverId>docker-hub</serverId>

执行命令

mvn  clean package -DpushImage

注意:pushImage标识,标识推送镜像标识,没有此标识,则不推送

推送到本地仓库

本地仓库很简单
修改 pom.xml 中的 plugincom.spotifyconfiguration.imageName元素增加 本地地址和端口即可
例如:本地 仓库地址及端口为 http://localhsot:5000
imageName

<imageName>fox/fox-eureka-server</imageName>

修改为

<imageName>localhost:5000/fox/fox-eureka-server</imageName>

执行命令

mvn clean package docker:build -DpushImage

注意:pushImage标识,标识推送镜像标识,没有此标识,则不推送

代码地址:https://github.com/foxiswho/example-java-spring-cloud/tree/master/fox-eureka-server

docker compose 编排

修改所有微服务配置eureka.client.serviceUrl.defaultZone (fox-eureka-server除外):

eureka.client.serviceUrl.defaultZone=http://peer1:1111/eureka/,http://peer2:1112/eureka/

注意,以上修改完成后,要重新构建及推送到仓库

docker-compose.yml 文件如下

version : "2"
services :
    peer1:
        image:fox/fox-eureka-server:0.0.1
        ports:
            - "1111:1111"
        environment:
            - spring.profiles.active=peer1
    peer2:
        image:fox/fox-eureka-server:0.0.1
        ports:
            - "1112:1112"
        environment:
            - spring.profiles.active=peer2

    service-provider:
        image:fox/fox-eureka-service-provider:0.0.1
    feign-service--consumer:
        image:fox/fox-eureka-feign-service-consumer:0.0.1
    api-gateway:
        image:fox/api-gateway:0.0.1
    config-eureka-server:
        image:fox/config-eureka-server:0.0.1
    config-eureka-client:
        image:fox/config-eureka-client:0.0.1
    fox-dashboard:
        image:fox/fox-dashboard:0.0.1

注意,这里只是一个 docker compose 案例,具体需要开启哪些端口需要 自己去配置

最后执行命令

docker-compose up

打开浏览器 网址 http://localhost:1111 即可看到效果

阅读更多

扫码向博主提问

风来了-

非学,无以致疑;非问,无以广识
  • 擅长领域:
  • php
  • java
  • go
  • 架构
  • more
去开通我的Chat快问
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页