From e663789af18dd5e1638d52d59c5922ffb467a8ff Mon Sep 17 00:00:00 2001
From: "Yangkai.Shen" <237497819@qq.com>
Date: Thu, 29 Nov 2018 17:12:59 +0800
Subject: [PATCH] =?UTF-8?q?:sparkles:=20spring-boot-demo-docker=20?=
=?UTF-8?q?=E5=AE=8C=E6=88=90?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
pom.xml | 1 +
spring-boot-demo-docker/.gitignore | 25 ++++
spring-boot-demo-docker/Dockerfile | 20 +++
spring-boot-demo-docker/README.md | 116 ++++++++++++++++++
spring-boot-demo-docker/pom.xml | 70 +++++++++++
.../SpringBootDemoDockerApplication.java | 25 ++++
.../docker/controller/HelloController.java | 27 ++++
.../src/main/resources/application.yml | 4 +
.../SpringBootDemoDockerApplicationTests.java | 16 +++
9 files changed, 304 insertions(+)
create mode 100644 spring-boot-demo-docker/.gitignore
create mode 100644 spring-boot-demo-docker/Dockerfile
create mode 100644 spring-boot-demo-docker/README.md
create mode 100644 spring-boot-demo-docker/pom.xml
create mode 100644 spring-boot-demo-docker/src/main/java/com/xkcoding/docker/SpringBootDemoDockerApplication.java
create mode 100644 spring-boot-demo-docker/src/main/java/com/xkcoding/docker/controller/HelloController.java
create mode 100644 spring-boot-demo-docker/src/main/resources/application.yml
create mode 100644 spring-boot-demo-docker/src/test/java/com/xkcoding/docker/SpringBootDemoDockerApplicationTests.java
diff --git a/pom.xml b/pom.xml
index a7f2ee1..9b7e149 100644
--- a/pom.xml
+++ b/pom.xml
@@ -37,6 +37,7 @@
spring-boot-demo-war
spring-boot-demo-elasticsearch
spring-boot-demo-neo4j
+ spring-boot-demo-docker
pom
diff --git a/spring-boot-demo-docker/.gitignore b/spring-boot-demo-docker/.gitignore
new file mode 100644
index 0000000..82eca33
--- /dev/null
+++ b/spring-boot-demo-docker/.gitignore
@@ -0,0 +1,25 @@
+/target/
+!.mvn/wrapper/maven-wrapper.jar
+
+### STS ###
+.apt_generated
+.classpath
+.factorypath
+.project
+.settings
+.springBeans
+.sts4-cache
+
+### IntelliJ IDEA ###
+.idea
+*.iws
+*.iml
+*.ipr
+
+### NetBeans ###
+/nbproject/private/
+/build/
+/nbbuild/
+/dist/
+/nbdist/
+/.nb-gradle/
\ No newline at end of file
diff --git a/spring-boot-demo-docker/Dockerfile b/spring-boot-demo-docker/Dockerfile
new file mode 100644
index 0000000..2807799
--- /dev/null
+++ b/spring-boot-demo-docker/Dockerfile
@@ -0,0 +1,20 @@
+# 基础镜像
+FROM openjdk:8-jdk-alpine
+
+# 作者信息
+MAINTAINER "Yangkai.Shen 237497819@qq.com"
+
+# 添加一个存储空间
+VOLUME /tmp
+
+# 暴露8080端口
+EXPOSE 8080
+
+# 添加变量,如果使用dockerfile-maven-plugin,则会自动替换这里的变量内容
+ARG JAR_FILE=target/spring-boot-demo-docker.jar
+
+# 往容器中添加jar包
+ADD ${JAR_FILE} app.jar
+
+# 启动镜像自动运行程序
+ENTRYPOINT ["java","-Djava.security.egd=file:/dev/urandom","-jar","/app.jar"]
\ No newline at end of file
diff --git a/spring-boot-demo-docker/README.md b/spring-boot-demo-docker/README.md
new file mode 100644
index 0000000..8ae00ce
--- /dev/null
+++ b/spring-boot-demo-docker/README.md
@@ -0,0 +1,116 @@
+# spring-boot-demo-docker
+
+> 本 demo 主要演示了如何容器化一个 Spring Boot 项目。通过 `Dockerfile` 的方式打包成一个 images 。
+
+## Dockerfile
+
+```dockerfile
+# 基础镜像
+FROM openjdk:8-jdk-alpine
+
+# 作者信息
+MAINTAINER "Yangkai.Shen 237497819@qq.com"
+
+# 添加一个存储空间
+VOLUME /tmp
+
+# 暴露8080端口
+EXPOSE 8080
+
+# 添加变量,如果使用dockerfile-maven-plugin,则会自动替换这里的变量内容
+ARG JAR_FILE=target/spring-boot-demo-docker.jar
+
+# 往容器中添加jar包
+ADD ${JAR_FILE} app.jar
+
+# 启动镜像自动运行程序
+ENTRYPOINT ["java","-Djava.security.egd=file:/dev/urandom","-jar","/app.jar"]
+```
+
+## 打包方式
+
+### 手动打包
+
+1. 前往 Dockerfile 目录,打开命令行执行
+
+ ```bash
+ $ docker build -t spring-boot-demo-docker .
+ ```
+
+2. 查看生成镜像
+
+ ```bash
+ $ docker images
+
+ REPOSITORY TAG IMAGE ID CREATED SIZE
+ spring-boot-demo-docker latest bc29a29ffca0 2 hours ago 119MB
+ openjdk 8-jdk-alpine 97bc1352afde 5 weeks ago 103MB
+ ```
+
+3. 运行
+
+ ```bash
+ $ docker run -d -p 9090:8080 spring-boot-demo-docker
+ ```
+
+### 使用 maven 插件打包
+
+1. pom.xml 中添加插件
+
+2. ```xml
+
+ 1.4.9
+
+
+
+
+ com.spotify
+ dockerfile-maven-plugin
+ ${dockerfile-version}
+
+ ${project.build.finalName}
+ ${project.version}
+
+ target/${project.build.finalName}.jar
+
+
+
+
+ default
+ package
+
+ build
+
+
+
+
+
+ ```
+
+2. 执行mvn打包命令,因为插件中 `execution` 节点配置了 package,所以会在打包的时候自动执行 build 命令。
+
+ ```bash
+ $ mvn clean package -Dmaven.test.skip=true
+ ```
+
+3. 查看镜像
+
+ ```bash
+ $ docker images
+
+ REPOSITORY TAG IMAGE ID CREATED SIZE
+ spring-boot-demo-docker 1.0.0-SNAPSHOT bc29a29ffca0 2 hours ago 119MB
+ openjdk 8-jdk-alpine 97bc1352afde 5 weeks ago 103MB
+ ```
+
+4. 运行
+
+ ```bash
+ $ docker run -d -p 9090:8080 spring-boot-demo-docker:1.0.0-SNAPSHOT
+ ```
+
+## 参考
+
+- docker 官方文档:https://docs.docker.com/
+- Dockerfile 命令,参考文档:https://docs.docker.com/engine/reference/builder/
+- maven插件使用,参考地址:https://github.com/spotify/dockerfile-maven
\ No newline at end of file
diff --git a/spring-boot-demo-docker/pom.xml b/spring-boot-demo-docker/pom.xml
new file mode 100644
index 0000000..5f8bd90
--- /dev/null
+++ b/spring-boot-demo-docker/pom.xml
@@ -0,0 +1,70 @@
+
+
+ 4.0.0
+
+ spring-boot-demo-docker
+ 1.0.0-SNAPSHOT
+ jar
+
+ spring-boot-demo-docker
+ Demo project for Spring Boot
+
+
+ com.xkcoding
+ spring-boot-demo
+ 1.0.0-SNAPSHOT
+
+
+
+ UTF-8
+ UTF-8
+ 1.8
+ 1.4.9
+
+
+
+
+ org.springframework.boot
+ spring-boot-starter-web
+
+
+
+ org.springframework.boot
+ spring-boot-starter-test
+ test
+
+
+
+
+ spring-boot-demo-docker
+
+
+ org.springframework.boot
+ spring-boot-maven-plugin
+
+
+ com.spotify
+ dockerfile-maven-plugin
+ ${dockerfile-version}
+
+ ${project.build.finalName}
+ ${project.version}
+
+ target/${project.build.finalName}.jar
+
+
+
+
+ default
+ package
+
+ build
+
+
+
+
+
+
+
+
diff --git a/spring-boot-demo-docker/src/main/java/com/xkcoding/docker/SpringBootDemoDockerApplication.java b/spring-boot-demo-docker/src/main/java/com/xkcoding/docker/SpringBootDemoDockerApplication.java
new file mode 100644
index 0000000..9f707bd
--- /dev/null
+++ b/spring-boot-demo-docker/src/main/java/com/xkcoding/docker/SpringBootDemoDockerApplication.java
@@ -0,0 +1,25 @@
+package com.xkcoding.docker;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+/**
+ *
+ * 启动器
+ *
+ *
+ * @package: com.xkcoding.docker
+ * @description: 启动器
+ * @author: yangkai.shen
+ * @date: Created in 2018-11-29 14:59
+ * @copyright: Copyright (c) 2018
+ * @version: V1.0
+ * @modified: yangkai.shen
+ */
+@SpringBootApplication
+public class SpringBootDemoDockerApplication {
+
+ public static void main(String[] args) {
+ SpringApplication.run(SpringBootDemoDockerApplication.class, args);
+ }
+}
diff --git a/spring-boot-demo-docker/src/main/java/com/xkcoding/docker/controller/HelloController.java b/spring-boot-demo-docker/src/main/java/com/xkcoding/docker/controller/HelloController.java
new file mode 100644
index 0000000..04884fc
--- /dev/null
+++ b/spring-boot-demo-docker/src/main/java/com/xkcoding/docker/controller/HelloController.java
@@ -0,0 +1,27 @@
+package com.xkcoding.docker.controller;
+
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+/**
+ *
+ * Hello Controller
+ *
+ *
+ * @package: com.xkcoding.docker.controller
+ * @description: Hello Controller
+ * @author: yangkai.shen
+ * @date: Created in 2018-11-29 14:58
+ * @copyright: Copyright (c) 2018
+ * @version: V1.0
+ * @modified: yangkai.shen
+ */
+@RestController
+@RequestMapping
+public class HelloController {
+ @GetMapping
+ public String hello() {
+ return "Hello,From Docker!";
+ }
+}
diff --git a/spring-boot-demo-docker/src/main/resources/application.yml b/spring-boot-demo-docker/src/main/resources/application.yml
new file mode 100644
index 0000000..a02fbde
--- /dev/null
+++ b/spring-boot-demo-docker/src/main/resources/application.yml
@@ -0,0 +1,4 @@
+server:
+ port: 8080
+ servlet:
+ context-path: /demo
\ No newline at end of file
diff --git a/spring-boot-demo-docker/src/test/java/com/xkcoding/docker/SpringBootDemoDockerApplicationTests.java b/spring-boot-demo-docker/src/test/java/com/xkcoding/docker/SpringBootDemoDockerApplicationTests.java
new file mode 100644
index 0000000..3e009f4
--- /dev/null
+++ b/spring-boot-demo-docker/src/test/java/com/xkcoding/docker/SpringBootDemoDockerApplicationTests.java
@@ -0,0 +1,16 @@
+package com.xkcoding.docker;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.junit4.SpringRunner;
+
+@RunWith(SpringRunner.class)
+@SpringBootTest
+public class SpringBootDemoDockerApplicationTests {
+
+ @Test
+ public void contextLoads() {
+ }
+
+}