本文介绍了使用Palantir Gradle插件构建Docker容器时找不到.jar文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我尝试在Windows 10下使用Spring Boot应用程序构建Docker容器,则会出现以下错误:

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

我正在使用Docker Community Edition版本18.03.0-ce-win59(16762)和带有Java 8的Gradle 4.7。

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

build.gradle (简称):

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 (位于顶层项目目录中的build.gradle兄弟):

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"]

如果我仅使用Docker(不使用Gradle)构建Docker容器,则可以使用。

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

如何让Gradle(或Docker?)找到文件myproject.jar?

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

推荐答案

问题是<$ c Docker文件中的$ c> COPY 命令:

COPY build/libs/myproject.jar myproject.jar

源目录 build / libs / 不是用于构建Docker容器的文件所在的位置。而是将目录 build / docker / 用作Docker构建上下文。执行 COPY 时,该目录是有效的工作目录。

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.

正确的 COPY 命令非常简单:

COPY myproject.jar /

Docker任务:

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

如果也要复制资源,需要将 processResources 添加到 files 参数:

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

files bootJar.archivePath, processResources

这篇关于使用Palantir Gradle插件构建Docker容器时找不到.jar文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 14:03
查看更多