forked from xkcoding/spring-boot-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
304 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# 基础镜像 | ||
FROM openjdk:8-jdk-alpine | ||
|
||
# 作者信息 | ||
MAINTAINER "Yangkai.Shen [email protected]" | ||
|
||
# 添加一个存储空间 | ||
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
# spring-boot-demo-docker | ||
|
||
> 本 demo 主要演示了如何容器化一个 Spring Boot 项目。通过 `Dockerfile` 的方式打包成一个 images 。 | ||
## Dockerfile | ||
|
||
```dockerfile | ||
# 基础镜像 | ||
FROM openjdk:8-jdk-alpine | ||
|
||
# 作者信息 | ||
MAINTAINER "Yangkai.Shen [email protected]" | ||
|
||
# 添加一个存储空间 | ||
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 | ||
<properties> | ||
<dockerfile-version>1.4.9</dockerfile-version> | ||
</properties> | ||
|
||
<plugins> | ||
<plugin> | ||
<groupId>com.spotify</groupId> | ||
<artifactId>dockerfile-maven-plugin</artifactId> | ||
<version>${dockerfile-version}</version> | ||
<configuration> | ||
<repository>${project.build.finalName}</repository> | ||
<tag>${project.version}</tag> | ||
<buildArgs> | ||
<JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE> | ||
</buildArgs> | ||
</configuration> | ||
<executions> | ||
<execution> | ||
<id>default</id> | ||
<phase>package</phase> | ||
<goals> | ||
<goal>build</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
``` | ||
|
||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<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/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>spring-boot-demo-docker</artifactId> | ||
<version>1.0.0-SNAPSHOT</version> | ||
<packaging>jar</packaging> | ||
|
||
<name>spring-boot-demo-docker</name> | ||
<description>Demo project for Spring Boot</description> | ||
|
||
<parent> | ||
<groupId>com.xkcoding</groupId> | ||
<artifactId>spring-boot-demo</artifactId> | ||
<version>1.0.0-SNAPSHOT</version> | ||
</parent> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> | ||
<java.version>1.8</java.version> | ||
<dockerfile-version>1.4.9</dockerfile-version> | ||
</properties> | ||
|
||
<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> | ||
|
||
<build> | ||
<finalName>spring-boot-demo-docker</finalName> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
<plugin> | ||
<groupId>com.spotify</groupId> | ||
<artifactId>dockerfile-maven-plugin</artifactId> | ||
<version>${dockerfile-version}</version> | ||
<configuration> | ||
<repository>${project.build.finalName}</repository> | ||
<tag>${project.version}</tag> | ||
<buildArgs> | ||
<JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE> | ||
</buildArgs> | ||
</configuration> | ||
<executions> | ||
<execution> | ||
<id>default</id> | ||
<phase>package</phase> | ||
<goals> | ||
<goal>build</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
25 changes: 25 additions & 0 deletions
25
...g-boot-demo-docker/src/main/java/com/xkcoding/docker/SpringBootDemoDockerApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.xkcoding.docker; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
/** | ||
* <p> | ||
* 启动器 | ||
* </p> | ||
* | ||
* @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); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
spring-boot-demo-docker/src/main/java/com/xkcoding/docker/controller/HelloController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
|
||
/** | ||
* <p> | ||
* Hello Controller | ||
* </p> | ||
* | ||
* @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!"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
server: | ||
port: 8080 | ||
servlet: | ||
context-path: /demo |
16 changes: 16 additions & 0 deletions
16
...t-demo-docker/src/test/java/com/xkcoding/docker/SpringBootDemoDockerApplicationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() { | ||
} | ||
|
||
} |