Build Springboot project with Maven plugin, generate Docker image push to Dockerhub

Source: Internet
Author: User
Tags docker hub docker run docker registry

One for building and pushing Dockerof the Mirror MavenPlug - ins.

Use the Maven plugin to build the Docker image, Docker mirror push it to the DockerHub top, or the private warehouse, the previous article is handwritten Dockerfile , this article with the open source plug-in docker-maven-plugin operation

The following actions. By default you have read my previous article:

Docker Deployment Springboot Project Integration Redis image do access count demo

Http://www.ymq.io/2018/01/11/Docker-deploy-spring-boot-Integrate-redis

Final effect

Environment preparation
    • System: Ubuntu 17.04 x64
    • Docker 17.12.0-ce

      Ubuntu 17.04 x64 Installing Docker CE

http://www.ymq.io/2018/01/11/Docker-Install-docker-ce/

Plugin Address

Docker-maven-plugin

GitHub Address:https://github.com/spotify/docker-maven-plugin

First, simple use 1. Modify Pom

pom.xmlAdd the following paragraph to the

<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <!--Docker Mave N Plugins, official website: https://github.com/spotify/docker-maven-plugin--<plugin> &LT;GROUPID&GT;COM.SPOTIFY&L T;/groupid> <artifactId>docker-maven-plugin</artifactId> <version>0.4.12</ve Rsion> <configuration> <!--Note that imagename must conform to the regular [a-z0-9-_.] , otherwise the build will not succeed-<!--see: Https://github.com/spotify/docker-maven-plugin Invalid Repository name ...                only [A-z0-9-_.] is allowed--> <imageName>microservice-discovery-eureka</imageName> <baseImage>java</baseImage> <entrypoint>["Java", "-jar", "/${project.build.finaln Ame}.jar "]</entrypoint> <rEsources> <resource> <targetPath>/</targetPath> <directory>${project.build.directory}</directory> <include>${project. build.finalname}.jar</include> </resource> </resources> &lt ;/configuration> </plugin> </plugins></build>
2. Build the image

Using the MAVEN command:mvn clean package docker:build

& cd /opt/other-projects/docker-spring-boot-demo-maven-plugin& mvn clean package docker:build

We will find that the console has something like the following:

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-pluginstep 1/3: from java---> D23bdf5b1b1bstep 2/3: ADD/ Docker-spring-boot-demo-maven-plugin-0.0.1-snapshot.jar//---> B5d8f92756f2step 3/3: Entrypoint ["Java", "-jar", "/docker-spring-boot-demo-maven-plugin-0.0.1-snapshot.jar"]---> Running in 6867f460b40cremoving Intermediate container 6867f460b40c---> 378fd82432e0progressmessage{id=null, Status=null, Stream=null, Error=null, Progress=null, progressdetail=null}successfully built 378fd82432e0successfully 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]------------------------------------------------------------------------[email  protected]:/opt/other-projects/docker-spring-boot-demo-maven-plugin#

Congratulations, the building was successful.

-We docker images will find that the image has been successfully built:

& [email protected]:# docker images docker-spring-boot-demo-maven-pluginREPOSITORY                             TAG                 IMAGE ID            CREATED             SIZEdocker-spring-boot-demo-maven-plugin   latest              378fd82432e0        3 minutes ago       659MB
3. Boot image
[email protected]:# docker run --name MySpringBootMavenPlugin -d -p 8080:80 docker-spring-boot-demo-maven-plugin84ebb2ebb8c002d3935e6e31c6d2aab05c32c075036368228e84f818d20ded4a
4. View containers
& [email protected]:# docker container ps -aCONTAINER ID        IMAGE                                  COMMAND                  CREATED             STATUS              PORTS                    NAMES84ebb2ebb8c0        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. Access to services

Browser input: Http://Docker host ip:8080 be able to see the interface, the end of the article at the beginning of the page.

Second, use Dockerfile1. New Dockerfile

Building a docker image using Dockerfile

The approach described above is the simplest way, and many times we still have to build it Dockerfile ,
First we /docker-spring-boot-demo-maven-plugin/src/main/resources set up the file under the directoryDockerfile

FROM java:8VOLUME /tmpADD docker-spring-boot-demo-maven-plugin-0.0.1-SNAPSHOT.jar app.jarRUN bash -c ‘touch /app.jar‘EXPOSE 9000ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
2. Modifying the POM

pom.xmlthe project is modified as follows: Specify Dockerfile the path

