Docker COPY 没有那个文件或目录

Docker COPY no such file or directory

构建 docker 图像复制任务失败。无此文件或目录。我正在使用你好世界example from spring

从 openjdk:8-jdk-alpine

建造

Run echo ${PWD} 打印 / Run ls 打印一组普通目录(/usr /var 等)但不存在项目文件

为什么 docker 没有使用 WORKING 目录?

FROM openjdk:8-jdk-alpine
VOLUME /tmp
ARG DEPENDENCY=target/dependency
COPY ${DEPENDENCY}/BOOT-INF/lib /app/lib
COPY ${DEPENDENCY}/META-INF /app/META-INF
COPY ${DEPENDENCY}/BOOT-INF/classes /app
ENTRYPOINT ["java","-cp","app:app/lib/*","hello.Application"]

要复制的文件由 gradle 准备,我可以确认它们存在:

task unpack(type: Copy) {
    dependsOn bootJar
    from(zipTree(tasks.bootJar.outputs.files.singleFile))
    into("build/dependency")
}

我是运行

docker build .

dockergradle任务

docker {
    name "${project.group}/${bootJar.baseName}"
    copySpec.from(tasks.unpack.outputs).into("dependency")
    buildArgs(['DEPENDENCY': "dependency"])
}

您遇到了 "no such file or directory" 错误,看起来确实如此。

Dockerfile 集:

ARG DEPENDENCY=target/dependency

然后尝试 COPY 操作:

COPY ${DEPENDENCY}/BOOT-INF/lib /app/lib

如果你解析 ${DEPENDENCY}COPY 命令看起来像:

COPY target/dependency/BOOT-INF/lib /app/lib

并且存储库中没有 target 目录。也许这是您应该按照教程创建的东西?来自该文档:

This Dockerfile has a DEPENDENCY parameter pointing to a directory where we have unpacked the fat jar. If we get that right, it already contains a BOOT-INF/lib directory with the dependency jars in it, and a BOOT-INF/classes directory with the application classes in it. Notice that we are using the application’s own main class hello.Application (this is faster than using the indirection provided by the fat jar launcher).

运行 这个在你的项目里面

mkdir target/dependency
(cd target/dependency; jar -xf ../*.jar)

问题

docker build -t springio/gs-spring-boot-docker .
Sending build context to Docker daemon  16.78MB
Step 1/8 : FROM openjdk:8-jdk-alpine
 ---> a3562aa0b991
Step 2/8 : RUN addgroup -S spring && adduser -S spring -G spring
 ---> Using cache
 ---> 5b122a1db135
Step 3/8 : USER spring:spring
 ---> Using cache
 ---> 3bdba2028e42
Step 4/8 : ARG DEPENDENCY=target/dependency
 ---> Using cache
 ---> 616945cc41ed
Step 5/8 : COPY ${DEPENDENCY}/BOOT-INF/lib /app/lib
COPY failed: file not found in build context or excluded by .dockerignore: stat target/dependency/BOOT-INF/lib: file does not exist

修复

mkdir -p target/dependency && (cd target/dependency; jar -xf ../*.jar)

将创建以下文件夹布局。 这样做是为了解释 docker 图片的分层选项。 Example 3. Dockerfile

中都有解释
   $pwd
   /java-training/workspace/gs-spring-boot-docker/complete
   $tree -L 3 target/dependency
    target/dependency
    ├── BOOT-INF
    │   ├── classes
    │   │   ├── application.yml
    │   │   └── hello
    │   ├── classpath.idx
    │   └── lib
    │       ├── jackson-annotations-2.11.0.jar
    │       ├── jackson-core-2.11.0.jar
    │       ├── jackson-databind-2.11.0.jar
    │       ├── jackson-datatype-jdk8-2.11.0.jar
    │       ├── jackson-datatype-jsr310-2.11.0.jar
    │       ├── jackson-module-parameter-names-2.11.0.jar
    │       ├── jakarta.annotation-api-1.3.5.jar
    │       ├── jakarta.el-3.0.3.jar
    │       ├── jul-to-slf4j-1.7.30.jar
    │       ├── log4j-api-2.13.2.jar
    │       ├── log4j-to-slf4j-2.13.2.jar
    │       ├── logback-classic-1.2.3.jar
    │       ├── logback-core-1.2.3.jar
    │       ├── slf4j-api-1.7.30.jar
    │       ├── snakeyaml-1.26.jar
    │       ├── spring-aop-5.2.6.RELEASE.jar
    │       ├── spring-beans-5.2.6.RELEASE.jar
    │       ├── spring-boot-2.3.0.RELEASE.jar
    │       ├── spring-boot-autoconfigure-2.3.0.RELEASE.jar
    │       ├── spring-boot-starter-2.3.0.RELEASE.jar
    │       ├── spring-boot-starter-json-2.3.0.RELEASE.jar
    │       ├── spring-boot-starter-logging-2.3.0.RELEASE.jar
    │       ├── spring-boot-starter-tomcat-2.3.0.RELEASE.jar
    │       ├── spring-boot-starter-web-2.3.0.RELEASE.jar
    │       ├── spring-context-5.2.6.RELEASE.jar
    │       ├── spring-core-5.2.6.RELEASE.jar
    │       ├── spring-expression-5.2.6.RELEASE.jar
    │       ├── spring-jcl-5.2.6.RELEASE.jar
    │       ├── spring-web-5.2.6.RELEASE.jar
    │       ├── spring-webmvc-5.2.6.RELEASE.jar
    │       ├── tomcat-embed-core-9.0.35.jar
    │       └── tomcat-embed-websocket-9.0.35.jar
    ├── META-INF
    │   ├── MANIFEST.MF
    │   └── maven
    │       └── org.springframework
    └── org
        └── springframework
            └── boot

现在可以搭建了!

 docker build -t springio/gs-spring-boot-docker .
Sending build context to Docker daemon  33.44MB
Step 1/8 : FROM openjdk:8-jdk-alpine
 ---> a3562aa0b991
Step 2/8 : RUN addgroup -S spring && adduser -S spring -G spring
 ---> Using cache
 ---> 5b122a1db135
Step 3/8 : USER spring:spring
 ---> Using cache
 ---> 3bdba2028e42
Step 4/8 : ARG DEPENDENCY=target/dependency
 ---> Using cache
 ---> 616945cc41ed
Step 5/8 : COPY ${DEPENDENCY}/BOOT-INF/lib /app/lib
 ---> Using cache
 ---> 1d1404d8b0e7
Step 6/8 : COPY ${DEPENDENCY}/META-INF /app/META-INF
 ---> Using cache
 ---> ad8c03fa8c4e
Step 7/8 : COPY ${DEPENDENCY}/BOOT-INF/classes /app
 ---> Using cache
 ---> 2aaadf41ccd2
Step 8/8 : ENTRYPOINT ["java","-cp","app:app/lib/*","hello.Application"]
 ---> Using cache
 ---> a21e7f0c88fc
Successfully built a21e7f0c88fc
Successfully tagged springio/gs-spring-boot-docker:latest

祝你好运!

我遇到了同样的问题。解决这个问题:

  1. 在执行你的
  2. 之前

docker build

command,只是 运行 这对项目根目录中的命令(Dockerfile 和 target 存在):

mkdir target/dependency
(cd target/dependency; jar -xf ../*.jar)

或者, 2. 只需在 .gitlab-ci.yml 文件中添加这两个命令(如果使用 gitlab CI/CD 管道):

docker-build:
  stage: package
  tags:
    - vasci2_shell_runner
  script:
    - mkdir target/dependency
    - (cd target/dependency; jar -xf ../*.jar)
    - docker build -t nexus.css.ir:30005/vas/harim/apc:lastest .