hipcheck icon indicating copy to clipboard operation
hipcheck copied to clipboard

Speed up Docker image build

Open alilleybrinker opened this issue 1 year ago • 7 comments
trafficstars

It's very slow (roughly 40 minutes): https://github.com/mitre/hipcheck/actions/runs/9617044102

Should look into how to speed this up. It's not a task we run frequently, but this still seems excessive.

alilleybrinker avatar Jun 21 '24 18:06 alilleybrinker

The free tier github workflow runners aren't that beefy, so that's one issue. The other one is that yall aren't doing any caching for your docker layers both for downloading dependencies and actually compiling all the stuff (which to my understanding has been a significant pain point for rust for forever). I don't know if you want to necessarily introduce caching into your docker builds - we don't for the various SAF apps, but we're also ok with the usually <10min it takes to build Heimdall.

Amndeep7 avatar Jun 21 '24 22:06 Amndeep7

Yeah that all sounds right. The current state is with nothing done to make builds in Docker better.

alilleybrinker avatar Jun 22 '24 01:06 alilleybrinker

One thing to be aware of in the area of improving the interaction between Rust builds and Docker image caching is cargo-chef. This is a custom Cargo subcommand designed to make Rust builds play nicely with Docker image layer caches by separating out a "build the dependencies" step from a "build the application" step in the Docker file.

Docker caches images layer-by-layer, and you can maximize the benefit of this caching by essentially ordering layers such that the layers which change more frequently are later in the files than the layers that change less frequently. Here, we are assuming that our own application code changes more frequently than our dependencies, and so we can benefit from caching dependencies separately to avoid expensive dependency rebuilds.

In our case at the moment, I don't know that this would help us substantially as we're only doing Docker builds when we release new versions (at minimum every 4 weeks), and we should generally expect at least some dependencies may change in that span.

However, we may make Docker builds more frequent in the future, and the cargo chef logic certainly shouldn't hurt build times much, so it may be useful anyway.

alilleybrinker avatar Jun 24 '24 15:06 alilleybrinker

@cstepanian I think the first step here will be to work on some benchmarking in CI for this performance, ideally with some sort of historic tracking. This also ties into tracking we want to do for runtime performance. You, me, and @vcfxb should sync about these overlapping efforts.

alilleybrinker avatar Jul 04 '24 15:07 alilleybrinker

I did some docker caching for rust in some previous open source work and the biggest improvement for me was building the whole project, stripping out any binaries and artifacts generated by my code (leaving ones from dependencies), saving the docker image, and then rebuilding again (this time just artifacts from the current crate). Rust doesn't play well with stuff like that by default, but it can be done.

vcfxb avatar Jul 04 '24 15:07 vcfxb

Sounds similar to what cargo chef does! https://github.com/LukeMathWalker/cargo-chef

alilleybrinker avatar Jul 04 '24 15:07 alilleybrinker

@cstepanian any update on this?

alilleybrinker avatar Aug 05 '24 15:08 alilleybrinker

Closing since we changed the Docker image to use prebuilt binaries.

alilleybrinker avatar Nov 29 '24 19:11 alilleybrinker