ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

使用 Docker 的多阶段构建来缩短构建时间

2022-11-02 15:00:26  阅读:280  来源: 互联网

标签:Docker 阶段 构建 缩短 语言


使用 Docker 的多阶段构建来缩短构建时间

就我而言,构建时间比图像大小更严重。
这是因为我最常使用的语言是脚本语言。

但是,如果使用得当,这种多阶段构建似乎可以大大减少构建时间。

让我们试一试。以一个简单的Ruby on Rails和Node.js资产环境为例,我们将从单阶段构建开始,并尝试从那里进行改进。
FROM ruby:3.1
RUN curl -fsSL https://deb.nodesource.com/setup_16.x | bash - && apt-get install -y nodejs
RUN npm install --global yarn@1.22.19
RUN gem install bundler
RUN mkdir -p /rails
WORKDIR /rails
COPY . .
RUN bundle install
RUN yarn install
RUN RAILS_ENV=production bundle exec rails assets:precompile
CMD ["bin/rails", "s"].
最后,铁轨
RUN bundle install
RUN yarn install
RUN RAILS_ENV=production bundle exec rails assets:precompile
完成,但这是最有可能花费最多时间的地方。
更烦人的是,如果修改了任何源,
COPY . .
图层缓存将被激活,因此请务必使用
RUN bundle install
RUN yarn install
RUN RAILS_ENV=production bundle exec rails assets:precompile
将被执行。
由于层缓存机制,情况始终如此。

缓存失效后,所有后续 Dockerfile 命令都会生成新映像,并且不会使用缓存。

Dockerfile 编写 Dockerfile 的最佳实践

要解决这个问题,下一步是准备一个阶段。builder
FROM ruby:3.1 as builder
WORKDIR /tmp
RUN curl -fsSL https://deb.nodesource.com/setup_16.x | bash - && apt-get install -y nodejs
RUN npm install --global yarn@1.22.19
RUN gem install bundler
COPY Gemfile Gemfile.lock . / /
RUN bundle install
COPY package.json yarn.lock . /
RUN yarn install
COPY app/assets app/assets
COPY app/javascript app/javascript
COPY bin bin
COPY config config
COPY Rakefile vite.config.ts . /
RUN RAILS_ENV=production bundle exec rails assets:precompile

FROM ruby:3.1 as app
RUN mkdir -p /rails
WORKDIR /rails
COPY --from=builder /usr/local/bundle /usr/local/bundle
COPY --from=builder /tmp/public/vite public/vite
COPY . .
CMD ["bin/rails", "s"].
这里值得注意。
COPY --from=builder /usr/local/bundle /usr/local/bundle
COPY --from=builder /tmp/public/vite public/vite
COPY . .
--from允许您从其他阶段复制文件,但问题是顺序**。
首先,您需要复制可能耗时的COPY
COPY --from=builder /usr/local/bundle /usr/local/bundle
COPY --from=builder /tmp/public/vite public/vite
首先写入,然后是:
COPY . .
除非修改其中一个,否则不会执行 Now。bundle installGemfileGemfile.lock

但是,问题仍然存在。
如果您更改任何 ,它甚至会运行 。GemfileGemfile.lockyarn install

因此,让我们现在进入最终形式。
FROM ruby:3.1 as builder
RUN curl -fsSL https://deb.nodesource.com/setup_16.x | bash - && apt-get install -y nodejs
RUN npm install --global yarn@1.22.19

FROM ruby:3.1 as bundler
WORKDIR /tmp
RUN gem install bundler
COPY Gemfile Gemfile.lock . /tmp
RUN bundle install

FROM node as yarn
WORKDIR /tmp
COPY package.json yarn.lock . / / RUN yarn install
RUN yarn install

FROM builder as assets
WORKDIR /tmp
COPY --from=bundler /usr/local/bundle /usr/local/bundle
COPY --from=yarn /tmp/node_modules node_modules
COPY app/assets app/assets
COPY app/javascript app/javascript
COPY bin bin
COPY config config
COPY Rakefile Gemfile Gemfile.lock package.json yarn.lock vite.config.ts . /
RUN RAILS_ENV=production bundle exec rails assets:precompile

FROM ruby:3.1 as app
RUN mkdir -p /rails
WORKDIR /rails
COPY --from=bundler /usr/local/bundle /usr/local/bundle
COPY --from=assets /tmp/public/vite public/vite
COPY . .
CMD ["bin/rails", "s"].

标签:Docker,阶段,构建,缩短,语言
来源:

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有