使用Maven外掛構建SpringBoot專案,生成Docker映象push到DockerHub上

搜雲庫技術團隊發表於2019-03-01

一個用於構建和推送Docker映象的Maven外掛。

使用Maven外掛構建Docker映象,將Docker映象pushDockerHub上,或者私有倉庫,上一篇文章是手寫Dockerfile,這篇文章藉助開源外掛docker-maven-plugin 進行操作

以下操作。預設你已經閱讀過我上一篇文章:

Docker 部署 SpringBoot 專案整合 Redis 映象做訪問計數Demo

www.ymq.io/2018/01/11/…

最終效果

docker-maven-plugin

環境準備

  • 系統:Ubuntu 17.04 x64
  • Docker 17.12.0-ce

Ubuntu 17.04 x64 安裝 Docker CE

www.ymq.io/2018/01/11/…

外掛地址

docker-maven-plugin

GitHub 地址:github.com/spotify/doc…

一、簡單使用

1.修改POM

pom.xml中新增下面這段,

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>

        <!-- 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</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>
複製程式碼

2.構建映象

使用 maven 命令: mvn clean package docker:build

& cd /opt/other-projects/docker-spring-boot-demo-maven-plugin
& mvn clean package docker:build
複製程式碼

我們會發現控制檯有類似如下內容:

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] 
[INFO] --- maven-jar-plugin:2.6:jar (default-jar) @ docker-spring-boot-demo-maven-plugin ---
[INFO] Building jar: /opt/other-projects/docker-spring-boot-demo-maven-plugin/target/docker-spring-boot-demo-maven-plugin-0.0.1-SNAPSHOT.jar
[INFO] 
[INFO] --- spring-boot-maven-plugin:1.5.9.RELEASE:repackage (default) @ docker-spring-boot-demo-maven-plugin ---
[INFO] 
[INFO] --- docker-maven-plugin:0.4.12:build (default-cli) @ docker-spring-boot-demo-maven-plugin ---
[INFO] Copying /opt/other-projects/docker-spring-boot-demo-maven-plugin/target/docker-spring-boot-demo-maven-plugin-0.0.1-SNAPSHOT.jar -> /opt/other-projects/docker-spring-boot-demo-maven-plugin/target/docker/docker-spring-boot-demo-maven-plugin-0.0.1-SNAPSHOT.jar
[INFO] Building image docker-spring-boot-demo-maven-plugin
Step 1/3 : FROM java

 ---> d23bdf5b1b1b
Step 2/3 : ADD /docker-spring-boot-demo-maven-plugin-0.0.1-SNAPSHOT.jar //

 ---> b5d8f92756f2
Step 3/3 : ENTRYPOINT ["java", "-jar", "/docker-spring-boot-demo-maven-plugin-0.0.1-SNAPSHOT.jar"]

 ---> Running in 6867f460b40c
Removing intermediate container 6867f460b40c
 ---> 378fd82432e0
ProgressMessage{id=null, status=null, stream=null, error=null, progress=null, progressDetail=null}
Successfully built 378fd82432e0
Successfully tagged docker-spring-boot-demo-maven-plugin:latest
[INFO] Built docker-spring-boot-demo-maven-plugin
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 20.568 s
[INFO] Finished at: 2018-01-15T09:21:39+00:00
[INFO] Final Memory: 37M/89M
[INFO] ------------------------------------------------------------------------
root@souyunku:/opt/other-projects/docker-spring-boot-demo-maven-plugin#
複製程式碼

恭喜,構建成功了。

-我們執行docker images 會發現該映象已經被構建成功:

& root@souyunku:# docker images docker-spring-boot-demo-maven-plugin
REPOSITORY                             TAG                 IMAGE ID            CREATED             SIZE
docker-spring-boot-demo-maven-plugin   latest              378fd82432e0        3 minutes ago       659MB
複製程式碼

3.啟動映象

root@souyunku:# docker run --name MySpringBootMavenPlugin -d -p 8080:80 docker-spring-boot-demo-maven-plugin
84ebb2ebb8c002d3935e6e31c6d2aab05c32c075036368228e84f818d20ded4a
複製程式碼

4.檢視容器

& root@souyunku:# docker container ps -a
CONTAINER ID        IMAGE                                  COMMAND                  CREATED             STATUS              PORTS                    NAMES
84ebb2ebb8c0        docker-spring-boot-demo-maven-plugin   "java -jar /docker-s…"   About an hour ago   Up About an hour    0.0.0.0:8080->80/tcp     MySpringBootMavenPlugin
複製程式碼

5.訪問服務

瀏覽器輸入:http://Docker宿主機IP:8080能夠正常看到介面,文章開頭的最終效果頁面。

二、使用Dockerfile

1.新建Dockerfile

使用Dockerfile進行構建Docker映象

上文講述的方式是最簡單的方式,很多時候,我們還是要藉助Dockerfile進行構建的,
首先我們在/docker-spring-boot-demo-maven-plugin/src/main/resources目錄下,建立檔案Dockerfile

FROM java:8
VOLUME /tmp
ADD docker-spring-boot-demo-maven-plugin-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"]
複製程式碼

2.修改POM

專案pom.xml修改為如下: 指定Dockerfile所在的路徑

<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>docker-spring-boot-demo-maven-plugin</imageName>
				<!-- 指定Dockerfile所在的路徑 -->
				<dockerDirectory>${basedir}/src/main/resources</dockerDirectory>
				<resources>
					<resource>
						<targetPath>/</targetPath>
						<directory>${project.build.directory}</directory>
						<include>${project.build.finalName}.jar</include>
					</resource>
				</resources>
			</configuration>
		</plugin>
	</plugins>
</build>
複製程式碼

3.構建映象

使用 maven 命令: mvn clean package docker:build

& cd /opt/other-projects/docker-spring-boot-demo-maven-plugin
& mvn clean package docker:build
複製程式碼

4.啟動映象

root@souyunku:# docker run --name MySpringBootMavenPlugin -d -p 8080:80 docker-spring-boot-demo-maven-plugin
84ebb2ebb8c002d3935e6e31c6d2aab05c32c075036368228e84f818d20ded4a
複製程式碼

其他步驟和上面一樣。這樣即可使用Dockerfile進行構建Docker映象啦。

5.訪問服務

瀏覽器輸入:http://Docker宿主機IP:8080能夠正常看到介面,文章開頭的最終效果頁面。

三、push 映象

Docker映象pushDockerHub

1.修改Maven配置

首先修改Maven的全域性配置檔案settings.xml

檢視settings.xml 所在位置

root@souyunku:# find / -name settings.xml
/etc/maven/settings.xml
複製程式碼

新增以下段落

vi /etc/maven/settings.xml
複製程式碼
<servers>
    <server>
        <id>docker-hub</id>
        <username>DockerHub 的賬號</username>
        <password>DockerHub 的密碼</password>
        <configuration>
            <email>admin@souyunku.com</email>
        </configuration>
    </server>
</servers>
複製程式碼

2.建立Repository

註冊個賬號:hub.docker.com/

DockerHub上建立Create Repository ,例如:docker-spring-boot-demo-maven-plugin,如下圖

docker-spring-boot-demo-maven-plugin

3.修改POM

專案pom.xml修改為如下:注意imageName的路徑要和repo的路徑一致

映象名稱

<properties>
	<docker.image.prefix>souyunku</docker.image.prefix>
</properties>
複製程式碼

Docker映象pushDockerHub

<!--3:將Docker映象push到DockerHub上-->

<!-- 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>${docker.image.prefix}/${project.artifactId}</imageName>
		<!-- 指定Dockerfile所在的路徑 -->
		<dockerDirectory>${basedir}/src/main/resources</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>
複製程式碼

4.構建映象

使用 maven 命令: mvn clean package docker:build -DpushImage

& cd /opt/other-projects/docker-spring-boot-demo-maven-plugin
& mvn clean package docker:build  -DpushImage
複製程式碼

看到類似這樣的資料,就證明構建映象沒毛病

[INFO] Building image souyunku/docker-spring-boot-demo-maven-plugin
Step 1/6 : FROM java:8

 ---> d23bdf5b1b1b
Step 2/6 : VOLUME /tmp

 ---> Using cache
 ---> cb237cc84527
Step 3/6 : ADD docker-spring-boot-demo-maven-plugin-0.0.1-SNAPSHOT.jar app.jar

 ---> 7fb5e3363ed5
Step 4/6 : RUN bash -c `touch /app.jar`

 ---> Running in ab5d10dd64ad
Removing intermediate container ab5d10dd64ad
 ---> 05d96fe59da4
Step 5/6 : EXPOSE 9000

 ---> Running in d63e20122d8e
Removing intermediate container d63e20122d8e
 ---> 55ba378141fd
Step 6/6 : ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

 ---> Running in 962d476363a3
Removing intermediate container 962d476363a3
 ---> 654b596fe91f
ProgressMessage{id=null, status=null, stream=null, error=null, progress=null, progressDetail=null}
Successfully built 654b596fe91f
Successfully tagged souyunku/docker-spring-boot-demo-maven-plugin:latest
[INFO] Built souyunku/docker-spring-boot-demo-maven-plugin
[INFO] Pushing souyunku/docker-spring-boot-demo-maven-plugin
The push refers to repository [docker.io/souyunku/docker-spring-boot-demo-maven-plugin]
464800d90790: Pushed 
d52b146f9147: Pushed 
35c20f26d188: Mounted from souyunku/docker-spring-boot-demo 
c3fe59dd9556: Mounted from souyunku/docker-spring-boot-demo 
6ed1a81ba5b6: Mounted from souyunku/docker-spring-boot-demo 
a3483ce177ce: Mounted from souyunku/docker-spring-boot-demo 
ce6c8756685b: Mounted from souyunku/docker-spring-boot-demo 
30339f20ced0: Mounted from souyunku/docker-spring-boot-demo 
0eb22bfb707d: Mounted from souyunku/docker-spring-boot-demo 
a2ae92ffcd29: Mounted from souyunku/docker-spring-boot-demo 
latest: digest: sha256:8d78ced0034f38be8086c8f812817ec4c12b178470b4cea668046906c825c9ee size: 2424
null: null 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 41.764 s
[INFO] Finished at: 2018-01-16T09:56:23+00:00
[INFO] Final Memory: 36M/88M
[INFO] ------------------------------------------------------------------------
root@souyunku:/opt/other-projects/docker-spring-boot-demo-maven-plugin# 
複製程式碼

5.檢視映象

root@souyunku:# docker images souyunku/docker-spring-boot-demo-maven-plugin 
REPOSITORY                                      TAG                 IMAGE ID            CREATED             SIZE
souyunku/docker-spring-boot-demo-maven-plugin   latest              654b596fe91f        27 minutes ago      674MB
複製程式碼
root@souyunku:# docker images souyunku/docker-spring-boot-demo-maven-plugin 
REPOSITORY                                      TAG                 IMAGE ID            CREATED             SIZE
souyunku/docker-spring-boot-demo-maven-plugin   latest              654b596fe91f        27 minutes ago      674MB
複製程式碼

Docker Hub 檢視映象,看到已經上傳成功

docker hub 檢視映象

6.啟動映象

root@souyunku:# docker run --name MySpringBootMavenPlugin -d -p 8080:80 docker-spring-boot-demo-maven-plugin
84ebb2ebb8c002d3935e6e31c6d2aab05c32c075036368228e84f818d20ded4a
複製程式碼

其他步驟和上面一樣。這樣即可使用Dockerfile進行構建Docker映象啦。

7.訪問服務

瀏覽器輸入:http://Docker宿主機IP:8080能夠正常看到介面,文章開頭的最終效果頁面。

四、繫結phase執行

將外掛繫結在某個phase執行

在很多場景下,我們有這樣的需求,例如執行mvn clean package 時,自動地為我們構建docker映象,可以嗎?答案是肯定的。我們只需要將外掛的goal 繫結在某個phase即可。

所謂的phasegoal,可以這樣理解:maven命令格式是:mvn phase:goal ,例如mvn package docker:build 那麼,packagedocker 都是phase,build 則是goal

1.修改POM

下面是示例:

首先配置屬性:

<properties>
	<docker.image.prefix>souyunku</docker.image.prefix>
</properties>
複製程式碼
<plugin>
	<groupId>com.spotify</groupId>
	<artifactId>docker-maven-plugin</artifactId>
	<version>0.4.12</version>
	
	<executions>
		<execution>
			<id>build-image</id>
			<phase>package</phase>
			<goals>
				<goal>build</goal>
			</goals>
		</execution>
	</executions>
	
	<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>${docker.image.prefix}/${project.artifactId}</imageName>
		<!-- 指定Dockerfile所在的路徑 -->
		<dockerDirectory>${basedir}/src/main/resources</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>
複製程式碼

新加內容

<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

2.構建映象

使用 maven 命令: mvn package

& cd /opt/other-projects/docker-spring-boot-demo-maven-plugin
& mvn package
複製程式碼

3.啟動映象

root@souyunku:# docker run --name MySpringBootMavenPlugin -d -p 8080:80 docker-spring-boot-demo-maven-plugin
84ebb2ebb8c002d3935e6e31c6d2aab05c32c075036368228e84f818d20ded4a
複製程式碼

4.訪問服務

瀏覽器輸入:http://Docker宿主機IP:8080能夠正常看到介面,文章開頭的最終效果頁面。

docker-maven-plugin

推薦閱讀:Docker Hub 倉庫使用,及搭建 Docker Registry

www.ymq.io/2017/12/31/…

GitHub :docker-spring-boot-demo-maven-plugin

github.com/souyunku/ot…

Contact

關注公眾號-搜雲庫
搜雲庫

相關文章