dockerprepare {
commonService = ['org.springframework.boot:spring-boot-starter-web','org.springframework.boot:spring-boot-starter-actuator']
}
Place your own Dockerfile in this directory to override the default:
${project.rootDir}/src/main/docker/
src/main/dockerroot/Dockerfile:
dockerprepare {
commonService = ['org.springframework.boot:spring-boot-starter-web','org.springframework.boot:spring-boot-starter-actuator']
dockerSrcDirectory = "${project.rootDir}/src/main/dockerroot/
}
plugins {
id 'org.springframework.boot' version '1.5.10-RELEASE'
id 'com.garyclayburg.dockerprepare' version '1.2.1'
id "com.bmuschko.docker-remote-api" version "3.1.0"
id 'java'
id 'eclipse'
}
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dockerprepare {
commonService = ['org.springframework.boot:spring-boot-starter-web']
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
def dockerImageName = 'myorg/' + project.name
task buildImage(type: com.bmuschko.gradle.docker.tasks.image.DockerBuildImage, dependsOn:
'dockerLayerPrepare') {
description = "build and tag a Docker Image"
inputDir = project.file(dockerprepare.dockerBuildDirectory)
tags = [dockerImageName + ':latest', dockerImageName + ':' + version]
}
task pushVersion(type: com.bmuschko.gradle.docker.tasks.image.DockerPushImage, dependsOn:
buildImage) {
description = "docker push <imageName>:<version>"
imageName = dockerImageName + ":" + version
}
task pushLatest(type: com.bmuschko.gradle.docker.tasks.image.DockerPushImage, dependsOn:
buildImage) {
description = "docker push <imageName>:latest"
imageName = dockerImageName + ":latest"
}
FROM openjdk:8u131-jre-alpine
# We choose this base image because:
# 1. it is the latest Java 8 version on alpine as of September 2017
# 2. jre-alpine instead of jdk-alpine is much smaller but still enough to
# run most microservice applications on the JVM
# 3. jre-alpine has a smaller security footprint than other official Java docker images
# 4. the explicit version number means the build will be repeatable
# i.e. not dependent on what :latest version may have been pulled from a
# docker registry before.
RUN adduser -D -s /bin/sh springboot
COPY ./bootrunner.sh /home/springboot/bootrunner.sh
RUN chmod 755 /home/springboot/bootrunner.sh && chown springboot:springboot /home/springboot/bootrunner.sh
WORKDIR /home/springboot
USER springboot
# We add a special springboot user for running our application.
# Java applications do not need to be run as root
ADD commonServiceDependenciesLayer1 /home/springboot/app/
# This layer is composed of all transitive dependencies of a
# commonService, e.g in your build.gradle:
#
#dockerprepare {
# commonService = ['org.springframework.boot:spring-boot-starter-web']
#}
#
# All 30 jar files pulled in from spring-boot-starter-web are added to this layer
ADD dependenciesLayer2/ /home/springboot/app/
# This layer contains dependent jar files of the app that aren't a
# commonService. Most of the time,
# having dependencies in this layer will take advantage of the docker build
# cache. This will give you faster build times, faster image
# uploads/downloads and reduced storage requirements.
# This layer is computed automatically from your spring boot application
ADD classesLayer3/ /home/springboot/app/
# This layer contains your application classes. It will
# likely change on each docker image build so we expect a docker cache miss.
# This layer is computed automatically from your spring boot application
VOLUME /tmp
EXPOSE 8080
ENV JAVA_OPTS=""
ENTRYPOINT ["./bootrunner.sh"]
#!/bin/sh
date_echo(){
datestamp=$(date "+%F %T")
echo "${datestamp} $*"
}
#exec the JVM so that it will get a SIGTERM signal and the app can shutdown gracefully
if [ -d "${HOME}/app" ]; then
#execute springboot expanded jar, which may have been constructed from several image layers
date_echo "exec java ${JAVA_OPTS} -Djava.security.egd=file:/dev/./urandom -cp ${HOME}/app org.springframework.boot.loader.JarLauncher $*"
# shellcheck disable=SC2086
exec java ${JAVA_OPTS} -Djava.security.egd=file:/dev/./urandom -cp "${HOME}/app" org.springframework.boot.loader.JarLauncher "$@"
elif [ -f "${HOME}/app.jar" ]; then
# execute springboot jar
date_echo "exec java ${JAVA_OPTS} -Djava.security.egd=file:/dev/./urandom -jar ${HOME}/app.jar $*"
# shellcheck disable=SC2086
exec java ${JAVA_OPTS} -Djava.security.egd=file:/dev/./urandom -jar "${HOME}/app.jar" "$@"
else
date_echo "springboot application not found in ${HOME}/app or ${HOME}/app.jar"
exit 1
fi
| Type | Name and description |
|---|---|
java.util.List<java.lang.String> |
commonServiceMarks dependencies to be placed in the top layer of a docker image, e.g. |
java.lang.String |
commonServiceDependenciesDirectoryBuild directory where common service dependencies are placed. |
java.lang.String |
dockerBuildClassesDirectoryBuild directory where classes of this project are placed. |
java.lang.String |
dockerBuildDependenciesDirectoryBuild directory where dependencies of this project are placed. |
java.lang.String |
dockerBuildDirectoryThis directory is the output of this plugin that represents your Spring Boot application in a docker layer-friendly format. |
java.lang.String |
dockerSrcDirectoryAll files or directories in this directory will be copied to the dockerBuildDirectory. |
java.lang.String |
dockerfileSetInstead of using the default Dockerfile and bootrunner.sh, you can specify a different
pre-packaged Dockerfile and bootrunner.sh: |
org.gradle.api.Project |
project |
| Constructor and description |
|---|
DockerPreparePluginExt
(org.gradle.api.Project project) |
DockerPreparePluginExt
() |
| Type Params | Return Type | Name and description |
|---|---|---|
|
void |
commonService(java.lang.String[] commonService)Marks dependencies to be placed in the top layer of a docker image, e.g. |
|
void |
dockerBuildDirectory(java.lang.String dockerBuildDirectory)This directory is the output of this plugin that represents your Spring Boot application in a docker layer-friendly format. |
|
void |
dockerSrcDirectory(java.lang.String sourceDirectory)All files or directories in this directory will be copied to the dockerBuildDirectory. |
|
void |
dockerfileSet(java.lang.String dockerfileset)Instead of using the default Dockerfile and bootrunner.sh, you can specify a different
pre-packaged Dockerfile and bootrunner.sh: |
|
java.lang.Object |
dockerprepare(groovy.lang.Closure closure) |
|
void |
setCommonService(java.lang.String[] commonService)Marks dependencies to be placed in the top layer of a docker image, e.g. |
|
void |
setDockerBuildDirectory(java.lang.String dockerBuildDirectory)This directory is the output of this plugin that represents your Spring Boot application in a docker layer-friendly format. |
|
void |
setDockerSrcDirectory(java.lang.String dockerSrcDirectory)All files or directories in this directory will be copied to the dockerBuildDirectory. |
|
void |
setDockerfileSet(java.lang.String dockerfileSet)Instead of using the default Dockerfile and bootrunner.sh, you can specify a different
pre-packaged Dockerfile and bootrunner.sh: |
| Methods inherited from class | Name |
|---|---|
class java.lang.Object |
java.lang.Object#wait(long, int), java.lang.Object#wait(long), java.lang.Object#wait(), java.lang.Object#equals(java.lang.Object), java.lang.Object#toString(), java.lang.Object#hashCode(), java.lang.Object#getClass(), java.lang.Object#notify(), java.lang.Object#notifyAll() |
Marks dependencies to be placed in the top layer of a docker image, e.g.
commonService = ['org.springframework.boot:spring-boot-starter-web','org.springframework.boot:spring-boot-actuator']
This example will place all the jar files associated with the embedded tomcat server
in the top layer of the docker image. This should allow for better docker cache hits
when you have several independent projects/docker images that share this same
commonService setting
commonService - list of dependencies common to many projectsBuild directory where common service dependencies are placed. The directory name must match the Directory in your Dockerfile.
${project.buildDir}/docker/commonServiceDependenciesLayer1
Build directory where classes of this project are placed. The directory name must match the Directory in your Dockerfile.
${project.buildDir}/docker/classesLayer3
Build directory where dependencies of this project are placed. The directory name must match the Directory in your Dockerfile.
${project.buildDir}/docker/dependenciesLayer2
This directory is the output of this plugin that represents your Spring Boot application in a docker layer-friendly format. This directory should be used as a context for creating a docker image. For example, to create the docker image from this directory manually:
$ docker build -t myname/myapp .
Defaults to: ${project.buildDir}/docker
All files or directories in this directory will be copied to the dockerBuildDirectory. If you want to use a custom Dockerfile to create your image, it should be placed here.
If the directory does not exist or is empty, a default Dockerfile and start script for your app will be placed in the dockerBuildDirectory
Defaults to "${project.rootDir}/src/main/docker/"
sourceDirectory - Source directory for adding files to docker Instead of using the default Dockerfile and bootrunner.sh, you can specify a different
pre-packaged Dockerfile and bootrunner.sh:
To use files from 90111-jre-sid-buildlabels
dockerfileSet = '90111-jre-sid-buildlabels'
dockerfileSet = '8u151-jre-alpine-buildlabels'
dockerfileSet = 'defaultdocker'
If not specified, the Dockerfile and bootrunner.sh file from the dockerfileSet defaultdocker will be used, unless you also have files in dockerSrcDirectory
Marks dependencies to be placed in the top layer of a docker image, e.g.
commonService = ['org.springframework.boot:spring-boot-starter-web','org.springframework.boot:spring-boot-actuator']
This example will place all the jar files associated with the embedded tomcat server
in the top layer of the docker image. This should allow for better docker cache hits
when you have several independent projects/docker images that share this same
commonService setting
commonService - list of dependencies common to many projectsThis directory is the output of this plugin that represents your Spring Boot application in a docker layer-friendly format. This directory should be used as a context for creating a docker image. For example, to create the docker image from this directory manually:
$ docker build -t myname/myapp .
Defaults to: ${project.buildDir}/docker
All files or directories in this directory will be copied to the dockerBuildDirectory. If you want to use a custom Dockerfile to create your image, it should be placed here.
If the directory does not exist or is empty, a default Dockerfile and start script for your app will be placed in the dockerBuildDirectory
Defaults to "${project.rootDir}/src/main/docker/"
sourceDirectory - Source directory for adding files to docker Instead of using the default Dockerfile and bootrunner.sh, you can specify a different
pre-packaged Dockerfile and bootrunner.sh:
To use files from 90111-jre-sid-buildlabels
dockerfileSet = '90111-jre-sid-buildlabels'
dockerfileSet = '8u151-jre-alpine-buildlabels'
dockerfileSet = 'defaultdocker'
If not specified, the Dockerfile and bootrunner.sh file from the dockerfileSet defaultdocker will be used, unless you also have files in dockerSrcDirectory
Marks dependencies to be placed in the top layer of a docker image, e.g.
commonService = ['org.springframework.boot:spring-boot-starter-web','org.springframework.boot:spring-boot-actuator']
This example will place all the jar files associated with the embedded tomcat server
in the top layer of the docker image. This should allow for better docker cache hits
when you have several independent projects/docker images that share this same
commonService setting
commonService - list of dependencies common to many projectsThis directory is the output of this plugin that represents your Spring Boot application in a docker layer-friendly format. This directory should be used as a context for creating a docker image. For example, to create the docker image from this directory manually:
$ docker build -t myname/myapp .
Defaults to: ${project.buildDir}/docker
All files or directories in this directory will be copied to the dockerBuildDirectory. If you want to use a custom Dockerfile to create your image, it should be placed here.
If the directory does not exist or is empty, a default Dockerfile and start script for your app will be placed in the dockerBuildDirectory
Defaults to "${project.rootDir}/src/main/docker/"
sourceDirectory - Source directory for adding files to docker Instead of using the default Dockerfile and bootrunner.sh, you can specify a different
pre-packaged Dockerfile and bootrunner.sh:
To use files from 90111-jre-sid-buildlabels
dockerfileSet = '90111-jre-sid-buildlabels'
dockerfileSet = '8u151-jre-alpine-buildlabels'
dockerfileSet = 'defaultdocker'
If not specified, the Dockerfile and bootrunner.sh file from the dockerfileSet defaultdocker will be used, unless you also have files in dockerSrcDirectory
Groovy Documentation