Skip to content

Commit ec89024

Browse files
committed
Spring Boot Docker
1 parent 34f2a5c commit ec89024

File tree

7 files changed

+121
-0
lines changed

7 files changed

+121
-0
lines changed

springboot-docker/Dockerfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
FROM java:8
2+
MAINTAINER "Jonsson Yan"
3+
EXPOSE 8080
4+
ARG JAR_FILE=target/*.jar
5+
COPY ${JAR_FILE} app.jar
6+
ENTRYPOINT ["java","-jar","/springboot-docker.jar"]

springboot-docker/README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Dockerfile
2+
3+
```dockerfile
4+
# 基础镜像
5+
FROM java:8
6+
7+
# 作者信息
8+
MAINTAINER "Jonsson Yan"
9+
10+
# 暴露8080端口
11+
EXPOSE 8080
12+
13+
# 添加变量,如果使用dockerfile-maven-plugin,则会自动替换这里的变量内容
14+
ARG JAR_FILE=target/*.jar
15+
16+
# 复制jar包到容器中
17+
COPY ${JAR_FILE} app.jar
18+
19+
# 启动镜像自动运行程序
20+
ENTRYPOINT ["java","-jar","/springboot-docker.jar"]
21+
```
22+
23+
# 手动打包
24+
25+
```shell
26+
# 前往 Dockerfile 目录,打开命令行执行
27+
docker build -t springboot-docker .
28+
# 查看生成镜像
29+
docker images
30+
# 运行
31+
docker run -d -p 8080:8080 --name springboot-docker-demo springboot-docker
32+
# 查看启动日志
33+
docker logs -n 300 -f springboot-docker-demo
34+
```
35+
36+
# Reference
37+
- Docker 官方文档:https://docs.docker.com/
38+
- Dockerfile 命令 参考文档:https://docs.docker.com/engine/reference/builder/
39+
- Maven插件使用 参考地址:https://github.com/spotify/dockerfile-maven
40+
- Spring Boot with Docker:https://spring.io/guides/gs/spring-boot-docker/

springboot-docker/pom.xml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>2.3.2.RELEASE</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<groupId>com.springboot</groupId>
12+
<artifactId>springboot-docker</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>springboot-docker</name>
15+
<description>Demo project for Spring Boot Docker</description>
16+
<properties>
17+
<java.version>1.8</java.version>
18+
</properties>
19+
<dependencies>
20+
<dependency>
21+
<groupId>org.springframework.boot</groupId>
22+
<artifactId>spring-boot-starter-web</artifactId>
23+
</dependency>
24+
</dependencies>
25+
26+
<build>
27+
<plugins>
28+
<plugin>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-maven-plugin</artifactId>
31+
</plugin>
32+
</plugins>
33+
</build>
34+
35+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.springboot;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class SpringbootDockerApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(SpringbootDockerApplication.class, args);
11+
}
12+
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.springboot.controller;
2+
3+
import org.springframework.stereotype.Controller;
4+
import org.springframework.web.bind.annotation.RequestMapping;
5+
import org.springframework.web.bind.annotation.RestController;
6+
7+
@RestController
8+
public class TestController {
9+
@RequestMapping("/helloDocker")
10+
public String helloDocker() {
11+
return "Hello Docker!";
12+
}
13+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.springboot;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.springframework.boot.test.context.SpringBootTest;
5+
6+
@SpringBootTest
7+
class SpringbootDockerApplicationTests {
8+
9+
@Test
10+
void contextLoads() {
11+
}
12+
13+
}

0 commit comments

Comments
 (0)