多阶段 Docker 构建复制失败,找不到文件

Multistage Docker Build COPY fails, file not found

我有一个小型 Jekyll 站点的多阶段 Docker文件。

Docker文件:

FROM jekyll/minimal AS build

COPY . /srv/jekyll

RUN jekyll build

FROM pierrezemb/gostatic

COPY --from=build /srv/jekyll/_site /srv/http

Docker 在最后阶段失败,出现以下错误:

Step 5/5 : COPY --from=build /srv/jekyll/_site /srv/http
COPY failed: stat /var/lib/docker/overlay2/e6b407b63b9578dd7ae4ccba968fff3f4e28e35e50e887c09319b32ccd548356/merged/srv/jekyll/_site: no such file or directory

如果我将第二个 FROM 和 exec 删除到构建容器中,我可以看到文件存在于 /srv/jekyll/_site

我拿走了你的dockerfile,并按照jekyll quickstart tutorial。虽然我无法实际构建您的 dockerfile,因为您选择了 jekyll/minimal 基本映像,但将其更改为 jekyll/builder 会使整个过程进行微小的更改。我正在 /tmp 文件夹中构建。

Truncated...
Fetching minima 2.5.0
Installing minima 2.5.0
Bundle complete! 4 Gemfile dependencies, 29 gems now installed.
Bundled gems are installed into `/usr/local/bundle`
The dependency tzinfo-data (>= 0) will be unused by any of the platforms Bundler is installing for. Bundler is installing for ruby but the dependency is only for x86-mingw32, x86-mswin32, x64-mingw32, java. To add those platforms to the bundle, run `bundle lock --add-platform x86-mingw32 x86-mswin32 x64-mingw32 java`.
ruby 2.6.0p0 (2018-12-25 revision 66547) [x86_64-linux-musl]
Configuration file: /tmp/_config.yml
            Source: /tmp
       Destination: /tmp/_site
 Incremental build: disabled. Enable with --incremental
      Generating...
       Jekyll Feed: Generating feed for posts
                    done in 0.507 seconds.
 Auto-regeneration: disabled. Use --watch to enable.
Removing intermediate container 10159e9e7776
 ---> cab3989600a7
Step 5/6 : FROM pierrezemb/gostatic
 ---> bbc54b2880be
Step 6/6 : COPY --from=build /tmp/_site /srv/http
 ---> 860f5db9d0f3
Successfully built 860f5db9d0f3
Successfully tagged test:latest

如果你把你的代码发给我 GitHub link 我可以看看,也许你在某处打错了字?

(这是我的 dockerfile,与 jekyll 的教程一起使用)

FROM jekyll/builder as build
WORKDIR /tmp
COPY . /tmp

RUN jekyll build

FROM pierrezemb/gostatic

COPY --from=build /tmp/_site /srv/http

看起来 /srv/jekyll 在您的父映像中被定义为一个卷。如果您从该图像创建容器,目录将不是来自图像,而是来自创建的临时卷,从而导致意外行为。在构建期间,如果您尝试使用 运行 命令更改该目录的内容,这些更改将在该 运行 命令结束时全部丢失,因为匿名卷已被清除。

我建议让该映像的上游创建者从他们的 Dockerfile 中删除 VOLUME 定义,或者分叉存储库并在没有该卷的情况下构建您自己的存储库。您始终可以在 运行 时定义卷,而无需在映像中定义卷,但一旦在映像中定义卷,您使用目录的能力将受到该卷的影响。