You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Breakout of #4021.
Also see overall Dockerfile analysis for context.
Refresher:
we clone too much in size (full history), while we do not need it - github clones for a cold-cache run account for about 1G of content
we clone too often - we should only ever clone any given project once per run
Describe the solution you'd like
Suggesting that:
we git clone --quiet (less noise on the logs)
we git clone --depth 1 (reduce network traffic and cache size)
we separate clone and build - one clone when building multi-arch, and use mounts for build to avoid unnecessary copies (reduce network traffic and cache size)
Note that if we want to keep building using other projects build scripts, we would have to bind mounts RW (as most of these scripts do not allow to build out of tree). I personally believe we should build directly ourselves in most cases instead of relying on Makefiles / scripts (see #4021 for reasons).
If we do not want that, or not feasible in certain cases, we just need a simple lock to prevent cross-platforms builds from conflicting: https://github.com/farcloser/lepton/blob/main/Dockerfile#L753-L755
Let me know if that plan (1, 2, 3) is fine or if you prefer something else.
it makes no difference wrt caching (as compared with a regular git clone) - same size, same behavior with changing version ARG for the project being cloned
what it does (as far as our use case is concerned), is strictly equivalent in result to git clone --depth 1 --branch BRANCH or git clone --depth 1 --revision REV.
On the other hand:
we can use it without having to install git inside the image (* note: unless we would need it for go mod)
git --revision is only for recent git which is not available yet in ubuntu / debian - which means we have to do git clone --depth 1 --branch BRANCH AND git rev-parse HEAD instead of just the ADD
we can probably do away with the git directory itself, which means saving a few megabytes on cache size
I don't quite care either way. Both approach work IMHO.
I have a slight preference for the "manual" way because of two reasons:
I find it easier to reason about things when they are explicit / immediately available to review (as the "buildkit" way does hide things away and forces you to go read their source to understand what happens) - but I appreciate that the "buildkit" way is slightly more compact and saves installing git inside the container.
it is less entrenched into the specific context of the Dockerfile - if/when we want to move some operations out of the Dockerfile, it is much easier to do without the buildkit-ism
Pro:
a. smallest change possible
b. keeps a separate /git-checkout-tag-with-hash.sh for other projects to re-use
Cons:
a. /git-checkout-tag-with-hash.sh will do a useless git checkout
b. adds an extra file
c. adds an extra layer
d. some intelligence is moved out of the Dockerfile into the script, making for a (slightly) less intuitive reading / debugging
plan B
Same as plan A, but addresses the layer concern (c.)
With some cons:
a. a bit more complex to grok visually, as most RUN commands are less legible than a proper shell script
b. less debugging info / messages in its current form
plan D
Move the (optional) git clone part to the script.
The script could be smart and figure out if it is already inside a clone or not, and decide to clone if not.
ARG VERSION=version@revision
ARG REPO=blablblabl
RUN --mount=target=git-checkout-tag-with-hash.sh,source=./hack/git-checkout-tag-with-hash.sh,type=bind
./git-checkout-tag-with-hash.sh "$VERSION""$REPO"
With script being updated as described.
LMK which plan (A, B, C, D) is preferred and I'll send a PR.
FWIW while you said they were equivalent, ADD should be able to perform a clone without keeping the .git dir if you don't need to interact with it (which is assumed given you discussed not needing git installed as a perk).
I contributed a related change to the testssl.sh project, although one of my gripes was a lack of .dockerignore support (which applies if you git clone via RUN too), for testssl.sh it was a non-issue since you could just clone the git repo and COPY that over instead (with the perks of .dockerignore). In the case of nerdctl you could do the same if you were tracking external repos via submodules (I haven't checked), none of which is relevant to image build caching of layers really 🤷♂️
You could also have leveraged cache mounts in your RUN instructions, but for CI use that does add some extra complexity to actually caching (should you need that, but the benefit of CI caching cloned repos is unlikely to be worthwhile vs fresh clones anyway).
Activity
apostasie commentedon Mar 20, 2025
(from #4017 (comment))
apostasie commentedon Mar 20, 2025
After some testing:
git clone --depth 1 --branch BRANCHorgit clone --depth 1 --revision REV.On the other hand:
git clone --depth 1 --branch BRANCHANDgit rev-parse HEADinstead of just the ADDI don't quite care either way. Both approach work IMHO.
I have a slight preference for the "manual" way because of two reasons:
Option 1, the manual way:
Option 2, the buildkit way:
Note that the buildkit way MAY likely means we have to split out commit shas from the VERSION variable.
Also note this is pseudo-code, not a proper patch - does not account for situations where the revision is empty, etc
Let me know which way you prefer @AkihiroSuda
AkihiroSuda commentedon Mar 20, 2025
Thanks for testing, the Option 1 seems fine for now, but eventually we should extend BuildKit to support
TAG@HASHformAkihiroSuda commentedon Mar 20, 2025
https://github.com/containerd/nerdctl/blob/v2.0.4/hack/git-checkout-tag-with-hash.sh is expected to be reusable in other projects that may run
git cloneoutside this script, so the script should not rungit cloneby default, but it may have an optional flags like--clone=$REPO --shallowapostasie commentedon Mar 20, 2025
Ok, so, integrating the script part:
Pro:
a. smallest change possible
b. keeps a separate
/git-checkout-tag-with-hash.shfor other projects to re-useCons:
a.
/git-checkout-tag-with-hash.shwill do a uselessgit checkoutb. adds an extra file
c. adds an extra layer
d. some intelligence is moved out of the Dockerfile into the script, making for a (slightly) less intuitive reading / debugging
Same as plan A, but addresses the layer concern (c.)
Do away with the script.
Pro:
a. removes the extraneous
git checkoutb. everything needed to read/debug in one place
c. removes extra file
With some cons:
a. a bit more complex to grok visually, as most RUN commands are less legible than a proper shell script
b. less debugging info / messages in its current form
Move the (optional)
git clonepart to the script.The script could be smart and figure out if it is already inside a clone or not, and decide to clone if not.
With script being updated as described.
LMK which plan (A, B, C, D) is preferred and I'll send a PR.
AkihiroSuda commentedon Mar 20, 2025
Thanks, I'd prefer the plan A.
The script can be just
ADD-ed to/usr/local/binin the base stage.An alternative would be plan E:
(No need to be smart as in plan D)
polarathene commentedon Nov 6, 2025
FWIW while you said they were equivalent,
ADDshould be able to perform a clone without keeping the.gitdir if you don't need to interact with it (which is assumed given you discussed not needinggitinstalled as a perk).I contributed a related change to the
testssl.shproject, although one of my gripes was a lack of.dockerignoresupport (which applies if yougit cloneviaRUNtoo), fortestssl.shit was a non-issue since you could just clone the git repo andCOPYthat over instead (with the perks of.dockerignore). In the case ofnerdctlyou could do the same if you were tracking external repos via submodules (I haven't checked), none of which is relevant to image build caching of layers really 🤷♂️You could also have leveraged cache mounts in your
RUNinstructions, but for CI use that does add some extra complexity to actually caching (should you need that, but the benefit of CI caching cloned repos is unlikely to be worthwhile vs fresh clones anyway).