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?
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
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 tobuild/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'
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With