<build> <plugins> <!--the maven plugin for Docker, official website: https://github.com/spotify/docker-maven-plugin <plugin> <groupId>com.spotify</groupId> <artifactid>docker-maven-plugin </artifactId> <version>0.4.12</version> <configuration> <! -Note that imagename must conform to the regular [a-z0-9-_.] , otherwise the build will not succeed-<!--see: Https://github.com/spotify/docker-maven-plugin Invalid Repository name ...                only [A-z0-9-_.] is allowed--> <imageName>docker-spring-boot-demo-maven-plugin</imageName> <!--Specify the path to Dockerfile-<dockerdirectory>${basedir}/src/main/resources</do ckerdirectory> <resources> <resource> &LT;TARGETPA  Th>/</targetpath> <directory>${project.build.directory}</directory>                      <include>${project.build.finalName}.jar</include> </resource> </resources> </configuration> </plugin> </plugins></build&gt ;
3. Build the image

Using the MAVEN command:mvn clean package docker:build

& cd /opt/other-projects/docker-spring-boot-demo-maven-plugin& mvn clean package docker:build
4. Boot image
[email protected]:# docker run --name MySpringBootMavenPlugin -d -p 8080:80 docker-spring-boot-demo-maven-plugin84ebb2ebb8c002d3935e6e31c6d2aab05c32c075036368228e84f818d20ded4a

The other steps are the same as above. You can use it Dockerfile to build the Docker image.

5. Access to services

Browser input: Http://Docker host ip:8080 be able to see the interface, the end of the article at the beginning of the page.

Third, push image

Will Docker Mirror push to the DockerHub top

1. Modify MAVEN Configuration

First modify Maven The global configuration file settings.xml ,

Check settings.xml your location

[email protected]:# find / -name settings.xml/etc/maven/settings.xml

Add the following paragraph

vi /etc/maven/settings.xml
<servers>    <server>        <id>docker-hub</id>        <username>DockerHub 的账号</username>        <password>DockerHub 的密码</password>        <configuration>            <email>[email protected]</email>        </configuration>    </server></servers>
2. Create Repository

Registered Account: https://hub.docker.com/

DockerHubcreated on the Create Repository , for example: docker-spring-boot-demo-maven-plugin , such as

3. Modifying the POM

The project pom.xml is modified as follows: Note that imageName the path is consistent with the path to the repo

Image name

<properties>    <docker.image.prefix>souyunku</docker.image.prefix></properties>

Will Docker Mirror push to the DockerHub top

<!--3: Push docker image onto Dockerhub--><!--Docker's maven plugin, official website: https://github.com/spotify/docker-maven-plugin --><plugin> <groupId>com.spotify</groupId> <artifactid>docker-maven-plugin</ Artifactid> <version>0.4.12</version> <configuration> <!--Note that imagename must conform to the regular [a-z0-9 -_.] , otherwise the build will not succeed-<!--see: Https://github.com/spotify/docker-maven-plugin Invalid Repository name ... only [A-z0-9-_.] is allowed-<!--if you want to push the Docker image to Dockerhub, the path here will be the same as the repo path, and <imagen ame>${docker.image.prefix}/${project.artifactid}</imagename> <!--Specify the path to Dockerfile and <d Ockerdirectory>${basedir}/src/main/resources</dockerdirectory> <resources> &LT;RESOURCE&G                T                <targetPath>/</targetPath> <directory>${project.build.directory}</directory> <include&gT;${project.build.finalname}.jar</include> </resource> </resources> <!--below two The line is used for Docker push to Dockerhub. -<serverId>docker-hub</serverId> <registryurl>https://index.docker.io/v1/</regist Ryurl> </configuration></plugin>
4. Build the image

Using the MAVEN command:mvn clean package docker:build -DpushImage

& cd /opt/other-projects/docker-spring-boot-demo-maven-plugin& mvn clean package docker:build  -DpushImage

Seeing data like this proves there's nothing wrong with building a mirror.

[INFO] Building image Souyunku/docker-spring-boot-demo-maven-pluginstep 1/6: from Java:8---> D23bdf5b1b1bstep 2/6: VOLUME/ TMP---> Using cache---> cb237cc84527step 3/6: ADD Docker-spring-boot-demo-maven-plugin-0.0.1-snapshot.jar App.jar---> 7fb5e3363ed5step 4/6: RUN bash-c ' Touch/app.jar '---> Running in ab5d10dd64adremoving Intermediate C Ontainer AB5D10DD64AD---> 05d96fe59da4step 5/6: EXPOSE 9000---> Running in d63e20122d8eremoving Intermediate cont Ainer d63e20122d8e---> 55ba378141fdstep 6/6: entrypoint ["Java", "-djava.security.egd=file:/dev/./urandom", "-jar" , "/app.jar"]---> Running in 962d476363a3removing Intermediate container 962d476363a3---> 654b596fe91fprogressmessage{id=null, Status=null, Stream=null, Error=null, Progress=null, progressDetail=null} Successfully built 654b596fe91fsuccessfully 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-pluginthe 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 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:2424null: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]------------------------------------------------------------------------[email   protected]:/opt/other-projects/docker-spring-boot-demo-maven-plugin#
5. View the image
[email protected]:# docker images souyunku/docker-spring-boot-demo-maven-plugin REPOSITORY                                      TAG                 IMAGE ID            CREATED             SIZEsouyunku/docker-spring-boot-demo-maven-plugin   latest              654b596fe91f        27 minutes ago      674MB
[email protected]:# docker images souyunku/docker-spring-boot-demo-maven-plugin REPOSITORY                                      TAG                 IMAGE ID            CREATED             SIZEsouyunku/docker-spring-boot-demo-maven-plugin   latest              654b596fe91f        27 minutes ago      674MB

Docker HubView the image and see that it has been uploaded successfully

6. Boot image
[email protected]:# docker run --name MySpringBootMavenPlugin -d -p 8080:80 docker-spring-boot-demo-maven-plugin84ebb2ebb8c002d3935e6e31c6d2aab05c32c075036368228e84f818d20ded4a

The other steps are the same as above. You can use it Dockerfile to build the Docker image.

7. Access to services

Browser input: Http://Docker host ip:8080 be able to see the interface, the end of the article at the beginning of the page.

Iv. Binding Phase Execution

Bind a plug-in to a phase execution

In many scenarios, we have this requirement, such as when executing mvn clean package , to automatically build a Docker image for us, OK? The answer is yes. We just need to bind the plug- goal in to a certain phase .

So-called phase and goal , as can be understood: the maven command format is: mvn phase:goal , for example mvn package docker:build then, package and both are docker phase,build goal .

1. Modifying the POM

Here's an example:

First configure the properties:

 <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-im age</id> <phase>package</phase> <goals> <goal>build< /goal> </goals> </execution> </executions> <configuration> <! -Note that imagename must conform to the regular [a-z0-9-_.] , otherwise the build will not succeed-<!--see: Https://github.com/spotify/docker-maven-plugin Invalid Repository name ... only [A-z0-9-_.] is allowed-<!--if you want to push the Docker image to Dockerhub, the path here will be the same as the repo path, and <imagen ame>${docker.image.prefix}/${project.artifactid}</imagename> <!--Specify the path to Dockerfile and <d Ockerdirectory>${basedir}/src/main/resources</dockerdirectory> <resources> &LT;RESOURCE> <targetPath>/</targetPath> <directory>${project.build.directory}<        /directory> <include>${project.build.finalName}.jar</include> </resource> </resources> <!--The following two lines are used for Docker push to Dockerhub. -<serverId>docker-hub</serverId> <registryurl>https://index.docker.io/v1/</regist Ryurl> </configuration></plugin>

New additions

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

This example refers to the docke goal of R build , which is bound on package this phase .
In other words, the user simply executes mvn package and executes automatically mvn docker:build .

2. Build the image

Using the MAVEN command:mvn package

& cd /opt/other-projects/docker-spring-boot-demo-maven-plugin& mvn package
3. Boot image
[email protected]:# docker run --name MySpringBootMavenPlugin -d -p 8080:80 docker-spring-boot-demo-maven-plugin84ebb2ebb8c002d3935e6e31c6d2aab05c32c075036368228e84f818d20ded4a
4. Access to Services

Browser input: Http://Docker host ip:8080 be able to see the interface, the end of the article at the beginning of the page.

Recommended reading: Docker Hub warehouse use, and build Docker Registry

http://www.ymq.io/2017/12/31/Docker-dockerHub/

GitHub:d Ocker-spring-boot-demo-maven-plugin

Https://github.com/souyunku/other-projects/tree/master/docker-spring-boot-demo-maven-plugin

Contact
    • Peng Lei
    • Source: Http://www.ymq.io/2018/01/15/Docker-maven-plugin
    • Email:[email protected]
    • Copyright belongs to the author, please specify the source of the reprint
    • WeChat: Pay attention to the public number, search cloud library, focus on the development of technology research and knowledge sharing

Build Springboot project with Maven plugin, generate Docker image push to Dockerhub

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.