build.gradle:
// Apply the java-library plugin to add support for Java Library
apply plugin: 'java-library'
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'docker'
// This is used as the docker image prefix (org)
group = 'chunzhenzyd'
jar {
//用于打包jar文件的名称和版本
baseName = 'test_springboot_docker'
version = '0.0.1'
}
sourceCompatibility = 10
targetCompatibility = 10
// use sudo gradle build buildImage
// tag::task[]
task buildImage(type: Docker, dependsOn: build) {
push = true
applicationName = jar.baseName
dockerfile = file('src/main/docker/Dockerfile')
doFirst {
copy {
from jar
into stageDir
}
}
}
// end::task[]
// In this section you declare where to find the dependencies of your project
repositories {
// Use jcenter for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
mavenCentral()
}
dependencies {
// This dependency is exported to consumers, that is to say found on their compile classpath.
api 'org.apache.commons:commons-math3:3.6.1'
// This dependency is used internally, and not exposed to consumers on their own compile classpath.
implementation 'com.google.guava:guava:21.0'
// Use JUnit test framework
testImplementation 'junit:junit:4.12'
compile 'org.springframework.boot:spring-boot-actuator:1.5.3.RELEASE'
compile 'org.springframework.boot:spring-boot-starter-web'
testCompile 'org.springframework.boot:spring-boot-starter-test:1.5.3.RELEASE'
}
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.springframework.boot:spring-boot-gradle-plugin:1.5.3.RELEASE'
classpath 'se.transmode.gradle:gradle-docker:1.2'
}
}
Dockerfile: 放在 src/main/docker/
FROM sapmachine/jdk10
VOLUME /tmp
ADD test_springboot_docker-0.0.1.jar app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
执行命令:
sudo gradle build buildImage
(没有sudo的话,可能会出现:java.io.IOException: Cannot run program "docker": error=2, No such file or directory)
docker run -d -p 8080:8080 chunzhenzyd/test_springboot_docker:0.0.1