-
Notifications
You must be signed in to change notification settings - Fork 225
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proposal: expose docker cache-from capability #940
Comments
Thanks for suggesting this feature and for providing the examples too. It's on our list of things to review / prioritise. |
@alexellis , this is something I would be willing to work on and support, so if there is a preferred design or an alternative, just let me know. |
For the sake of a specific implementation, we use a command that looks like this:
|
@alexellis After discussing the proposal more in the community calls, we decided it would be good to try and simplify the yaml/flags a little bit so that the most common usecases do not require extensive configuration. I present two options below. Initial simplificationFirst, to simplify the configuration, if
Second, the yaml and cli will support two options to control the cache In general, if the value is a string with no Also, simplify the configuration, we assume that users of the cache This presents several configuation combinations. Below i outline each configuration, what it means, and the equivalent
version: 1.0
provider:
name: openfaas
gateway: http://127.0.0.1:8080
functions:
my-function:
lang: python3-flask-debian
handler: ./my-function
image: ghcr.io/lucasroesler/my-function:latest
build_cache:
from:
- ghcr.io/lucasroesler/my-function:latest This is the simplest cache option, the cache metadata is stored with the output image. This is equivalent to DOCKER_BUILDKIT=1 docker build \
--cache-from ghcr.io/lucasroesler/my-function:latest \
--build-arg BUILDKIT_INLINE_CACHE=1 \
-t ghcr.io/lucasroesler/my-function:latest \
.
version: 1.0
provider:
name: openfaas
gateway: http://127.0.0.1:8080
functions:
my-function:
lang: python3-flask-debian
handler: ./my-function
image: ghcr.io/lucasroesler/my-function:latest
build_cache:
from:
- ghcr.io/lucasroesler/my-function:buildcache
to:
- ghcr.io/lucasroesler/my-function:buildcache In this configuration we assume that the DOCKER_BUILDKIT=1 docker build \
--cache-from type=registry,ref=ghcr.io/lucasroesler/my-function:buildcache \
--cache-to type=registry,ref=ghcr.io/lucasroesler/my-function:buildcache,mode=max \
--build-arg BUILDKIT_INLINE_CACHE=1 \
-t ghcr.io/lucasroesler/my-function:latest \
.
version: 1.0
provider:
name: openfaas
gateway: http://127.0.0.1:8080
functions:
my-function:
lang: python3-flask-debian
handler: ./my-function
image: ghcr.io/lucasroesler/my-function:latest
build_cache:
from:
- ghcr.io/lucasroesler/my-function:buildcache
to:
- ref=ghcr.io/lucasroesler/my-function:buildcache,mode=max In this configuration we assume that the DOCKER_BUILDKIT=1 docker build \
--cache-from type=registry,ref=ghcr.io/lucasroesler/my-function:buildcache \
--cache-to type=registry,ref=ghcr.io/lucasroesler/my-function:buildcache,mode=max \
--build-arg BUILDKIT_INLINE_CACHE=1 \
-t ghcr.io/lucasroesler/my-function:latest \
.
version: 1.0
provider:
name: openfaas
gateway: http://127.0.0.1:8080
functions:
my-function:
lang: python3-flask-debian
handler: ./my-function
image: ghcr.io/lucasroesler/my-function:latest
build_cache:
from:
- type=gha
to:
- type=gha,mode=max In this configuration we simply pass the parameters as is DOCKER_BUILDKIT=1 docker build \
--cache-from type=gha \
--cache-to type=gha,mode=max \
--build-arg BUILDKIT_INLINE_CACHE=1 \
-t ghcr.io/lucasroesler/my-function:latest \
. Further simplificationI think that example (1) and (2) could potentiallly further simplified. I believe the two most common configurations will be (1) inline cache to the current image or (b) external max cache to a separate tag. To simplify these two cases we can accept a single string value for the
version: 1.0
provider:
name: openfaas
gateway: http://127.0.0.1:8080
functions:
my-function:
lang: python3-flask-debian
handler: ./my-function
image: ghcr.io/lucasroesler/my-function:latest
build_cache: inline This is equivalent to DOCKER_BUILDKIT=1 docker build \
--cache-from ghcr.io/lucasroesler/my-function:latest \
--build-arg BUILDKIT_INLINE_CACHE=1 \
-t ghcr.io/lucasroesler/my-function:latest \
.
version: 1.0
provider:
name: openfaas
gateway: http://127.0.0.1:8080
functions:
my-function:
lang: python3-flask-debian
handler: ./my-function
image: ghcr.io/lucasroesler/my-function:latest
build_cache: max In this configuration we assume that the DOCKER_BUILDKIT=1 docker build \
--cache-from type=registry,ref=ghcr.io/lucasroesler/my-function:buildcache \
--cache-to type=registry,ref=ghcr.io/lucasroesler/my-function:buildcache,mode=max \
--build-arg BUILDKIT_INLINE_CACHE=1 \
-t ghcr.io/lucasroesler/my-function:latest \
.
version: 1.0
provider:
name: openfaas
gateway: http://127.0.0.1:8080
functions:
my-function:
lang: python3-flask-debian
handler: ./my-function
image: ghcr.io/lucasroesler/my-function:latest
build_cache:
from:
- type=gha
to:
- type=gha,mode=max In this configuration we simply pass the parameters as is DOCKER_BUILDKIT=1 docker build \
--cache-from type=gha \
--cache-to type=gha,mode=max \
--build-arg BUILDKIT_INLINE_CACHE=1 \
-t ghcr.io/lucasroesler/my-function:latest \
. User experienceThere are a couple of errors that can occur and should probably be handled directly in the CLI. Docker buildxThe max and other advanced modes require When cache is enabled, we can either
Registry errorsNot all registries will support the cache images, in my experience, this is typically seen as a When cache mode is enabled and the builder returns an error (doesn't exit cleanly) then we should print a warning and a link to the docs about known supported/unsupported registries. For example GCR is does not support cache but Google Artifact Registry does. |
Expected Behaviour
When building a function using
faas-cli build
, it should be possible to reference an external source for the docker build/ayer cache, the most common case would be referencing a pervious build. When enabled, it would allow infrequently changed steps, for examplepip install
to be cached between builds and reduce the total build time.Current Behaviour
Only the local build cache can be used by
faas-cli build
. This is most noticeable in CI/CD workflows where the docker builder is often isolated and new between each build. For example, in Github Actions, this seems to be the case. Because the build cache starts empty, every layer of the function build must be rebuilt, even if only a small change was made at the end of the docker file. This is very noticable in Python and NodeJS projects where the final step is often just copying a small amount of function code, but there is often a slowpip install
ornpm -i
.Why do you need this?
This will improve build times in CI/CD environments.
Who is this for?
I work at Contiamo but the feature could benefit any function build
Are you a GitHub Sponsor (Yes/No?)
Check at: https://github.com/sponsors/openfaas
List All Possible Solutions and Workarounds
one possible solution is to add
docker pull
as a build step prior to runningfaas-cli build
. This would seed the docker cache with the relevant images. This has a disadvantage that it will always pull those images even if they could not be used in the cache. This requires time and uses network bandwidth that wasn't needed, actually making the build longer than if that step was skipped. In some of the other options we will see that it can be more efficient.Use the
--shrinkwrap
flag to prepare the build context and then usedocker build --cache-from
to pass references to the candidate images for the build cache.Allow passing a flag or set a yaml config for
faas-cli build
so that it can set the--cache-from
flag. This would allow theinline
cache mode when the user also passes theBUILDKIT_INLINE_CACHE=1
build arg.Allow passing arbitrary flags=value pairs to
faas-cli build
so that the developer can set the appropriate flag:--cache-from
, if usingdocker build
, or--cache-from
/--cache-to
, when usingdocker buildx
.Which Solution Do You Recommend?
I think either 3 or 4 are the best alternatives, option 3 is most focused on just this problem, but option 4 would be the most flexible.
For any solution, I think the feature shoudl be opt in, meaning the feature does not automatically enable additional caching by default. The developer must explicitly enable this additional caching behavior.
Option 3 experience / implementation
For option 3 I think we could implement this as just providing the
--cache-from
flag infaas-cli
and then adding a new section to the function spec.The DX would look like this
Alternatively, in the YAML it could look like this
In both configurations, the values should simply be passed to the
--cache-from
as is without modification. This will then allow usage of the advanced options whenbuildx
is explicitly enabled in the environment, for example,"type=local,src=path/to/dir"
It would be nice to also support the
cache-to
flag from buildx, but this flag is not supported by the default docker build and would cause an error. However, it allows for much more advanced caching options, such as storing the cache locally in a folder, in a blob storage, or in a registry. It also allows "max" mode, caching all of the build layers, including intermediate layers from multi-stage builds. This provides significantly more cache hit opportunities. If we want to allow this opportunity, but add the required documentation, I think it could look like thisor
Option 4 experience / implementation
Option 4 enables the same experience, but would look like this
faas-cli build -f stack.yaml --builder-flag "--cache-from=ghcr.io/lucasroesler/my-function:latest,ghcr.io/lucasroesler/my-other-function:latest" --build-arg BUILDKIT_INLINE_CACHE=1
and
Caching impact
inline caching
There are several styles and options of docker layer caching enabled by
docker
andbuildkit
, note that buildkit is required for this feature.The first and simplest is called
inline
caching. This adds some additional metadata to the image config to indicate that the layers can be reused in build caches. It is only some additional metadata in the docker manifest config and it requires that the image is built with thisinline
cache enabled. The result has no impact an the actual image or layer sizes because it is only additional metadata that is pushed to the remote registry.DOCKER_BUILDKIT=1 docker build --build-arg BUILDKIT_INLINE_CACHE=1 -t caching-test:with-cache .
I tested this with an image and the build size was the same with and without the inline cache. This can be tested with any docker image
During subsequent builds, the builder will download just this metadata to determine if a cache hit is possible and then, only when it is useful, download the actual layer data. The result has no impact an the actual image or layer sizes because it is only additional metadata that is pushed to the remote registry.
other caching modes
Note that there are two caching modes,
min
andmax
. The inline caching will usemin
mode, which is why it has no impact on the final size, it is just a tiny amount of metadata.With
max
mode all build layers, including ephemeral multi-stage build layers are saved. This clearly results in more data, but is not supported by theinline
cache type. Instead these layers can be exported to a local folder, a blob storage, or to a docker registry.To use these other destinations or the max mode, we would need to enable support for the
--cache-to
flag.Additional caching background
This feature is also implemented in the
docker-build-push
Github action, see here https://github.com/docker/build-push-action/blob/master/docs/advanced/cache.md. This could provide a good example for how to document the feature.Relevant docs about docker/buildkit caching:
Context
I have a python function that we build frequently because it bundles a machine learning model in the image. As a result, the last layer is just copying the machine learning model but all of the other layers (the dependencies and the function code) are not frequently changing.
In our CI/CD system (github actions) the build cache is always empty, which means our builds spend a lot of time on the
apt get
andpip install
stages even though these are not actually changing and would normally be skipped when built on my local laptop, where the build cache contains previous versions of the function.Your Environment
FaaS-CLI version ( Full output from:
faas-cli version
): 0.14.11Docker version ( Full output from:
docker version
):The text was updated successfully, but these errors were encountered: