一個用於構建和推送Docker
鏡像的Maven
插件。java
使用Maven
插件構建Docker
鏡像,將Docker
鏡像push
到DockerHub
上,或者私有倉庫,上一篇文章是手寫Dockerfile
,這篇文章藉助開源插件docker-maven-plugin
進行操做git
如下操做。默認你已經閱讀過我上一篇文章:github
Docker 部署 SpringBoot 項目整合 Redis 鏡像作訪問計數Demoredis
http://www.ymq.io/2018/01/11/Docker-deploy-spring-boot-Integrate-redisspring
Ubuntu 17.04 x64 安裝 Docker CEdocker
http://www.ymq.io/2018/01/11/Docker-Install-docker-ce/瀏覽器
docker-maven-pluginbash
GitHub 地址:https://github.com/spotify/docker-maven-pluginapp
在pom.xml
中添加下面這段,dom
<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>
使用 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
root@souyunku:# docker run --name MySpringBootMavenPlugin -d -p 8080:80 docker-spring-boot-demo-maven-plugin 84ebb2ebb8c002d3935e6e31c6d2aab05c32c075036368228e84f818d20ded4a
& 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
瀏覽器輸入:http://Docker宿主機IP:8080可以正常看到界面,文章開頭的最終效果頁面。
使用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"]
項目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>
使用 maven 命令: mvn clean package docker:build
& cd /opt/other-projects/docker-spring-boot-demo-maven-plugin & mvn clean package docker:build
root@souyunku:# docker run --name MySpringBootMavenPlugin -d -p 8080:80 docker-spring-boot-demo-maven-plugin 84ebb2ebb8c002d3935e6e31c6d2aab05c32c075036368228e84f818d20ded4a
其餘步驟和上面同樣。這樣便可使用Dockerfile
進行構建Docker
鏡像啦。
瀏覽器輸入:http://Docker宿主機IP:8080可以正常看到界面,文章開頭的最終效果頁面。
將Docker
鏡像push
到DockerHub
上
首先修改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>
註冊個帳號:https://hub.docker.com/
在DockerHub
上建立Create Repository
,例如:docker-spring-boot-demo-maven-plugin
,以下圖
項目pom.xml
修改成以下:注意imageName
的路徑要和repo的路徑一致
鏡像名稱
<properties> <docker.image.prefix>souyunku</docker.image.prefix> </properties>
將Docker
鏡像push
到DockerHub
上
<!--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>
使用 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#
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
查看鏡像,看到已經上傳成功
root@souyunku:# docker run --name MySpringBootMavenPlugin -d -p 8080:80 docker-spring-boot-demo-maven-plugin 84ebb2ebb8c002d3935e6e31c6d2aab05c32c075036368228e84f818d20ded4a
其餘步驟和上面同樣。這樣便可使用Dockerfile
進行構建Docker
鏡像啦。
瀏覽器輸入:http://Docker宿主機IP:8080可以正常看到界面,文章開頭的最終效果頁面。
將插件綁定在某個phase執行
在不少場景下,咱們有這樣的需求,例如執行mvn clean package
時,自動地爲咱們構建docker鏡像,能夠嗎?答案是確定的。咱們只須要將插件的goal
綁定在某個phase
便可。
所謂的phase
和goal
,能夠這樣理解:maven
命令格式是:mvn phase:goal
,例如mvn package docker:build
那麼,package
和 docker
都是phase,build
則是goal
。
下面是示例:
首先配置屬性:
<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>
本例指的是講docke
r的build
目標,綁定在package
這個phase
上。
也就是說,用戶只須要執行mvn package
,就自動執行了mvn docker:build
。
使用 maven 命令: mvn package
& cd /opt/other-projects/docker-spring-boot-demo-maven-plugin & mvn package
root@souyunku:# docker run --name MySpringBootMavenPlugin -d -p 8080:80 docker-spring-boot-demo-maven-plugin 84ebb2ebb8c002d3935e6e31c6d2aab05c32c075036368228e84f818d20ded4a
瀏覽器輸入:http://Docker宿主機IP:8080可以正常看到界面,文章開頭的最終效果頁面。
推薦閱讀:Docker Hub 倉庫使用,及搭建 Docker Registry
http://www.ymq.io/2017/12/31/Docker-dockerHub/
GitHub :docker-spring-boot-demo-maven-plugin
https://github.com/souyunku/other-projects/tree/master/docker-spring-boot-demo-maven-plugin