docker加速构建镜像_如何通过利用Docker多阶段构建来加速Node的重建

严兴旺
2023-12-01

docker加速构建镜像

by John Standish

约翰·斯坦迪什(John Standish)

如何通过利用Docker多阶段构建来加速Node的重建 (How to speed up Node re-builds by leveraging docker multi-stage builds)

Reinstalling npm dependencies can be a wasteful use of time and money. Depending on the size of your project, this can take several minutes. In my personal experience, I’ve seen npm install take upwards of 5 minutes. Now, if you have separate stages (Gate, CI, different environment branches) to your pipeline, this waiting time gets even worse. All jokes aside from the picture below, waiting is pricey!

重新安装npm依赖项可能浪费时间和金钱。 根据项目的大小,这可能需要几分钟。 以我的个人经验,我已经看到npm install需要花费5分钟以上的时间。 现在,如果您对管道有单独的阶段(门,配置项,不同的环境分支),则等待时间会变得更糟。 除了下面的图片,所有的笑话,等待都是昂贵的!

浪费很贵,很贵 (Waste is expensive, very expensive)

TL; DR (TL;DR)

It’s a significant amount of money (~$9,918/yr per developer) and time (18,792 minutes per year or 13.05 days per year) that’s wasted waiting for dependencies to install while our code goes through its pipeline. These numbers are an average of 4 check-ins per day. On the low end, waiting for the gate, it’s ~$3,132/yr per developer. See calculations below for where I got those numbers.

这是大量的金钱( 每个开发人员每年约9,918美元 )和时间(每年18,792分钟或每年13.05天),浪费了我们的代码通过其管道时要等待安装依赖项的时间。 这些数字平均每天4次签到。 在低端,等待入场费,每个开发人员每年约3,132美元有关我从何处获得这些数字,请参见下面的计算。

Let’s do some quick math to see why 5 minutes is a big problem. Assume you have a Gate, and a CI build for your 2 environments (Staging and Production). Each stage requires you to start a clean build.

让我们做一些快速数学运算,看看为什么5分钟是个大问题。 假设您有一个Gate和一个针对两个环境(暂存和生产)的CI构建。 每个阶段都要求您开始全新的构建。

So let’s add up how long we’re waiting for npm install to complete:

因此,让我们加总等待npm install完成的时间:

Build time: 1 minuteGate: 5 minutesStaging CI: 5 MinutesProduction CI: 5 MinutesNPM Wait Time: 15 minutesTotal Build Time: 18 minutes

构建时间:1分钟门控:5分钟登台CI:5分钟生产CI:5分钟NPM等待时间:15分钟 总构建时间:18分钟

Okay, so 18 minutes doesn’t seem that bad. That’s a coffee break, and we all love coffee. But that 18 minutes is idle time, time waiting for something to go out the door.

好吧,所以18分钟似乎还不错。 喝咖啡休息时间,我们都喜欢喝咖啡。 但是那18分钟是空闲时间,是等待东西出来的时间。

Now let’s expand that math a bit and multiply by a small team (4 developers), and for fun, we’ll figure an average amount check-ins and an hourly rate. Time is money, right? The average amount of check-ins is what I’ve seen in my day job, and your numbers may vary.

现在让我们稍微扩展一下数学,然后由一个小团队(4个开发人员)相乘,并且为了娱乐,我们将得出平均签入量和每小时费用。 时间就是金钱,对不对? 平均签到数量是我在日常工作中看到的数量,您的数量可能有所不同。

Build Time: 3 minutesNPM Wait Time: 15 minutesDevelopers: 4Avg. Check-Ins: 4Hourly Rate: $30 (your hourly rate may be higher)

建立时间:3分钟NPM等待时间:15分钟开发人员:4平均。 入住:4小时收费:$ 30(您的小时费率可能更高)

Gate Wait Time: 96 minutes (Gate wait time X Developers X Avg Check-Ins)Gate Cost: $48 (Gate Wait Time in hours x Hourly Rate)Total Time: 288 minutes (Build and NPM time X Developers X Avg Check-Ins)Cost: $144/day (Total Time in hours X Hourly Rate)

