Skip to content

Commit

Permalink
BREAKING CHANGE: baseImage made required (automatic base image select…
Browse files Browse the repository at this point in the history
…ion removed)
  • Loading branch information
augi committed Feb 17, 2022
1 parent db2753a commit 19c12a5
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 47 deletions.
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Gradle Docker Java Plugin [![Build](https://github.com/augi/gradle-docker-java/actions/workflows/build.yml/badge.svg)](https://github.com/augi/gradle-docker-java/actions/workflows/build.yml) [![Version](https://badgen.net/maven/v/maven-central/cz.augi/gradle-docker-java)](https://repo1.maven.org/maven2/cz/augi/gradle-docker-java/)

Gradle plugin that wraps your JVM application to a new Docker image.
The image has [standard labels](http://label-schema.org/rc1/) derived from the build environment (environment variables, Git).
The image has [standard labels](http://label-schema.org/rc1/) and [OCI annotations](https://github.com/opencontainers/image-spec/blob/main/annotations.md) derived from the build environment (environment variables, Git).
Almost all the logic on Dockerfile generation is in [this file](src/main/groovy/cz/augi/gradle/dockerjava/DistDockerTask.groovy).

The plugin takes product of `distTar` task (added by [the application plugin](https://docs.gradle.org/current/userguide/application_plugin.html)) and wraps it to Docker image.
Expand All @@ -18,14 +18,12 @@ The plugin is published to [Gradle Plugins portal](https://plugins.gradle.org/pl
}

dockerJava {
baseImage = 'my-org/our-base-image:1.2.3'
image = "myorg/my-app:$version" // name of the resulting Docker image; mandatory
alternativeImages = ["myorg/my-app:latest"] // array of alternative image names; default is empty
ports = [80] // list of exposed ports; default: empty
labels = ['mylabel':'mylabelvalue'] // additonal labels of Dockerfile; default: empty
volumes = ['/my-folder'] // list of volumes; default: empty
baseImage = 'my-org/our-base-image:1.2.3' // default: automatically choosed the best based on current Docker platform and Java version
javaVersion = JavaVersion.VERSION_1_8 // Java version used to choose appropriate base Docker image; default: project.targetCompatibility
windowsBaseImageSpecifier = 'windowsservercore-ltsc2016' // suffix of AdoptOpenJDK Docker image that will be used as base image; another option is 'windowsservercore-1809'
dockerfileLines = ['RUN apt-get ...'] // additional lines to include to Dockerfile; default: empty
arguments = ['--server'] // arguments to be passed to your application; default: empty
dockerBuildDirectory = project.file('my-directory') // directory where Dockerfile is created; default: "$buildDir/dockerJava"
Expand Down
40 changes: 3 additions & 37 deletions src/main/groovy/cz/augi/gradle/dockerjava/DistDockerTask.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class DistDockerTask extends DefaultTask {
def dockerFile = new File(workDir, 'Dockerfile')
dockerFile.delete()
if (dockerExecutor.getDockerPlatform().toLowerCase().contains('win')) {
dockerFile << 'FROM ' + (settings.baseImage ?: getWindowsBaseImage()) + '\n'
dockerFile << 'FROM ' + settings.baseImage + '\n'
dockerFile << 'SHELL ["cmd", "/S", "/C"]\n'
if (settings.ports.any()) {
dockerFile << 'EXPOSE ' + settings.ports.join(' ') + '\n'
Expand All @@ -47,7 +47,7 @@ class DistDockerTask extends DefaultTask {
dockerFile << "WORKDIR C:\\\\bin\n"
dockerFile << "ENTRYPOINT ${startScripts.windowsScript.name} ${settings.arguments.join(' ')}"
} else {
dockerFile << 'FROM ' + (settings.baseImage ?: getLinuxBaseImage()) + '\n'
dockerFile << 'FROM ' + settings.baseImage + '\n'
if (settings.ports.any()) {
dockerFile << 'EXPOSE ' + settings.ports.join(' ') + '\n'
}
Expand All @@ -57,40 +57,10 @@ class DistDockerTask extends DefaultTask {
dockerFile << "COPY $unpackedDistributionDir /var/app\n"
dockerFile << "COPY $applicationJarFilename /var/app/lib\n"
dockerFile << "WORKDIR /var/app/bin\n"
if (!settings.baseImage && settings.javaVersion == JavaVersion.VERSION_1_8) {
dockerFile << 'ENV JAVA_OPTS="-XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap $JAVA_OPTS"\n'
}
dockerFile << "ENTRYPOINT [\"./${startScripts.unixScript.name}\"${settings.arguments.collect { ",\"$it\"" }.join('')}]"
}
}

private String getWindowsBaseImage() {
getAdoptOpenJdkBaseImageNamePrefix() + (settings.windowsBaseImageSpecifier ? ('-' + settings.windowsBaseImageSpecifier) : '')
}

private String getLinuxBaseImage() {
getAdoptOpenJdkBaseImageNamePrefix()
}

private String getAdoptOpenJdkBaseImageNamePrefix() {
switch (settings.javaVersion) {
case JavaVersion.VERSION_1_8:
return 'adoptopenjdk:8u272-b10-jre-hotspot'
case JavaVersion.VERSION_1_9:
case JavaVersion.VERSION_1_10:
case JavaVersion.VERSION_11:
return 'adoptopenjdk:11.0.9_11-jre-hotspot'
case JavaVersion.VERSION_12:
case JavaVersion.VERSION_13:
case JavaVersion.VERSION_14:
return 'adoptopenjdk:14.0.2_12-jre-hotspot'
case JavaVersion.VERSION_15:
return 'adoptopenjdk:15.0.1_9-jre-hotspot'
default:
throw new RuntimeException("Java version ${settings.javaVersion} is not supported")
}
}

private Map<String, String> getLabels() {
def url = getUrl()
def vcsUrl = getVcsUrl()
Expand Down Expand Up @@ -198,11 +168,7 @@ interface DistDockerSettings {
String getImage()
@Input @Optional
String[] getAlternativeImages()
@Input @Optional
JavaVersion getJavaVersion()
@Input @Optional
String getWindowsBaseImageSpecifier()
@Input @Optional
@Input
String getBaseImage()
@Input @Optional
Integer[] getPorts()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import org.gradle.api.Project
import org.gradle.api.logging.Logger
import org.gradle.platform.base.Platform
import org.gradle.process.ExecSpec
import org.gradle.util.VersionNumber
import org.gradle.util.internal.VersionNumber

class DockerExecutor {
private final Project project
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ class DockerJavaExtension implements DistDockerSettings, DockerPushSettings {

String image
String[] alternativeImages = []
JavaVersion getJavaVersion() { customJavaVersion ?: project.targetCompatibility }
void setJavaVersion(JavaVersion version) { customJavaVersion = version }
private JavaVersion customJavaVersion
String windowsBaseImageSpecifier = 'windowsservercore-ltsc2016'
String baseImage
Integer[] ports = []
String[] volumes = []
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cz.augi.gradle.dockerjava

import org.gradle.testfixtures.ProjectBuilder
import org.gradle.util.VersionNumber
import org.gradle.util.internal.VersionNumber
import spock.lang.Specification

class DockerExecutorTest extends Specification {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class DockerJavaPluginTest extends Specification {
}
mainClassName = 'cz.augi.gradle.dockerjava.TestApp'
dockerJava {
baseImage = 'adoptopenjdk:8u272-b10-jre-hotspot'
image = "test/my-app:$version"
ports = [80, 8080]
volumes = ['/test-volume']
Expand Down

0 comments on commit 19c12a5

Please sign in to comment.