Skip to content

Commit 80af73f

Browse files
committed
使用gradle-docker插件构建项目镜像
1 parent afa4a9d commit 80af73f

File tree

6 files changed

+124
-0
lines changed

6 files changed

+124
-0
lines changed

SpringBoot-Builder-Docker/.gitignore

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
.gradle
2+
/build/
3+
!gradle/wrapper/gradle-wrapper.jar
4+
5+
### STS ###
6+
.apt_generated
7+
.classpath
8+
.factorypath
9+
.project
10+
.settings
11+
.springBeans
12+
.sts4-cache
13+
14+
### IntelliJ IDEA ###
15+
.idea
16+
*.iws
17+
*.iml
18+
*.ipr
19+
/out/
20+
21+
### NetBeans ###
22+
/nbproject/private/
23+
/nbbuild/
24+
/dist/
25+
/nbdist/
26+
/.nb-gradle/

SpringBoot-Builder-Docker/Dockerfile

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
FROM openjdk:8
2+
LABEL maintainer="[email protected]"
3+
WORKDIR /
4+
ARG JAR_FILE
5+
ADD ${JAR_FILE} app.jar
6+
EXPOSE 8080
7+
ENTRYPOINT ["java","-jar","app.jar"]

SpringBoot-Builder-Docker/README.md

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
## Gradle 构建 SpringBoot 并打包镜像Demo
2+
3+
使用 gradle-docker 插件
4+
5+
构建子模块镜像,在子模块的build.gralde配置插件
6+
7+
具体参照,我写的一篇文章https://blog.csdn.net/qq_39211866/article/details/81123694
8+
9+
配置完了,在主目录下构建,命令如下
10+
```text
11+
# 构建项目,忽略测试用例
12+
gradle clean build -x test
13+
# 构建镜像
14+
gradle SpringBoot-Builder-Docker:docker
15+
16+
```
17+
18+
![](https://ws1.sinaimg.cn/large/006mOQRagy1fymsk4xqzxj31gm04m755.jpg)
19+
20+
构建成功:
21+
![](https://ws1.sinaimg.cn/large/006mOQRagy1fymsp7smuaj317q03sgmk.jpg)
22+
23+
启动镜像:
24+
```text
25+
docker run -p 8080:8080 -d docker/springboot-docker:1.0.0
26+
```
27+
-p 挂载宿主机端口;
28+
-d 后台启动容器。
29+
30+
31+
访问https://localhost:8080/,可以看到成功调用接口。
32+
![](https://ws1.sinaimg.cn/large/006mOQRagy1fymsra1jb6j30nu0aqq3o.jpg)
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
buildscript {
2+
repositories {
3+
maven { url "https://plugins.gradle.org/m2/" }
4+
maven { url "http://maven.aliyun.com/nexus/content/groups/public/" }
5+
mavenCentral()
6+
}
7+
dependencies {
8+
classpath("org.springframework.boot:spring-boot-gradle-plugin:2.0.4.RELEASE")
9+
//引入gradle-docker
10+
classpath('gradle.plugin.com.palantir.gradle.docker:gradle-docker:0.19.2')
11+
}
12+
}
13+
//引入构建docker插件
14+
apply plugin: 'com.palantir.docker'
15+
apply plugin: 'org.springframework.boot'
16+
apply plugin: 'application'
17+
18+
group = 'docker'
19+
version = project.findProperty('projVersion') ?: '1.0.0'
20+
mainClassName = 'com.dashuai.learning.docker.DockerApplication'
21+
22+
tasks.withType(JavaCompile) {
23+
options.encoding = 'UTF-8'
24+
}
25+
dependencies {
26+
compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '2.0.4.RELEASE'
27+
testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: '2.0.4.RELEASE'
28+
}
29+
//具体配置生成镜像
30+
docker {
31+
dockerfile file('Dockerfile')
32+
name "${project.group}/springboot-docker:${project.version}"
33+
files jar.archivePath
34+
buildArgs(['JAR_FILE': "${jar.archiveName}"])
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.dashuai.learning.docker;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.web.bind.annotation.RequestMapping;
6+
import org.springframework.web.bind.annotation.RestController;
7+
8+
import java.util.Date;
9+
10+
@SpringBootApplication
11+
@RestController
12+
public class DockerApplication {
13+
14+
@RequestMapping("/")
15+
public String getDate(){
16+
return new Date().toString();
17+
}
18+
19+
public static void main(String[] args) {
20+
SpringApplication.run(DockerApplication.class, args);
21+
}
22+
23+
}
24+

SpringBoot-Builder-Docker/src/main/resources/application.properties

Whitespace-only changes.

0 commit comments

Comments
 (0)