Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.jar file not found when building a Docker container with Palantir Gradle plug-in

If I try to build a Docker container with a Spring Boot application under Windows 10, I get the following error:

> Task :docker FAILED
COPY failed: stat /var/lib/docker/tmp/docker-builder711841135/myproject.jar: no such file or directory

I'm using Docker Community Edition in version 18.03.0-ce-win59 (16762) and Gradle 4.7 with Java 8.

build.gradle (shortened):

plugins {
    id 'java'
    id 'org.springframework.boot' version '2.0.1.RELEASE'
    id "com.palantir.docker" version "0.19.2"
}

version = '2.0.0'
sourceCompatibility = 1.8
group = "com.example"

repositories {
    mavenCentral()
}

bootJar {
    archiveName 'myproject.jar'
}

dependencies {
    ...
}

docker {
    dependsOn(build)
    name "${project.group}/${jar.baseName}"
    files bootJar
}

Dockerfile (sibling of build.gradle in the top-level project directory):

FROM openjdk:8-jre
COPY build/libs/myproject.jar myproject.jar

ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/myproject.jar"]

If I build the Docker container with Docker only (without Gradle) it works.

How can I let Gradle (or Docker?) find the file myproject.jar?

like image 871
deamon Avatar asked Apr 20 '18 12:04

deamon


2 Answers

The problem is the COPY command in the Docker file:

COPY build/libs/myproject.jar myproject.jar

The source directory build/libs/ is not where the files for building the Docker container reside. Instead the directory build/docker/ is used as Docker build context. When COPY is executed this directory is the effective working directory.

The correct COPY command is as simple as this:

COPY myproject.jar /

Docker task:

docker {
    dependsOn bootJar
    name "${project.group}/${jar.baseName}:${version}"
    files bootJar.archivePath
}

If you want to copy resources too, you need to add processResources to the files parameter:

files bootJar.archivePath, processResources
like image 95
deamon Avatar answered Oct 16 '22 22:10

deamon


That seems consistent with palantir/gradle-docker issue 76 which specifies:

I configured a new project according to examples, what I observe is that entire project (minus build/) is copied to build/docker. I causes an unnecessary large docker build context.

Either the COPY should be build/docker/libs/myproject.jar, or build/ itself is not included at all.

The issue mentioned a possible workaround overriding another docker step - dockerPrepare - preparing the 'build/docker' folder explicitly.

I need only a single fatJar in addition to the Dockerfile:

task dockerCopy(type: MoveTo, dependsOn: dockerPrepare) {
    from('build/packed') {
        include "${jarFullName}"
    }
    into 'build/docker'
}
like image 24
VonC Avatar answered Oct 16 '22 22:10

VonC