在复制任务上构建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 .
docker gradle任务
docker {
name "${project.group}/${bootJar.baseName}"
copySpec.from(tasks.unpack.outputs).into("dependency")
buildArgs(['DEPENDENCY': "dependency"])
}
答案 0 :(得分:2)
问题
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 :(得分:1)
您收到“没有这样的文件或目录”错误,看来这是事实。
Dockerfile设置:
ARG DEPENDENCY=target/dependency
然后尝试进行COPY
操作:
COPY ${DEPENDENCY}/BOOT-INF/lib /app/lib
如果您解析${DEPENDENCY}
,则COPY
命令如下所示:
COPY target/dependency/BOOT-INF/lib /app/lib
存储库中没有target
目录。也许这是您应该按照本教程创建的内容?从该文档中:
此Dockerfile具有一个DEPENDENCY参数,该参数指向我们解开胖子jar的目录。如果我们做对了,它已经包含一个包含依赖项jar的BOOT-INF / lib目录,以及一个其中包含应用程序类的BOOT-INF / classes目录。请注意,我们正在使用应用程序自己的主类hello.Application(这比使用胖子启动器提供的间接调用要快)。
答案 2 :(得分:0)
在您的项目中运行
<div class="is-visible item-box__container">
<section class="item-box js-catalog-item" data-id="377519381" data-distance="7.5 km" data-track-event="user.click_list_item" data-track-event-params="{"id":377519381,"content_type":"item","channel":"c","content_source":"catalog","position":2}">
<figure class="item-box__media media media--item">
<div class="media__placeholder">
<figcaption class="media-caption">
<div class="media-caption__header">
<a class="item-box__brand" href="/brand/g-shock">G-Shock</a>
</div>
<div class="media-caption__body">
Montre G-SHOCK, choc resist
En très bon état, très peu portée
Noire et verte
</div>
</figcaption>
<a class="media__image-wrapper js-item-link is-loaded" data-brand="G-Shock" style="background-color: #E0DACA" href="/hommes/montres/377519381-montre-casio-g-shock">
<img alt="Montre Casio G-SHOCK" title="Montre Casio G-SHOCK" width="310" height="430" class="js-item-thumbnail item-thumbnail lazy-thumbnail __act_as_lazy loaded" data-src="https://images.vinted.net/thumbs/310x430/07155_aGa3Ye8bvFPHqMWKFzjFSLyP.jpeg?1577297436$80bbacb963888041f628527d9953a9f002a27897" data-item-id="377519381" data-item-source="c" data-content-type="item" data-content-source="catalog" data-item-owner-id="31967105" data-disable-tracking="false" data-bumped="false" data-page="1" data-per-page="24" style="background-color: #E0DACA" src="https://images.vinted.net/thumbs/310x430/07155_aGa3Ye8bvFPHqMWKFzjFSLyP.jpeg?1577297436$80bbacb963888041f628527d9953a9f002a27897">
</a> </div>
</figure>
<div class="item-box__details">
<a class="item-box__avatar circle u-float-right is-loaded" href="/membres/31967105-nicolascarpenzano">
<img width="30" height="30" data-src="https://images.vinted.net/thumbs/50x50/05ac7_CNKk3J2PPa4vaP26wpPApxJQ.jpeg?1575225047$9534574dc11ad39f9b48bc94c13dcbc7333a9229" class="item-box__avatar-picture circle lazy-thumbnail __act_as_lazy loaded" src="https://images.vinted.net/thumbs/50x50/05ac7_CNKk3J2PPa4vaP26wpPApxJQ.jpeg?1575225047$9534574dc11ad39f9b48bc94c13dcbc7333a9229">
</a>
<div class="item-box__title">
<span>35,00 €</span>
</div>
<div class="item-box__subtitle u-align-items-center">
<div class="u-flexbox">
</div>
</div>
</div>
<footer class="item-box__footer">
<ul class="nav item-box__nav">
<li class="nav-item">
<div class="u-flexbox u-justify-content-between">
<div>
<div class="u-flexbox u-align-items-center js-toggle-favourite-state u-cursor-pointer is-fav-loaded" data-entry-type="10" data-entry-id="377519381" data-state="false" data-entity-title="Montre Casio G-SHOCK" data-currency="€" data-price="35.0" data-count="4" data-label="count" data-source="item" data-ui-components="true"> <div class="js-favourite-icon">
<div class="js-favourite-on u-line-height-default u-hidden">
<div class="c-icon--small c-icon"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16">
<path fill="#FB7567" fill-rule="evenodd" d="M0 5.73685c0 2.9354 2.664 5.029 4.667 6.60768C6.75 13.9842 7.333 14.33613 8 15c.667-.66387 1.224-1.0508 3.333-2.65547C13.366 10.79883 16 8.64027 16 5.70486c0-4.30814-5.333-6.63868-8-2.65547C5.333-.93384 0 1.3967 0 5.73683z"></path>
</svg>
</div>
</div>
<div class="js-favourite-off u-line-height-default "></div>
<div class="c-spacer--small c-spacer--vertical c-spacer"></div>
<div class="js-favourite-count">4</div></div>
</div>
<div>Boosté</div>
</div>
</li>
</ul>
</footer>
</section></div>
答案 3 :(得分:0)
我遇到了同样的问题。解决这个问题:
docker 构建
命令,只需在项目的根目录(Dockerfile 和目标存在的地方)运行此命令:
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 .