使用Maven插件构建上传docker镜像

工具

插件名称 官方地址
docker-maven-plugin https://github.com/spotify/dockerfile-maven

使用插件构建Docker镜像

简单使用

我们以之前的项目:microservice-discovery-eureka为例:

  • 在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
<build>
<plugins>
<!-- docker的maven插件,官网:https://github.com/spotify/dockerfile-maven -->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.4.4</version>
<configuration>
<!-- 注意imageName一定要是符合正则[a-z0-9-_.]的,否则构建不会成功 -->
<!-- 详见:https://github.com/spotify/docker-maven-plugin Invalid repository name ... only [a-z0-9-_.] are allowed-->
<imageName>microservice-discovery-eureka</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>
  • 执行命令:
1
mvn clean package docker:build
  • 我们会发现控制台有类似如下内容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[INFO] Building image microservice-discovery-eureka
Step 1 : FROM java
Pulling from library/java
Digest: sha256:581a4afcbbedd8fdf194d597cb5106c1f91463024fb3a49a2d9f025165eb675f
Status: Downloaded newer image for java:latest
---> ea40c858f006
Step 2 : ADD /microservice-discovery-eureka-0.0.1-SNAPSHOT.jar //
---> d1c174083bca
Removing intermediate container 91913d847c20
Step 3 : ENTRYPOINT java -jar /microservice-discovery-eureka-0.0.1-SNAPSHOT.jar
---> Running in 0f2aeccdfd46
---> d57b027ca65a
Removing intermediate container 0f2aeccdfd46
Successfully built d57b027ca65a
[INFO] Built microservice-discovery-eureka
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:38 min
[INFO] Finished at: 2016-09-18T01:05:05-07:00
[INFO] Final Memory: 40M/198M

恭喜,构建成功了。

  • 我们执行docker images 会发现该镜像已经被构建成功:
1
2
REPOSITORY                      TAG                 IMAGE ID            CREATED              SIZE
microservice-discovery-eureka latest d57b027ca65a About a minute ago 681.5 MB
  • 启动镜像
1
docker run -p 8761:8761 microservice-discovery-eureka

我们会发现该Docker镜像会很快地启动。

  • 访问测试

访问http://Docker宿主机IP:8761 ,能够正常看到Eureka界面。

使用Dockerfile进行构建

上文讲述的方式是最简单的方式,很多时候,我们还是要借助Dockerfile进行构建的,首先我们在/microservice-discovery-eureka/src/main/docker目录下,建立文件Dockerfile

1
2
3
4
5
6
FROM java:8
VOLUME /tmp
ADD microservice-discovery-eureka-0.0.1-SNAPSHOT.jar app.jar
RUN bash -c 'touch /app.jar'
EXPOSE 9000
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

修改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
<build>
<plugins>
<!-- docker的maven插件,官网:https://github.com/spotify/docker-maven-plugin -->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.12</version>
<configuration>
<!-- 注意imageName一定要是符合正则[a-z0-9-_.]的,否则构建不会成功 -->
<!-- 详见:https://github.com/spotify/docker-maven-plugin Invalid repository name ... only [a-z0-9-_.] are allowed-->
<imageName>microservice-discovery-eureka-dockerfile</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>

其他步骤一样。这样即可使用Dockerfile进行构建Docker镜像啦。

将Docker镜像push到DockerHub上

  • 首先修改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>
  • 在DockerHub上创建repo,例如:test,如下图

DockerHub

  • 项目pom.xml修改为如下:注意imageName的路径要和repo的路径一致
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
<build>
<plugins>
<!-- docker的maven插件,官网:https://github.com/spotify/docker-maven-plugin -->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.12</version>
<configuration>
<!-- 注意imageName一定要是符合正则[a-z0-9-_.]的,否则构建不会成功 -->
<!-- 详见:https://github.com/spotify/docker-maven-plugin Invalid repository
name ... only [a-z0-9-_.] are allowed -->
<!-- 如果要将docker镜像push到DockerHub上去的话,这边的路径要和repo路径一致 -->
<imageName>eacdy/test</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>
<!-- 以下两行是为了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镜像已经被push到DockerHub上了。

