Spring基础:快速入门spring boot(6):用docker进行Build/Ship/Run

本篇文章将在将介绍如何使用spring boot的docker-maven-plugin插件生成镜像以及推送到docker库中以及运行确认。

Spring基础:快速入门spring boot(6):用docker进行Build/Ship/Run_第1张图片

准备

JDK和Maven以及Docker是需要的,本文中使用CentOS7.2,因为Docker也支持windows,所以OS不限于linux。相关版本如下:

软件 版本 安装
JDK 1.8 http://blog.csdn.net/liumiaocn/article/details/53512956
Maven 3.3.9 http://blog.csdn.net/liumiaocn/article/details/53512956
Docker 1.10 yum install docker; systemctl start docker

创建Maven项目

Maven坐标信息如下:

项目 详细
groupId com.liumiaocn.springboot.demo
artifactId dockerdemo
version=0.0.1 SNAPSHOT

创建命令:设定interactiveMode为false,需要询问的设定Maven将直接使用缺省值。

mvn archetype:generate -DgroupId=com.liumiaocn.springboot.demo -DartifactId=dockerdemo -Dversion=0.0.1-SNAPSHOT -DinteractiveMode=false

创建时需要下载archetype-catalog.xml,此文件较大,目前已经5M以上了,一般因此会较慢,可以在命令行中加入 -X选项察看DEBUG信息已确认原因

创建详细参照

[root@liumiaocn demo]# ll
total 0
[root@liumiaocn demo]# mvn archetype:generate -DgroupId=com.liumiaocn.springboot.demo -DartifactId=dockerdemo -Dversion=0.0.1-SNAPSHOT -DinteractiveMode=false
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:2.4:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:2.4:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO]
[INFO] --- maven-archetype-plugin:2.4:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Batch mode
[INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.archetypes:maven-archetype-quickstart:1.0)
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/archetypes/maven-archetype-quickstart/1.0/maven-archetype-quickstart-1.0.jar
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/archetypes/maven-archetype-quickstart/1.0/maven-archetype-quickstart-1.0.jar (5 KB at 1.0 KB/sec)
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/archetypes/maven-archetype-quickstart/1.0/maven-archetype-quickstart-1.0.pom
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/archetypes/maven-archetype-quickstart/1.0/maven-archetype-quickstart-1.0.pom (703 B at 1.1 KB/sec)
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:1.0
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: basedir, Value: /root/demo
[INFO] Parameter: package, Value: com.liumiaocn.springboot.demo
[INFO] Parameter: groupId, Value: com.liumiaocn.springboot.demo
[INFO] Parameter: artifactId, Value: dockerdemo
[INFO] Parameter: packageName, Value: com.liumiaocn.springboot.demo
[INFO] Parameter: version, Value: 0.0.1-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: /root/demo/dockerdemo
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 08:19 min
[INFO] Finished at: 2017-01-18T20:34:25-05:00
[INFO] Final Memory: 13M/50M
[INFO] ------------------------------------------------------------------------
[root@liumiaocn demo]# ll
total 0
drwxr-xr-x. 3 root root 30 Jan 18 20:34 dockerdemo
[root@liumiaocn demo]#

创建后的结构
Spring基础:快速入门spring boot(6):用docker进行Build/Ship/Run_第2张图片

修改App.java

此时已经自动给生成了App.java文件,我们将其修改成一个最简单的Web应用的HelloWorld。修改后如下:

package com.liumiaocn.springboot.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication
public class App {
    @RequestMapping("/")
    public String Hello(){
        return "Hello, Spring Boot...";
    }
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

修改Pom

修改前Pom

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0modelVersion>
  <groupId>com.liumiaocn.springboot.demogroupId>
  <artifactId>dockerdemoartifactId>
  <packaging>jarpackaging>
  <version>0.0.1-SNAPSHOTversion>
  <name>dockerdemoname>
  <url>http://maven.apache.orgurl>
  <dependencies>
    <dependency>
      <groupId>junitgroupId>
      <artifactId>junitartifactId>
      <version>3.8.1version>
      <scope>testscope>
    dependency>
  dependencies>
project>

加入spring boot所需相关内容,修改后如下

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0modelVersion>
  <groupId>com.liumiaocn.springboot.demogroupId>
  <artifactId>dockerdemoartifactId>
  <packaging>jarpackaging>
  <version>0.0.1-SNAPSHOTversion>
  <name>dockerdemoname>
  <url>http://maven.apache.orgurl>

  <parent>
      <groupId>org.springframework.bootgroupId>
      <artifactId>spring-boot-starter-parentartifactId>
      <version>1.4.2.RELEASEversion>
      <relativePath/> 
  parent>

  <dependencies>
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-webartifactId>
    dependency>

    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-testartifactId>
        <scope>testscope>
    dependency>

    <dependency>
      <groupId>junitgroupId>
      <artifactId>junitartifactId>
      <version>3.8.1version>
      <scope>testscope>
    dependency>
  dependencies>

