Spring Boot 2.0 (iii): Deploy Spring boot with Docker

Source: Internet
Author: User
Tags docker ps

Docker technology has been developed to provide a more convenient environment for micro-service landing, the use of Docker deployment Spring Boot is very simple, this article we come to a simple study.

Start by building a simple Spring Boot project, then add Docker support to the project, and finally deploy the project.

A simple Spring Boot project

In pom.xml , use Spring Boot 2.0 related dependencies

<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.0.RELEASE</version></parent>

Add Web and test dependencies

<dependencies>     <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId> </dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies>

Create a Dockercontroller, in which there is a index() method that is returned when accessed:Hello Docker!

@RestControllerpublic class DockerController { @RequestMapping("/") public String index() { return "Hello Docker!"; }}

Start class

   @SpringBootApplication public  Class dockerapplication {public static void main (string[]  Args) {springapplication. Run (dockerapplication. Classargs              /span>              

After the start of the project, after the successful launch, the browser puts the question: http://localhost:8080/ , the page returns: Hello Docker! , the Spring boot project configuration is OK.

Spring Boot Project Add Docker support

pom.xml-properties Add a Docker image name in

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

Add the Docker Build plugin in plugins:

<build><plugins><plugin><groupid>org.springframework.boot</groupId><artifactid>spring-boot-maven-plugin</artifactId></plugin><!--Docker Maven Plugin--<plugin><groupid>com.spotify</groupId><artifactid>docker-maven-plugin</artifactId><version>1.0.0</version><configuration><imagename>${docker.image.prefix}/${ Project.artifactid}</imagename><dockerdirectory>src/main/docker< Span class= "NT" ></dockerdirectory><resources><resource>< Span class= "NT" ><targetpath>/</targetpath><directory>${ Project.build.directory}</directory><include>${ Project.build.finalname}.jar</include></resource> </resources></configuration></plugin>< span class= "C" ><!--Docker maven plugin--></plugins></build>  

Create the src/main/docker Dockerfile file in the directory, and the Dockerfile file is used to illustrate how to build the image.

FROM openjdk:8-jdk-alpineVOLUME /tmpADD spring-boot-docker-1.0.jar app.jarENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

This Dockerfile file is simple, build the JDK infrastructure, add the Spring Boot Jar to the image, and simply explain:

    • From, which indicates that the JDK8 environment is used as the base image, and if the mirror is not local, it will be downloaded from DockerHub
    • VOLUME, VOLUME points to a /tmp directory, and because Spring Boot uses the built-in Tomcat container, Tomcat is used /tmp as the working directory by default. The effect of this command is to /var/lib/docker create a temporary file in the host directory and link it to the directory in the container. /tmp
    • ADD, copy files and rename
    • EntryPoint, in order to shorten the boot time of Tomcat, the added java.security.egd system attribute points to the /dev/urandom entrypoint

This is done by adding Docker dependencies to the Spring Boot project.

Build a packaged environment

We need a Docker environment to package the Spring Boot project, and it's a hassle to build a docker environment in Windows, so here's the Centos 7 example.

Installing the Docker Environment

Installation

install docker

After the installation is complete, use the following command to start the Docker service and set it to boot:

service docker startchkconfig docker on#LCTT 译注:此处采用了旧式的 sysv 语法,如采用CentOS 7中支持的新式 systemd 语法,如下:systemctl  start docker.servicesystemctl  enable docker.service

Using the Docker China accelerator

vi  /etc/docker/daemon.json#添加后:{    "registry-mirrors": ["https://registry.docker-cn.com"], "live-restore": true}

Restart Docker

systemctl restart docker

Input docker version return version information is installed properly.

Installing the JDK
-y install java-1.8.0-openjdk*

Configure environment variables Open vim /etc/profile Add a bit of content

export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.161-0.b14.el7_4.x86_64 export PATH=$PATH:$JAVA_HOME/bin 

After the modification is complete, make it effective

source /etc/profile

Input java -version return version information is installed properly.

Installing MAVEN

Download:http://mirrors.shu.edu.cn/apache/maven/maven-3/3.5.2/binaries/apache-maven-3.5.2-bin.tar.gz

## 解压tar vxf apache-maven-3.5.2-bin.tar.gz## 移动mv apache-maven-3.5.2 /usr/local/maven3

Modify the environment variables to /etc/profile add the following lines in

MAVEN_HOME=/usr/local/maven3export MAVEN_HOMEexport PATH=${PATH}:${MAVEN_HOME}/bin

Remember to perform source /etc/profile the environment variables in effect.

Input mvn -version return version information is installed properly.

The entire build environment is configured to complete.

Deploying the Spring Boot project using Docker

On the project spring-boot-docker copy server, go to the project path to package the test.

#打包mvn package#启动java -jar target/spring-boot-docker-1.0.jar

Seeing the boot log for Spring boot indicates that the environment is not in trouble, we then use DockerFile to build the image.

mvn package docker:build

The first build may be a bit slow, and when you see the following, it shows that the build was successful:

... Step 1:from Openjdk:8-jdk-alpine---> 224765a6bdbestep 2:volume/tmp---> Using Cache---> B4e86cc8654estep 3:add spring-boot-docker-1.0.jar App.jar---> a20fe75963abremoving Intermediate container 593ee5e1ea51step 4:entrypoint java-djava.security.egd=file:/dev/./urandom-jar/app.jar ---> Running in 85d558a10cd4 Span class= "NT" >---> 7102f08b5e95removing Intermediate container 85d558a10cd4successfully built 7102f08b5e95[info] Built springboot/spring-boot-docker[info] --- ---------------------------------------------------------------------[info] BUILD SUCCESS [info] ------------------------------------------------------------------------ [info] total time:54.346 s[info] finished at:2018-03-13t16:20:15+ 08:00[info] Final memory:42m/182m[info] ------------------------ ------------------------------------------------ 

Use docker images the commands to view the built-in mirrors:

docker imagesREPOSITORY                      TAG                 IMAGE ID            CREATED             SIZEspringboot/spring-boot-docker   latest              99ce9468da74        6 seconds ago       117.5 MB

springboot/spring-boot-dockerIs the image we built, the next step is to run the image

-p 8080:8080 -t springboot/spring-boot-docker

After the boot is complete we use docker ps to view the running image:

docker psCONTAINER ID        IMAGE                           COMMAND                  CREATED             STATUS              PORTS                    NAMES049570da86a9        springboot/spring-boot-docker   "java -Djava.security"   30 seconds ago      Up 27 seconds       0.0.0.0:8080->8080/tcp   determined_mahavira

You can see that the container we are building is running, accessing the browser: http://192.168.0.x:8080/ , returning

Hello Docker!

Description successful using Docker to deploy Spring Boot project!

Spring Boot 2.0 (iii): Deploy Spring boot with Docker

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.