将镜像push到私有仓库

在很多场景下,我们需要将镜像push到私有仓库中去,这边为了讲解的全面性,私有仓库采用的是配置登录认证的私有仓库。

  • Use the following command line:

    1
    mvn --encrypt-master-password <password>

    Note: This will not prompt for a password, so it must be typed on the command-line in plaintext. See Tips below for more information.

    This command will produce an encrypted version of the password, something like

    1
    {jSMOWnoPFgsHVpMvz5VrIt5kRbzGpI8u+9EF1iFQyJQ=}

    Store this password in the ~/.m2/settings-security.xml; it should look like

    1
    2
    3
    <settingsSecurity>
    <master>{jSMOWnoPFgsHVpMvz5VrIt5kRbzGpI8u+9EF1iFQyJQ=}</master>
    </settingsSecurity>

    When this is done, you can start encrypting existing server passwords.

    How to encrypt server passwords

    You will have to use the following command line:

    1
    mvn --encrypt-password <password>

    This command will produce an encrypted version of it, something like

    1
    {COQLCE6DU6GtcS5P=}

    Cut-n-paste it into your settings.xml file in the server section. This will look like:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    <settings>
    ...
    <servers>
    ...
    <server>
    <id>my.server</id>
    <username>foo</username>
    <password>{COQLCE6DU6GtcS5P=}</password>
    </server>
    ...
    </servers>
    ...
    </settings>

    Please note that password can contain any information outside of the curly brackets, so that the following will still work:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    <settings>
    ...
    <servers>
    ...
    <server>
    <id>my.server</id>
    <username>foo</username>
    <password>Oleg reset this password on 2009-03-11, expires on 2009-04-11 {COQLCE6DU6GtcS5P=}</password>
    </server>
    ...
    </servers>
    ...
    </settings>

    Then you can use, say, deploy plugin, to write to this server:

    1
    2
    3
    mvn deploy:deploy-file -Durl=https://maven.corp.com/repo \
    -DrepositoryId=my.server \
    -Dfile=your-artifact-1.0.jar \
  • 和push镜像到DockerHub中一样,我们首先需要修改Maven的全局配置文件settings.xml,添加以下段落

1
2
3
4
5
6
7
8
9
10
<servers>
<server>
<id>docker-registry</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
13
14
15
16
17
18
19
20
21
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.12</version>
<configuration>
<!-- 路径为:私有仓库地址/你想要的镜像路径 -->
<imageName>reg.itmuch.com/test-pull-registry</imageName>
<dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory>

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

<!-- 与maven配置文件settings.xml一致 -->
<serverId>docker-registry</serverId>
</configuration>
</plugin>
  • 执行:
1
mvn clean package docker:build  -DpushImage

稍等片刻,将会push成功。

  • 如果想要从私服上下载该镜像,执行:
1
2
docker login reg.itmuch.com  # 然后输入账号和密码
docker pull reg.itmuch.com/test-pull-registry

将插件绑定在某个phase执行

在很多场景下,我们有这样的需求,例如执行mvn clean package 时,自动地为我们构建docker镜像,可以吗?答案是肯定的。我们只需要将插件的goal 绑定在某个phase即可。

所谓的phase和goal,可以这样理解:maven命令格式是:mvn phase:goal ,例如mvn package docker:build 那么,packagedocker 都是phase,build 则是goal 。

下面是示例:

首先配置属性:

1
2
3
<properties>
<docker.image.prefix>reg.itmuch.com</docker.image.prefix>
</properties>

插件配置:

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
<build>
<plugins>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<executions>
<execution>
<id>build-image</id>
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
<configuration>
<imageName>${docker.image.prefix}/${project.artifactId}</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>

如上,我们只需要添加:

1
2
3
4
5
6
7
8
9
<executions>
<execution>
<id>build-image</id>
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>

即可。本例指的是讲docker的build目标,绑定在package这个phase上。也就是说,用户只需要执行mvn package ,就自动执行了mvn docker:build

坚持技术分享,您的支持将鼓励我继续创作!