Spring Boot Tutorial (11) Springboot program builds a docker image

Source: Internet
Author: User
Tags docker run spring boot tutorial

Preparatory work

Environment:

    • Linux environment or Mac, do not use Windows
    • JDK 8
    • Maven 3.0
    • Docker

See Docker tutorial for no knowledge of Docker.

Create a Springboot Project

Introduce a web-based start-up dependency and create a controler:

@SpringBootApplication @restcontrollerpublic class Springbootwithdockerapplication {    @RequestMapping ("/")    Public String Home () {        return ' Hello Docker world ';    }    public static void Main (string[] args) {        springapplication.run (springbootwithdockerapplication.class, args);}    }

  

Containerized the Springboot Project

Docker has a simple dockerfile file as a layer for the specified image. Let's first create a dockerfile file:

Src/main/docker/dockerfile:from Frolvlad/alpine-oraclejdk8:slimvolume/tmpadd Springboot-with-docker-0.0.1-snapshot.jar app.jarrun sh-c ' Touch/app.jar ' ENV java_opts= "" ENTRYPOINT ["sh", "-C", " Java $JAVA _opts-djava.security.egd=file:/dev/./urandom-jar/app.jar "]

  

We build Docker images with Maven.

In Maven's POM catalog, plus the plug-in built with Docker images

<properties> <docker.image.prefix>springio</docker.image.prefix></properties><build > <plugins> <plugin> <groupId>com.spotify</groupId> &LT;ARTIFAC Tid>docker-maven-plugin</artifactid> <version>0.4.11</version> <configuratio N> <imageName>${docker.image.prefix}/${project.artifactId}</imageName> <do Ckerdirectory>src/main/docker</dockerdirectory> <resources> <resource&                        Gt <targetPath>/</targetPath> <directory>${project.build.directory}</directory&gt                        ; <include>${project.build.finalName}.jar</include> </resource> </res ources> </configuration> </plugin> </plugins></build>

  

Note: ${docker.image.prefix} is your user name in the Docker's official repository, if you don't need to upload an image, just fill it out.

Through the MAVEN command:

First step: mvn clean

Step Two: MVN package Docker:bulid, as follows:

Step 2/6: volume/tmp-> Running in a98be3878053-> 8286e98b54c5 removing intermediate container a98be3878053 step 3/6: ADD springboot-with-docker-0.0.1-snapshot.jar app.jar-> c6ce13e50bbd removing intermediate container a303a3058869 Step 4/6: RUN sh-c ' Touch/app.jar ', Running in cf231afe700e-> 9a0ec8936c00 removing intermediate Container cf231afe700e Step 5/6: ENV java_opts "", Running in e192597fc881-> 2cb0d73bbdb0 removing intermediate Container e192597fc881 Step 6/6: entrypoint sh-c java $JAVA _opts-djava.security.egd=file:/dev/./urandom-jar/app.jar- > Running in ab85f53fcdd8-> 60fdb5c61692 removing intermediate container ab85f53fcdd8 successfully built 60fdb5c616 [INFO] Built Forezp/springboot-with-docker [INFO] ———————————————————————— [INFO] BUILD SUCCESS [INFO] —————————————— —————————— [INFO] Total time:01:45 min [info] finished at:2017-04-19t05:37:44-07:00 [INFO] Final memory:19m/48m [INFO] ————————————————————————

  

The image was built successfully. To view the image:

docker images

Show:

Forezp/springboot-with-docker latest 60fdb5c61692 about a minute ago 195 MB

  

Boot Image:

$ docker run-p 8080:8080-t Forezp/springboot-with-docker

  

Open browser to access localhost:8080; Browser display: Hello Docker world.
Illustrates that Docker's Springboot project has been deployed.

To stop mirroring:

Docker Stop 60fdb5c61692

  

To delete a mirror:

Docker RM 60fdb5c61692

  

Source Source

Spring Boot Tutorial (11) Springboot program builds a docker image

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.