【问题标题】:Multistage Docker Build COPY fails, file not foundMultistage Docker Build COPY 失败,找不到文件
【发布时间】:2021-01-30 22:26:44
【问题描述】:

我有一个用于小型 Jekyll 站点的多级 Dockerfile。

Dockerfile:

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 中。

【问题讨论】:

    标签: docker jekyll


    【解决方案1】:

    我已经获取了您的 dockerfile,并关注了 jekyll quickstart tutorial。虽然由于您选择了 jekyll/minimal 基础映像,我实际上无法构建您的 dockerfile,但将其更改为 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 链接到你的代码,我可以看看,也许你在某个地方打错了?

    (这是我的 dockerfile,适用于 jekyll 的教程)

    FROM jekyll/builder as build
    WORKDIR /tmp
    COPY . /tmp
    
    RUN jekyll build
    
    FROM pierrezemb/gostatic
    
    COPY --from=build /tmp/_site /srv/http
    

    【讨论】:

    • 这个技巧(使用/tmp)对我有用。接受的答案并不是真的有用(让上游 Dockerfile 的创建者修改它)。
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2019-08-23
      • 2018-06-27
      • 2018-12-25
      • 1970-01-01
      • 1970-01-01
      • 2021-03-19
      • 2021-03-06
      • 1970-01-01
      • 2021-05-04
      相关资源
      最近更新 更多