闸门等待时间:96分钟 (闸门等待时间X开发人员X平均登记入住时间) 闸门成本:$ 48(闸门等待时间小时数x小时收费) 总时间:288分钟(构建和NPM时间X开发人员X平均登记时间) 费用:144美元/天(总时间(小时)X每小时费用)

So we’re looking $144/day of idle time, or $720/week, or $37,584/yr. And that’s waiting for our software to ship! On the low end, if we check-in our code and wait for the gate, that’s $12,528/yr. YIKES! The yearly cost was based on 261 American working days in a year (https://hr.uiowa.edu/payroll/2015-fiscal-year-payroll-calendar)

因此,我们希望每天的空闲时间为144美元,或每周为720 美元 ,或每年为37,584美元 。 这正在等待我们的软件发布! 在低端,如果我们签入代码并等待入场费则为$ 12,528 / yr 。 赞! 年度费用基于一年中的261个美国工作日( https://hr.uiowa.edu/payroll/2015-fiscal-year-payroll-calendar )

让我们缓存并构建这个东西! (Let’s cache and build this thing!)

TL; DR (TL;DR)

Here are my instructions on how to run. https://github.com/jstandish/cached-node-module-build-example/blob/master/DOCKER_BUILD.md

这是我的运行说明。 https://github.com/jstandish/cached-node-module-build-example/blob/master/DOCKER_BUILD.md

Alright, we’ve established that waiting for things is expensive. So as such, we should try and reduce how long we spend on npm install steps. We should only re-run npm install when the package.json file changes. By selectively running this we can significantly reduce the amount of time for new Gate/CI/CD builds from several minutes to less than a minute (depends on the size of your project).

好了,我们已经确定等待的代价是昂贵的。 因此,我们应该尝试减少在npm install步骤上花费的时间。 当package.json文件更改时,我们仅应重新运行npm install 。 通过有选择地运行此程序,我们可以将构建新的Gate / CI / CD的时间从几分钟显着减少到不到一分钟(取决于项目的大小)。

阶段1-创建缓存阶段 (Phase 1 — Create a cache stage)

Our first step will be to create a multi-stage dockerfile. This will allow us to copy in the package.json file and only run a certain stage if that file has changed.

我们的第一步将是创建一个多阶段的dockerfile 。 这将使我们能够复制package.json文件,并且仅在该文件已更改的情况下才运行特定阶段。

阶段2-创建构建阶段 (Phase 2 — Create a build stage)

The next step will be to create the next stage which will pipe a command to npm. This is done using the ENTRYPOINT statement in your dockerfile. This will execute the given command pipe in any arguments. We are using a docker image that has Chromium installed already; this will allow us to run Chrome Headless for our unit tests.

下一步将是创建下一个阶段,该阶段会将命令传递给npm 。 这是用做ENTRYPOINT声明你的dockerfile 。 这将在任何参数中执行给定的命令管道。 我们正在使用已安装Chromium的docker映像; 这将使我们能够运行Chrome Headless进行单元测试。

Here is the complete dockerfile:

这是完整的dockerfile:

第3阶段-建立图片 (Phase 3 — Build the image)

Now that we have our dockerfile set up, let’s build it. You will need to do this every time the files change, but the time required to copy your new files is trivial because docker will skip subsequent layers that haven’t changed. Woohoo!

现在我们已经设置了dockerfile ,让我们构建它。 每次文件更改时都需要执行此操作,但是复制新文件所需的时间微不足道,因为docker会跳过未更改的后续层。 hoo!

This took about 2 minutes. But this could take longer depending on your internet connection, disk speed, CPU, etc.

这花费了大约2 分钟。 但这可能需要更长的时间,具体取决于您的互联网连接,磁盘速度,CPU等。

Any build after our initial docker build will take less time because we will only re-run npm install if the package.json file has changed!

最初的docker build构建之后的任何构建都将花费更少的时间,因为只有在package.json文件已更改的情况下,我们才重新运行npm install

阶段4-构建代码! (Phase 4 — Build the code!)

So now let’s build our code inside the node-build-test image. We’ll specify a mount point so we can copy our build output to it. This will allow us to extract the compiled code from the dockerized environment! I am using a forked angular project as an example, but you can use this now for any project.

现在,让我们在node-build-test映像中构建代码。 我们将指定一个安装点,以便可以将构建输出复制到该安装点。 这将使我们能够从dockerized环境中提取编译后的代码! 我以叉形角项目为例,但是您现在可以将其用于任何项目。

The build time took about 45 seconds. But that was compiling our code, not waiting for npm install. YES!

构建时间大约花费了45秒。 但这就是编译我们的代码,而不是等待npm install 。 是!

And we now have our compiled files!

现在我们有了我们的编译文件!

因此,所有后续构建只需45秒? (So, it’ll only be 45 seconds for all subsequent builds?)

Yes! Because the npm install step is completely skipped, because the package.json file hasn’t changed, you’ll gain the benefits. If you change the package.json, you’ll incur the same time penalty you would have anyways.

是! 由于完全跳过了npm install步骤,因为package.json文件没有更改,所以您将获得好处。 如果更改package.json ,则将产生相同的时间损失。

那么,我们节省了多少时间/金钱? (So how much time/money did we save?)

So let’s go back to our calculation that we did previously, and now subtract our npm install wait time. We’ll keep the build time in there because you can’t get away from that.

因此,让我们回到之前所做的计算,现在减去npm install等待时间。 我们将在此处保留构建时间,因为您无法摆脱它。

Build Time: 3 minuteDevelopers: 4Avg. Check-Ins: 4Hourly Rate: $30 (your hourly rate may be higher)

建立时间:3分钟开发商:4Avg。 入住:4小时收费:$ 30(您的小时费率可能更高)

Gate Time: 16 minutes (Gate wait time X Developers X Avg Check-Ins)Gate Cost: $8 (Gate Wait Time in hours x Hourly Rate)Total Time: 48 minutes (Build time X Developers X Avg Check-Ins x Environments)Cost: $24/day (Total Time in hours X Hourly Rate)

登机时间:16分钟 (登机口等待时间X开发人员X平均登记入住时间) 登机成本:$ 8(登机口等待时间小时数x小时收费) 总时间:48分钟(构建时间X开发商X平均登机时间x环境) 成本:$ 24 /天(总时间(小时)X每小时费用)

So let’s look at that over a day, week, and year.

因此,让我们看一下一天,一周和一年中的情况。

Day: $24Week: $120Year: $6,264

日:$ 24 周:$ 120 年:$ 6,264

我们为4个开发人员团队节省了多少? (And how much did we save for our team of 4 developers?)

The below is the format of (previous amount — new amount). And yes, it’s a lot of savings over the year!

以下是( 先前金额-新金额 )的格式。 是的,一年中节省了很多钱!

Day: ($144 - $24) = $120Week: ($720 - $120) =$600Year: ($37,584 - $6,264) = $31,320

日:($ 144- $ 24)= $ 120 星期:($ 720-$ 120)= $ 600 年:($ 37,584- $ 6,264)= $ 31,320

结论 (Conclusion)

I hope you have enjoyed seeing how utilizing a docker multi-stage build can save you a significant amount of time, as well as money. Docker multi-stage builds are very powerful and can enable you to ship and build faster.

我希望您很高兴看到利用Docker多阶段构建可以如何为您节省大量时间和金钱。 Docker多阶段构建功能非常强大,可以使您快速交付和构建。

If you want to play around with this, please clone my GitHub repository and have fun!

如果您想玩这个游戏,请克隆我的GitHub存储库并玩得开心!

https://github.com/jstandish/cached-node-module-build-example

https://github.com/jstandish/cached-node-module-build-example

Thanks for reading!

谢谢阅读!

https://www.instagram.com/john.does.code

https://www.instagram.com/john.does.code

翻译自: https://www.freecodecamp.org/news/speed-up-node-re-builds-leveraging-docker-multi-stage-builds-and-save-money-65189a4ab115/

docker加速构建镜像

 类似资料: