coolify icon indicating copy to clipboard operation
coolify copied to clipboard

[Feature]: cache Docker builds

Open utterstep opened this issue 1 year ago • 5 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

Summary

Reuse already backed layers while building Docker images on Coolify. I guess the issue is "what to do with cloning of a repo on the Build phase". If that helps, I can tackle this myself, given some guidance :)

Why should this be worked on?

I'm using Coolify on my Raspberry Pi 4B. It's proven to be a tremendously useful tool, allowing me to move out from costly cloud deployments with my hobby projects, which is great! But one of my usecases includes development of Rust applications, which are compiling at a rather slow speed on not-so-performant RPi.

I've set my Dockerfile so in most cases I do not need full rebuild, if files are unmodified, which shrinks down build time from ~2000s to ~120s for me, in typical cases. However, due to Coolify cloning repo anew in the beginning of a Build phase, I cannot leverage it.

I think there are a lot of people with usecases like mine, who would gladly use Coolify more if this issue is resolved :)

Again, I'll gladly tackle this issue myself, with some guidance from Coolify team, if that can help :)

utterstep avatar Apr 09 '23 05:04 utterstep

You can use https://github.com/LukeMathWalker/cargo-chef to cache build dependencies and speed-up your docker builds.

otahirs avatar Apr 09 '23 11:04 otahirs

Hi, @otahirs! Thanks for your reaction :)

You can use https://github.com/LukeMathWalker/cargo-chef to cache build dependencies and speed-up your docker builds.

Yeah, I know about cargo-chef, and it's a wonderful tool, but I'm not quite sure it'll help in this case.

As I know, chef works by creating recipe.json file, which specifies deps of the crate which is being built. And as long as recipe.json is not changed, Docker will notice it and after the first run it will reuse the dependencies layer, and in my case it would surely have the same content.

But the key point here is that while deciding, whether file was changed, or not, Docker uses not only it's content, but also metadata, such as creation/modification time.

And, as I see, Coolify clones the whole repo anew each time the build is triggered. So in that case, Docker will perceive recipe.json as a new file (it's creation timestamp will be different) and will run chef prepare from the scratch, instead of reusing it, if my understanding is correct.

So what I'm looking for is a way to somehow preserve the repo between the builds, leading to having only the actual changed files metadata modified and, consequently, usage of cached layers :)

utterstep avatar Apr 09 '23 12:04 utterstep

just give it a try :)

...
[20:20:00.046] #12 CACHED
[20:20:00.048] #18 [builder 6/8] RUN cargo chef cook --release --recipe-path recipe.json
[20:20:00.051] #18 sha256:1e3ef2337c094c2f7ed4351b043016af5b070eb22a97319c389c13e94250a925
[20:20:00.053] #18 CACHED
[20:20:00.054] #19 [builder 7/8] COPY . .
[20:20:00.058] #19 sha256:c9aeefab3f6efafaacbe2abad3fe2af86598c5a3756a71c610f8122724b3df72
[20:20:00.068] #19 DONE 0.0s
...

image

otahirs avatar Apr 09 '23 12:04 otahirs

Thanks for your involvement!

It somehow doesn't work in my case :( Screenshot 2023-04-09 at 22 19 49

I'll try to investigate more tomorrow. In case you'd be interested – here is the Dockerfile

utterstep avatar Apr 09 '23 18:04 utterstep

One potential issue I see is that in planner you are working in WORKDIR /app, but in builder you are in WORKDIR /.

otahirs avatar Apr 09 '23 21:04 otahirs

I'm closing this issue, because tracking features will be moved to GitHub discussions. Github issues will be only for bugs.

Version 4 is in full focus. v3 related issues will be closed, if it is not a critical bug.

Thanks for your understanding.

andrasbacsai avatar Sep 08 '23 08:09 andrasbacsai