spotify

 ·  ☕ 3  · 👀...

介绍

技术 特性 适用场景 说明
spotify - 支持Maven构建
- 支持Dockerfile构建
- 支持密码或加密密码验证
- 构建Docker镜像 - Github

构建镜像

配置

配置使用插件构建镜像

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <!-- docker的maven插件 -->
        <plugin>
            <groupId>com.spotify</groupId>
            <artifactId>docker-maven-plugin</artifactId>
            <version>0.4.12</version>
            <configuration>
                <!-- 注意imageName一定要是符合正则[a-z0-9-_.]的,否则构建不会成功 -->
                <imageName>demo8</imageName>
                <baseImage>java</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>
        </plugin>
    </plugins>
</build>

配置使用Dockerfile构建镜像

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <!-- docker的maven插件 -->
        <plugin>
            <groupId>com.spotify</groupId>
            <artifactId>docker-maven-plugin</artifactId>
            <version>0.4.12</version>
            <configuration>
                <!-- 注意imageName一定要是符合正则[a-z0-9-_.]的,否则构建不会成功 -->
                <imageName>demo8</imageName>
                <!-- 指定Dockerfile所在的路径 -->
                <dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory>
                <resources>
                    <resource>
                        <targetPath>/</targetPath>
                        <directory>${project.build.directory}</directory>
                        <include>${project.build.finalName}.jar</include>
                    </resource>
                </resources>
            </configuration>
        </plugin>
    </plugins>
</build>

更灵活的配置示例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<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.1.1</version>
            <!--插件绑定到phase-->
            <executions>
                <execution>
                    <phase>install</phase>
                    <goals>
                        <goal>build</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <!--配置变量,包括是否build、imageName、imageTag,非常灵活-->
                <skipDocker>${docker.skip.build}</skipDocker>
                <imageName>${docker.image.prefix}/${project.artifactId}</imageName>
                <!--最后镜像产生了两个tag,版本和和最新的-->
                <imageTags>
                    <imageTag>${project.version}</imageTag>
                    <imageTag>latest</imageTag>
                </imageTags>
                <forceTags>true</forceTags>
                <env>
                    <TZ>Asia/Shanghai</TZ>
                </env>
                <!--时区配置-->
                <runs>
                    <run>ln -snf /usr/share/zoneinfo/$TZ /etc/localtime</run>
                    <run>echo $TZ > /etc/timezone</run>
                </runs>
                <dockerDirectory>${project.basedir}</dockerDirectory>
                <resources>
                    <resource>
                        <targetPath>/</targetPath>
                        <directory>${project.build.directory}</directory>
                        <include>${project.build.finalName}.jar</include>
                    </resource>
                </resources>
            </configuration>
        </plugin>
    </plugins>
</build>

构建镜像

1
$ mvn clean package docker:build

将Docker镜像push到DockerHub上

配置settings.xml

修改Maven的全局配置文件settings.xml,添加如下内容:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<servers>
  <server>
    <id>docker-hub</id>
    <username>你的DockerHub用户名</username>
    <password>你的DockerHub密码</password>
    <configuration>
      <email>你的DockerHub邮箱</email>
    </configuration>
  </server>
</servers>

配置pom.xml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
<build>
    <plugins>
        <!-- docker的maven插件 -->
        <plugin>
                ......
                <!-- 以下两行是为了docker push到DockerHub使用的。 -->
                <serverId>docker-hub</serverId>
                <registryUrl>https://index.docker.io/v1/</registryUrl>
            </configuration>
        </plugin>
    </plugins>
</build>

推送

1
$ mvn clean package docker:build  -DpushImage

构建docker镜像并推送到阿里镜像仓库

配置pom.xml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<build>
    <plugins>
        <plugin>
            <groupId>com.spotify</groupId>
            <artifactId>docker-maven-plugin</artifactId>

            <configuration>
                <serverId>docker-aliyun</serverId>
                <registryUrl>registry.cn-hangzhou.aliyuncs.com</registryUrl>

                <baseImage>java:8</baseImage>
                <entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>

                <imageName>registry.cn-hangzhou.aliyuncs.com/company/image-name</imageName>

                <resources>
                    <resource>
                        <targetPath>/</targetPath>
                        <directory>${project.build.directory}</directory>
                        <include>${project.build.finalName}.jar</include>
                    </resource>
                </resources>
            </configuration>
        </plugin>
    </plugins>
</build>

配置docker

修改~/.docker/config.json,内容如下:

1
2
3
4
5
6
7
{
    "auths": {
        "registry.cn-hangzhou.aliyuncs.com": {
            "auth": "ZG9ja2VyQDM2MGVkaW******UAxODU2ODMyNjA0MDgzODA5OkRIQDM2MGVkaWFuIzIwMTch"
        }
    }
}

推送

1
$ mvn clean package docker:push

Wanglibing
Wanglibing
Engineer,Lifelong learner