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

2022/11/2 23:24:52

本文主要是介绍使用 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 的多阶段构建来缩短构建时间的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程