  <build>
      <plugins>
          <plugin>
              <groupId>org.springframework.bootgroupId>
              <artifactId>spring-boot-maven-pluginartifactId>
          plugin>
      plugins>
  build>
project>

编译打包

使用如下maven命令进行编译打包

项目 详细
命令 mvn clean package

启动确认

使用如下方式启动该服务

项目 详细
命令 cd target; java -jar dockerdemo-0.0.1-SNAPSHOT.jar

画面确认

Spring基础:快速入门spring boot(6):用docker进行Build/Ship/Run_第3张图片

修正pom的build部分

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0modelVersion>
  <groupId>com.liumiaocn.springboot.demogroupId>
  <artifactId>dockerdemoartifactId>
  <packaging>jarpackaging>
  <version>0.0.1-SNAPSHOTversion>
  <name>dockerdemoname>
  <url>http://maven.apache.orgurl>

  <parent>
      <groupId>org.springframework.bootgroupId>
      <artifactId>spring-boot-starter-parentartifactId>
      <version>1.4.2.RELEASEversion>
      <relativePath/> 
  parent>

  <dependencies>
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-webartifactId>
    dependency>

    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-testartifactId>
        <scope>testscope>
    dependency>

    <dependency>
      <groupId>junitgroupId>
      <artifactId>junitartifactId>
      <version>3.8.1version>
      <scope>testscope>
    dependency>
  dependencies>

  <build>
    <plugins>
      <plugin>
          <groupId>org.springframework.bootgroupId>
          <artifactId>spring-boot-maven-pluginartifactId>
      plugin>

      <plugin>
        <groupId>com.spotifygroupId>
        <artifactId>docker-maven-pluginartifactId>
        <configuration>
            <imageName>springboot-docker-demoimageName>
            <forceTags>trueforceTags>
            <imageTags>
              <imageTag>latestimageTag>
            imageTags>
            <baseImage>java:alpinebaseImage>
            <entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]entryPoint>
            <resources>
                <resource>
                    <targetPath>/targetPath>
                    <directory>${project.build.directory}directory>
                    <include>${project.build.finalName}.jarinclude>
                resource>
            resources>
        configuration>
      plugin>
    plugins>
  build>
project>

生成docker镜像

使用mvn clean package docker:build生成镜像,注意package命令要先行。另外如果想直接push到私库或者dockerhub上,事前docker login之后,加上-DpushImage即可。

[root@liumiaocn dockerdemo]# mvn clean package docker:build
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for com.liumiaocn.springboot.demo:dockerdemo:jar:0.0.1-SNAPSHOT
[WARNING] 'build.plugins.plugin.version' for com.spotify:docker-maven-plugin is missing. @ line 45, column 15
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building dockerdemo 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.6.1:clean (default-clean) @ dockerdemo ---
[INFO] Deleting /root/demo/dockerdemo/target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ dockerdemo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /root/demo/dockerdemo/src/main/resources
[INFO] skip non existing resourceDirectory /root/demo/dockerdemo/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ dockerdemo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /root/demo/dockerdemo/target/classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ dockerdemo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /root/demo/dockerdemo/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ dockerdemo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /root/demo/dockerdemo/target/test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ dockerdemo ---
[INFO] Surefire report directory: /root/demo/dockerdemo/target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.liumiaocn.springboot.demo.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.014 sec - in com.liumiaocn.springboot.demo.AppTest

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO]
[INFO] --- maven-jar-plugin:2.6:jar (default-jar) @ dockerdemo ---
[INFO] Building jar: /root/demo/dockerdemo/target/dockerdemo-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:1.4.2.RELEASE:repackage (default) @ dockerdemo ---
[INFO]
[INFO] --- docker-maven-plugin:0.4.13:build (default-cli) @ dockerdemo ---
[INFO] Copying /root/demo/dockerdemo/target/dockerdemo-0.0.1-SNAPSHOT.jar -> /root/demo/dockerdemo/target/docker/dockerdemo-0.0.1-SNAPSHOT.jar
[INFO] Building image springboot-docker-demo
Step 1 : FROM java:alpine
 ---> e40ba8c51bb2
Step 2 : ADD /dockerdemo-0.0.1-SNAPSHOT.jar //
 ---> ed34815e725e
Removing intermediate container 8784da544f9e
Step 3 : ENTRYPOINT java -jar /dockerdemo-0.0.1-SNAPSHOT.jar
 ---> Running in 1381f35250e1
 ---> e9260209838b
Removing intermediate container 1381f35250e1
Successfully built e9260209838b
[INFO] Built springboot-docker-demo
[INFO] Tagging springboot-docker-demo with latest
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 45.429 s
[INFO] Finished at: 2017-01-18T21:05:29-05:00
[INFO] Final Memory: 36M/88M
[INFO] ------------------------------------------------------------------------
[root@liumiaocn dockerdemo]#

确认docker镜像

[root@liumiaocn dockerdemo]# docker images |grep springboot-docker-demo
springboot-docker-demo                                   latest              285c2351de9b        About a minute ago   145 MB
[root@liumiaocn dockerdemo]#

运行docker镜像

将该镜像运行起来,把对外port设定为9090

项目 详细
命令 docker run -p 9090:8080 springboot-docker-demo

结果确认

Spring基础:快速入门spring boot(6):用docker进行Build/Ship/Run_第4张图片

你可能感兴趣的:(spring,深入浅出Docker,spring快速入门)