程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Docker 构建步骤不持久化文件系统更改大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决Docker 构建步骤不持久化文件系统更改?

开发过程中遇到Docker 构建步骤不持久化文件系统更改的问题如何解决?下面主要结合日常开发的经验,给出你关于Docker 构建步骤不持久化文件系统更改的解决方法建议,希望对你解决Docker 构建步骤不持久化文件系统更改有所启发或帮助;

我有一个多阶段 Dockerfile,用于构建包含 webpacked 前端资产的 Rails 应用。

带有 RUN bin/rails assets:precompile 的步骤调用 webpack 构建所有要发布到 public/packs 的 .Js 和 .CSS 资产。这工作正常。问题是,此 RUN 步骤的更改不会持久化。人们会期望以下内容起作用:

RUN bin/rails assets:precompile && \
    test -d /opt/app/public/packs && \
    ls /opt/app/public/packs/* && \
    du -sh /opt/app/public/packs

RUN ls /opt/app/public/packs/*

第一个命令执行得很好并显示了所有构建的资产。伟大的。第二个命令失败:

ls: cAnnot access '/opt/app/public/packs/*': No such file or directory

我已经使用 --no-cache 构建了图像并获得了相同的结果。我也在我们的 CI 工作流程中看到了同样的结果。

问题是:什么会导致 RUN 步骤的结果无法保存?

这是构建过程的一个例外,表明资产构建工作正常,结果可以看到:

Step 34/35 : RUN bin/rails assets:precompile &&     test -d /opt/app/public/packs &&     ls /opt/app/public/packs/* &&     du -sh /opt/app/public/packs
 ---> Running in 5c31033bd34c                                                                                                                                                   yarn install v1.22.10                                                                                                                                                           [1/4] Resolving packages...
success Already up-to-date.                                                                                                                                                     Done in 0.39s.                                                                                                                                                                  Compiling...
Compiled all packs in /opt/app/public/packs                                                                                                                                     yarn run v1.22.10                                                                                                                                                               $ /opt/node_modules/.bin/webpack --config /opt/app/config/webpack/production.Js
Hash: 3a1c2a59a582aeab432b                                                                                                                                                      Version: webpack 4.44.2
Time: 16798ms                                                                                                                                                                   Built at: 03/23/2021 11:04:01 AM
                                                                    Asset        Size   Chunks                                Chunk names
                                             CSS/application-ad4e93e8.CSS     195 KiB  0,1,2  [emitted] [immutable]         application
                                          CSS/application-ad4e93e8.CSS.br    27.1 KiB           [emitted]
                                          CSS/application-ad4e93e8.CSs.gz    34.9 KiB           [emitted]
                                             CSS/stylesheets-ad4e93e8.CSS     195 KiB        2  [emitted] [immutable]         stylesheets
                                          CSS/stylesheets-ad4e93e8.CSS.br    27.1 KiB           [emitted]
                                          CSS/stylesheets-ad4e93e8.CSs.gz    34.9 KiB           [emitted]
                                   Js/application-71c9136107a4a504df9b.Js     569 KiB  0,2  [emitted] [immutable]  [big]  application
                       Js/application-71c9136107a4a504df9b.Js.liCENSE.txt     2.2 KiB           [emitted]
                                Js/application-71c9136107a4a504df9b.Js.br     122 KiB           [emitted]
                                Js/application-71c9136107a4a504df9b.Js.gz     148 KiB           [emitted]
                               Js/application-71c9136107a4a504df9b.Js.map    1.86 MiB  0,2  [emitted] [dev]               application
                            Js/application-71c9136107a4a504df9b.Js.map.br     402 KiB           [emitted]              [big]
                            Js/application-71c9136107a4a504df9b.Js.map.gz     491 KiB           [emitted]              [big]
                                        Js/images-aef2116965a80a23e5d5.Js    1.31 KiB        1  [emitted] [immutable]         images
                                     Js/images-aef2116965a80a23e5d5.Js.br   600 bytes           [emitted]
                                     Js/images-aef2116965a80a23e5d5.Js.gz   673 bytes           [emitted]
                                    Js/images-aef2116965a80a23e5d5.Js.map    5.13 KiB        1  [emitted] [dev]               images
                                 Js/images-aef2116965a80a23e5d5.Js.map.br    1.72 KiB           [emitted]
                                 Js/images-aef2116965a80a23e5d5.Js.map.gz    1.92 KiB           [emitted]
                                   Js/stylesheets-3d6d364a819b59783614.Js  1010 bytes        2  [emitted] [immutable]         stylesheets
                                Js/stylesheets-3d6d364a819b59783614.Js.br   468 bytes           [emitted]
                                Js/stylesheets-3d6d364a819b59783614.Js.gz   519 bytes           [emitted]
                               Js/stylesheets-3d6d364a819b59783614.Js.map    4.55 KiB        2  [emitted] [dev]               stylesheets
                            Js/stylesheets-3d6d364a819b59783614.Js.map.br    1.55 KiB           [emitted]
                            Js/stylesheets-3d6d364a819b59783614.Js.map.gz    1.72 KiB           [emitted]
                                                            manifest.Json    2.63 KiB           [emitted]
                                                         manifest.Json.br   598 bytes           [emitted]
                                                         manifest.Json.gz   681 bytes           [emitted]
                             media/Fonts/Fontawesome-webFont-674f50d2.eot     162 KiB           [emitted]
                          media/Fonts/Fontawesome-webFont-674f50d2.eot.br    88.3 KiB           [emitted]
                          media/Fonts/Fontawesome-webFont-674f50d2.eot.gz    95.9 KiB           [emitted]
                             media/Fonts/Fontawesome-webFont-912ec66d.svg     434 KiB           [emitted]              [big]
                          media/Fonts/Fontawesome-webFont-912ec66d.svg.br     108 KiB           [emitted]
                          media/Fonts/Fontawesome-webFont-912ec66d.svg.gz     131 KiB           [emitted]
                           media/Fonts/Fontawesome-webFont-af7ae505.woff2    75.4 KiB           [emitted]
                             media/Fonts/Fontawesome-webFont-b06871f2.ttf     162 KiB           [emitted]
                          media/Fonts/Fontawesome-webFont-b06871f2.ttf.br    88.3 KiB           [emitted]
                          media/Fonts/Fontawesome-webFont-b06871f2.ttf.gz    95.8 KiB           [emitted]
                            media/Fonts/Fontawesome-webFont-fee66e71.woff    95.7 KiB           [emitted]
   media/images/Fonts/Noah-ExtraBold-276b1b48304f694814bb94f404ce806c.otf     106 KiB           [emitted]
media/images/Fonts/Noah-ExtraBold-276b1b48304f694814bb94f404ce806c.otf.br      50 KiB           [emitted]
media/images/Fonts/Noah-ExtraBold-276b1b48304f694814bb94f404ce806c.otf.gz    59.5 KiB           [emitted]
   media/images/Fonts/Noah-ExtraBold-c95e32a64b94c190a618a5f7a337b0b6.ttf     135 KiB           [emitted]
media/images/Fonts/Noah-ExtraBold-c95e32a64b94c190a618a5f7a337b0b6.ttf.br    54.9 KiB           [emitted]
media/images/Fonts/Noah-ExtraBold-c95e32a64b94c190a618a5f7a337b0b6.ttf.gz    64.6 KiB           [emitted]
     media/images/Fonts/Noah-Regular-160999b118eefa6cf7dad830b2662250.otf    96.5 KiB           [emitted]
  media/images/Fonts/Noah-Regular-160999b118eefa6cf7dad830b2662250.otf.br    46.4 KiB           [emitted]
  media/images/Fonts/Noah-Regular-160999b118eefa6cf7dad830b2662250.otf.gz    54.4 KiB           [emitted]
      media/images/icon/contact-icon-4948ACB3b4a035afc087d4a49006bdc1.png    1.13 KiB           [emitted]
       media/images/icon/credit-card-5e76418433bf1520bb14dbb2d059f2f6.png    1.04 KiB           [emitted]
                   media/images/logo-a53b7b1aecd172e93f6f7f75aae92186.svg    3.91 KiB           [emitted]
                media/images/logo-a53b7b1aecd172e93f6f7f75aae92186.svg.br    1.43 KiB           [emitted]
                media/images/logo-a53b7b1aecd172e93f6f7f75aae92186.svg.gz    1.67 KiB           [emitted]
               media/images/logo_red-04d88826aba647ad737f64c8fd5f42ec.png    4.13 KiB           [emitted]
             media/images/three-dots-7627bc820cce40098e897ca98413d895.svg    1.47 KiB           [emitted]
          media/images/three-dots-7627bc820cce40098e897ca98413d895.svg.br   307 bytes           [emitted]
          media/images/three-dots-7627bc820cce40098e897ca98413d895.svg.gz   377 bytes           [emitted]
Entrypoint application [big] = CSS/application-ad4e93e8.CSS Js/application-71c9136107a4a504df9b.Js Js/application-71c9136107a4a504df9b.Js.map
Entrypoint images = Js/images-aef2116965a80a23e5d5.Js Js/images-aef2116965a80a23e5d5.Js.map
Entrypoint stylesheets = CSS/stylesheets-ad4e93e8.CSS Js/stylesheets-3d6d364a819b59783614.Js Js/stylesheets-3d6d364a819b59783614.Js.map
 [26] ./app/webpacker/images/logo_red.png 104 bytes {0} {1} [built]
 [27] ./app/webpacker/images/icon/contact-icon.png 113 bytes {0} {1} [built]
 [28] ./app/webpacker/images/icon/credit-card.png 112 bytes {0} {1} [built]
 [44] ./app/webpacker/packs/stylesheets.sCSS 39 bytes {0} {2} [built]
 [45] ./app/webpacker/packs/images.Js + 1 modules 141 bytes {0} {1} [built]
      | ./app/webpacker/packs/images.Js 18 bytes [built]
      | ./app/webpacker/images/index.Js 108 bytes [built]
 [83] ./app/webpacker/packs/application.Js 1.33 KiB {0} [built]
[127] ./app/webpacker/src/JavaScripts/general.Js 86 bytes {0} [built]
[128] ./app/webpacker/src/JavaScripts/cocoon.Js 3.97 KiB {0} [built]
[129] ./app/webpacker/images sync \.(gif|jpg|png|svg|eot|otf|ttf|woff|woff2)$/ 361 bytes {0} [built]
[130] ./app/webpacker/images/Fonts/Noah-ExtraBold.otf 116 bytes {0} [optional] [built]
[131] ./app/webpacker/images/Fonts/Noah-ExtraBold.ttf 116 bytes {0} [optional] [built]
[132] ./app/webpacker/images/Fonts/Noah-Regular.otf 114 bytes {0} [optional] [built]
[133] ./app/webpacker/images/logo.svg 100 bytes {0} [optional] [built]
[134] ./app/webpacker/images/three-dots.svg 106 bytes {0} [optional] [built]
[135] ../node_modules/@rails/actiontext/app/JavaScript/actiontext/index.Js + 1 modules 2.64 KiB {0} [built]
      |    2 modules
    + 122 hIDden modules

WARNING in asset size limit: The following asset(s) exceed the recommended size limit (244 KiB).
This can impact web perfoRMANce.
Assets:
  media/Fonts/Fontawesome-webFont-912ec66d.svg (434 KiB)
  Js/application-71c9136107a4a504df9b.Js (569 KiB)
  Js/application-71c9136107a4a504df9b.Js.map.gz (491 KiB)
  Js/application-71c9136107a4a504df9b.Js.map.br (402 KiB)

WARNING in entrypoint size limit: The following entrypoint(s) combined asset size exceeds the recommended limit (244 KiB). This can impact web perfoRMANce.
Entrypoints:
  application (764 KiB)
      CSS/application-ad4e93e8.CSS
      Js/application-71c9136107a4a504df9b.Js


WARNING in webpack perfoRMANce recommendations:
You can limit the size of your bundles by using @R_301_5565@() or require.ensure to lazy load some parts of your application.
For more info visit https://webpack.Js.org/guIDes/code-splitTing/
Child mini-css-extract-plugin ../node_modules/css-loader/dist/cJs.Js??ref--7-1!../node_modules/postcss-loader/src/index.Js??ref--7-2!../node_modules/sass-loader/dist/cJs.Js??re
f--7-3!app/webpacker/packs/stylesheets.sCSS:
    Entrypoint mini-css-extract-plugin = *
    [0] ../node_modules/css-loader/dist/cJs.Js??ref--7-1!../node_modules/postcss-loader/src??ref--7-2!../node_modules/sass-loader/dist/cJs.Js??ref--7-3!./app/webpacker/packs/st
ylesheets.sCSS 829 KiB {0} [built]
    [3] ./app/webpacker/images/Fonts/Noah-ExtraBold.otf 116 bytes {0} [built]
    [4] ./app/webpacker/images/Fonts/Noah-ExtraBold.ttf 116 bytes {0} [built]
    [5] ./app/webpacker/images/Fonts/Noah-Regular.otf 114 bytes {0} [built]
        + 8 hIDden modules
Done in 17.52s.

/opt/app/public/packs/manifest.Json
/opt/app/public/packs/manifest.Json.br
/opt/app/public/packs/manifest.Json.gz

/opt/app/public/packs/CSS:
application-ad4e93e8.CSS
application-ad4e93e8.CSS.br
application-ad4e93e8.CSs.gz
stylesheets-ad4e93e8.CSS
stylesheets-ad4e93e8.CSS.br
stylesheets-ad4e93e8.CSs.gz

/opt/app/public/packs/Js:
application-71c9136107a4a504df9b.Js
application-71c9136107a4a504df9b.Js.liCENSE.txt
application-71c9136107a4a504df9b.Js.br
application-71c9136107a4a504df9b.Js.gz
application-71c9136107a4a504df9b.Js.map
application-71c9136107a4a504df9b.Js.map.br
application-71c9136107a4a504df9b.Js.map.gz
images-aef2116965a80a23e5d5.Js
images-aef2116965a80a23e5d5.Js.br
images-aef2116965a80a23e5d5.Js.gz
images-aef2116965a80a23e5d5.Js.map
images-aef2116965a80a23e5d5.Js.map.br
images-aef2116965a80a23e5d5.Js.map.gz
stylesheets-3d6d364a819b59783614.Js
stylesheets-3d6d364a819b59783614.Js.br
stylesheets-3d6d364a819b59783614.Js.gz
stylesheets-3d6d364a819b59783614.Js.map
stylesheets-3d6d364a819b59783614.Js.map.br
stylesheets-3d6d364a819b59783614.Js.map.gz

/opt/app/public/packs/media:
Fonts
images
6.4M    /opt/app/public/packs
Removing intermediate container 5c31033bd34c
 ---> 45c4a0be56fe
Step 35/35 : RUN ls /opt/app/public/packs/
 ---> Running in 5ade2c36f7c4
ls: cAnnot access '/opt/app/public/packs/': No such file or directory
The command '/bin/sh -c ls /opt/app/public/packs/' returned a non-zero code: 2

这是整个 Dockerfile:

# Allow the image to be built with any Ruby version.
# This arg is only available at build time and only usable by the `FROM` lines.
ARG ruby_version=2.7

################################################################################
# Base image with dependencIEs to support development and production images.
#
FROM ruby:${ruby_version}-slim AS base

# git-core:         fetching gems stored in git repos
# build-essential   building native gems (gcc,make,etC)
# libpq-dev         POSTGResql development headers for building `pg` gem
RUN apt-get update && \
    apt-get upgrade -y && \
    apt-get install -y --no-install-recommends \
      git-core \
      build-essential \
      libpq-dev \
      curl

# Install node and yarn for dealing with webpack assets
RUN curl -fsSL https://deb.nodesource.com/setup_14.x | bash - && \
    apt-get install -y nodeJs && \
    npm install --global yarn && \
    echo '--modules-folder /opt/node_modules' >> ~/.yarnrc

# update RubyGems system software
RUN gem update --system --silent

# Create a directory for custom binarIEs and executable scripts
RUN mkdir /opt/bin
ENV PATH=/opt/bin:$PATH

# Add Helper script that will ensure the correct version of Bundler is installed
copY docker/install-bundler.sh /opt/bin/install-bundler

# Add entrypoint used during development that ensures gems are up to date
copY docker/entrypoint.dev.sh /opt/bin/entrypoint-dev

# Home of the application source code.
workdir /opt/app
VolUME /opt/app

# Specify the target application environment of this build
# It should be one of:
# development,test,production
ARG app_env

ENV APP_ENV=$app_env \
    NODE_ENV=$app_env \
    RAILS_ENV=$app_env \
    RAILS_LOG_TO_STDOUT=true


################################################################################
# Stage that includes bundler,the gems,and source code
#
# Used by the test runner in CI and for building standlone runnable images
# that will work for any environment. Not used by docker-compose.
FROM base AS app

copY Gemfile Gemfile.lock /opt/app/

RUN install-bundler

# Use Helper script that runs 'bundle install' appropriately given the APP_ENV
copY docker/bundle-install-env.sh /opt/bin/bundle-install-env
RUN bundle-install-env

# copy specific files and directorIEs we need to run the app
copY .rubocop.yml config.ru Rakefile ./
copY app/ ./app/
copY bin/ ./bin/
copY config/ ./config/
copY db/ ./db/
copY lib/ ./lib/
copY public/ ./public/
copY test/ ./test/
copY vendor/ ./vendor/

# Pre-compile and cache code using bootsnap
RUN bin/bootsnap precompile --gemfile app/ lib/


################################################################################
# Builder stage for release image prep
#
FROM app AS builder

# Remove cached .gem files and gem component we don't need (e.g. tests,docs)
# This is to save space in the final image.
RUN rm /usr/local/bundle/cache/*.gem && \
    rm -rf /usr/local/bundle/gems/*/{Benchmarks,doc,example,perf,spec,tests}/

copY babel.config.Js package.Json postCSs.config.Js yarn.lock ./

RUN yarn install

# Rails refuses to run the precompile without a secret_key_base
ENV SECRET_KEY_BASE=00000000000000000000000000000000000000000000

RUN bin/rails assets:precompile && \
    test -d /opt/app/public/packs && \
    ls /opt/app/public/packs/* && \
    du -sh /opt/app/public/packs

RUN ls /opt/app/public/packs/

# ... omitted the final diStribution stage based on a fresh image; it never gets that far

解决方法

删除 VOLUME 行。

VOLUME 指令唯一特别明显的效果是它可以防止任何后续的 RUN 命令更改该目录。在 Dockerfile 中声明 VOLUME 几乎是不必要的(并且卷不适合源代码的存储);您可以删除该行,不会发生任何不好的事情。

大佬总结

以上是大佬教程为你收集整理的Docker 构建步骤不持久化文件系统更改全部内容,希望文章能够帮你解决Docker 构建步骤不持久化文件系统更改所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: