Reputation: 24767
I've followed the official spring boot docker for gradle tutorial, but I can't make it work.
My build.gradle
buildscript {
repositories {
mavenCentral()
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:2.1.6.RELEASE")
classpath('gradle.plugin.com.palantir.gradle.docker:gradle-docker:0.22.1')
}
}
group = 'springio'
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'com.palantir.docker'
task unpack(type: Copy) {
dependsOn bootJar
from(zipTree(tasks.bootJar.outputs.files.singleFile))
into("build/dependency")
}
docker {
name "${project.group}/${bootJar.baseName}"
copySpec.from(tasks.unpack.outputs).into("dependency")
buildArgs(['DEPENDENCY': "dependency"])
}
bootJar {
baseName = 'globalstatus'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
implementation('org.springframework.boot:spring-boot-starter-web')
testImplementation('org.springframework.boot:spring-boot-starter-test')
}
My Dockerfile
FROM openjdk:8-jdk-alpine
VOLUME /tmp
ARG JAR_FILE
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
gradlew build
gives me a valid and runnable JAR
But if I do gradlew build docker
and then start the container I get
Error: Invalid or corrupt jarfile /app.jar
Upvotes: 2
Views: 1141
Reputation: 4721
The app.jar
should be an executable jar
file but it is instead a directory. Because the JAR_FILE
is not set as build argument the COPY
instruction copies the content of the <project_dir>/build/docker
directory to the /app.jar
directory in the container. You can list it with:
docker run --entrypoint="ls" springio/globalstatus -la /app.jar
To fix the build you need to change the docker task:
docker {
name "${project.group}/${bootJar.baseName}"
files bootJar.outputs
buildArgs(['JAR_FILE': bootJar.archiveFileName.get()])
}
Explanation:
The docker gradle plugin work directory is <project_dir>/build/docker
so when the docker image is build file operations are relative to this path.
files
copies the output of the bootJar
task into the build/docker
directory. The buildArgs
sets the build argument JAR_FILE
to the name of the archive (the spring boot jar file). When the image is build from Dockerfile
, the COPY
instruction will add the jar file to the root directory of the container.
Upvotes: 2