The first spring Boot app to build, run, publish through Docker

Source: Internet
Author: User
Tags bind resource xmlns tomcat docker run
1. About Docker

Docker is an open-source application container engine that allows developers to package their applications and dependencies into a portable container, and then publish them to any popular Linux machine or virtualize them. Containers are completely sandbox-aware and do not have any interfaces with each other. Docker image is the scenario used to run the containerized process, and in this article we will build a simple Spring Boot application. 2. Environment Construction JDK 1.8+ Maven 3.0+ Docker latest version. 3. Building a project with Maven 3.1 Creating a directory structure

Mkdir-p Src/main/java/com/lidong/demo

In Linux or Mac systems.
3.2 Creating a Pom.xml file

<?xml version= "1.0" encoding= "UTF-8"?> <project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "htt P://www.w3.org/2001/xmlschema-instance "xsi:schemalocation=" http://maven.apache.org/POM/4.0.0 Http://maven.apach E.org/xsd/maven-4.0.0.xsd "> <modelVersion>4.0.0</modelVersion> <groupId>com.lidong.demo< /groupid> <artifactId>lidong-spring-boot-demo</artifactId> <version>1.0-snapshot</ version> <parent> <groupId>org.springframework.boot</groupId> <artifactid>s Pring-boot-starter-parent</artifactid> <version>1.5.1.RELEASE</version> <relativepath /> </parent> <properties> &LT;PROJECT.BUILD.SOURCEENCODING&GT;UTF-8&LT;/PROJECT.BUILD.SOURC Eencoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <j Ava.version>1.8</java.version> <docker.image.prefix>springio</docker.image.prefix> </properties> <dependencies > <dependency> <groupId>org.springframework.boot</groupId> <artifa Ctid>spring-boot-starter-web</artifactid> </dependency> <dependency> < Groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid&
            Gt <scope>test</scope> </dependency> <dependency> <groupid>org.spri Ngframework.boot</groupid> <artifactId>spring-boot-starter-thymeleaf</artifactId> &L
                t;/dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-maven-plugin </artifactid> </plugin> <plugin> <groupid>com.spotify</groupi D> <artifactId>docker-maven-plugin</artifactId> <version>0.4.13</v Ersion> <configuration> <imagename>${docker.image.prefix}/${project.art
                    Ifactid}</imagename> <dockerDirectory>src/main/docker</dockerDirectory> <resources> <resource> <targetpath>/</tar
                            Getpath> <directory>${project.build.directory}</directory>
                    <include>${project.build.finalName}.jar</include> </resource>
    </resources> </configuration> </plugin> </plugins> </build> </Project> 

Attention:

Spring Boot Maven Plugin offers a number of handy features:
1) It collects all the jar files on the classpath and builds it into a single, runnable jar, which makes it easier to execute and transport the service.
2) It searches the public static void Main () method to mark the class as operational.
3) It provides a built-in dependency parser that sets the version number to match the Spring Boot dependency. You can overwrite any version you want, but it will select the Boot version set by default.

Spotify's Docker-maven-plugin plugin is the Docker Image used to build maven
1) imagename Specifies the name of the mirror, this example is Springio/lidong-spring-boot-demo
2) dockerdirectory Specify the location of the Dockerfile
3) resources refers to the files that need to be put together with Dockerfile and used when building the image, and the general application jar package needs to be included. 4. Writing the first spring Boot application

Write a simple Spring Boot application:

Src/main/java/com/lidong/demo/samplecontroller.java:

package Com.lidong.demo;
Import org.springframework.boot.SpringApplication;
Import org.springframework.boot.autoconfigure.SpringBootApplication;
Import Org.springframework.stereotype.Controller;
Import org.springframework.web.bind.annotation.RequestMapping;

Import Org.springframework.web.bind.annotation.ResponseBody; /** * @ Project Name: Lidong-dubbo * @ class Name: Samplecontroller * Description of class: * @ Author: Lidong * @ created: 2017/2/19 9:34 * @ Company: Chni * @QQ: 156 1281670 * @ Email: lidong1665@163.com */@Controller @SpringBootApplication public class Samplecontroller {@ResponseBod
    Y @RequestMapping (value = "/") String Home () {return "Hello Docker World"; } public static void Main (string[] args) {Springapplication.run (Samplecontroller.class, "--server.port=8081")
    ; }
}
Class uses @SpringBootApplication @RestController identity, and Spring MVC can be used to process WEB requests. @RequestMapping will/Map to home () and respond with "Hello Docker World" text. The main () method uses the Spring boot Springapplication.run () method to launch the app. 5. Running the program 5.1 using the MAVEN command
MVN Package

Run:

Java-jar Target/lidong-spring-boot-demo-1.0-snapshot.jar

Access project

If the program runs correctly, the browser accesses http://localhost:8081/, and you can see the words "Hello Docker World" on the page. 5.2 Using the idea plugin

6. Containerized the project

Docker uses the Dockerfile file format to specify the image layer,

Create file Src/main/docker/dockerfile:

From Frolvlad/alpine-oraclejdk8:slim
volume/tmp
ADD lidong-spring-boot-demo-1.0-snapshot.jar App.jar
RUN sh-c ' Touch/app.jar '
ENV java_opts= ""
entrypoint ["sh", "-C", "JAVA $JAVA _opts-djava.security.egd=file:/d Ev/./urandom-jar/app.jar "]

Explain the following configuration file:

VOLUME specifies that the temp file directory is/tmp. The effect is to create a temporary file in the host/var/lib/docker directory and link to/tmp of the container. The steps are optional, which is necessary if the application of the file system is involved. The/tmp directory is used to persist to the Docker Data folder because the inline Tomcat container used by Spring Boot uses/tmp as the working directory by default
The project's jar file is added as "App.jar" to the container's
EntryPoint executes the project App.jar. To shorten the Tomcat boot time, add a system attribute pointing to "/dev/urandom" as Entropy Source
Building Docker Image

Execute build as Docker Image:

MVN Package Docker:build

Run

Run Docker Image

Docker Run-p 8081:8081-t Springio/lidong-spring-boot-demo

See the icon for this spring. I thought we were done with the spring boot program on Docker.

Next go to visit the http://localhost:8081/in the browser, you can see the page "Hello Docker World" words.

Resources:
http://spring.io/guides/gs/spring-boot-docker/

Here is an introduction to the simple Spring boot. Detailed knowledge of Spring-boot building MicroServices is described later in this article.

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.