|
| 1 | +# buildx transparent support |
| 2 | + |
| 3 | +* Author(s): Mariano Reingart (@reingart) |
| 4 | +* Design Shepherd: \<skaffold-core-team-member\> |
| 5 | +* Date: 2025-02-02 |
| 6 | +* Status: Draft |
| 7 | + |
| 8 | +## Objectives |
| 9 | + |
| 10 | +Transparent local and remote container builds via buildx, a Docker CLI plugin for extended capabilities with BuildKit. |
| 11 | + |
| 12 | +## Background |
| 13 | + |
| 14 | +[buildx](https://docs.docker.com/reference/cli/docker/buildx/) is an enhanced container builder using BuildKit that can replace traditional |
| 15 | +`docker build` command, with almost the same syntax (transparently). |
| 16 | + |
| 17 | +This adds several distributed features and advanced capabilities: |
| 18 | +* local & remote build-kit builders, either running standalone, in docker containers or Kubernetes clusters |
| 19 | +* improved cache support (registry destination, pushing full metadatada and multi-stage layers) |
| 20 | +* improved multi-platform image building support (eg. x86_64 and arm64 combined, with emulation or cross-compilation) |
| 21 | + |
| 22 | +This features are very useful for corporate CI/CD, for example when using GitLab, where the use case requires: |
| 23 | +* using ephemeral remote buildkit instances (rootless) |
| 24 | +* using remote docker registries for cache, with multi-stage and multi-platform images support |
| 25 | +* using a different cache destination tag for flexibility and workflows separation |
| 26 | + |
| 27 | +The remote BuildKit [rootless](https://github.com/moby/buildkit/blob/master/docs/rootless.md) support is useful in cases |
| 28 | +where a privileged docker daemonis not possible or desirable due security policies (eg. untrusted images or review pipelines). |
| 29 | +Daemon-less mode is also useful to offload container building from developers notebooks, sharing and reusing remote caches more effectively. |
| 30 | + |
| 31 | +The buildx command also supports exporting the build cache using `--cache-to`, useful for remote shared caches in distributed use cases. |
| 32 | +Beside speed-ups thanks to caching improvements for metadata and multi-stage layers, this could allow different branches |
| 33 | +to have different cache destinations (production vs development cache, with different permissions). |
| 34 | + |
| 35 | +Multi-platform combined image builds are directly supported by buildx, including improved caching of common layers and emulation / cross-compilation. |
| 36 | +This could simplify pipelines and provide faster builds of complex codebases. |
| 37 | + |
| 38 | +References: |
| 39 | + |
| 40 | +* https://www.docker.com/blog/image-rebase-and-improved-remote-cache-support-in-new-buildkit/ |
| 41 | +* https://www.docker.com/blog/faster-multi-platform-builds-dockerfile-cross-compilation-guide/ |
| 42 | + |
| 43 | +## Proposal |
| 44 | + |
| 45 | +This proposal aims to improve Skaffold's build process by adding transparent support for docker buildx. |
| 46 | +The primary goal is to enable users to leverage the advanced features of buildx, such as remote buildkit builders and improved caching, without significant changes to their existing Skaffold configurations. |
| 47 | + |
| 48 | +New Global Configs: |
| 49 | + |
| 50 | +* `buildx-builder`: Enables automatic detection of buildx and multiple builder support. |
| 51 | +* `cache-tag`: Allows overriding the default cache tagging strategy, useful for managing caches across different branches or environments. |
| 52 | + |
| 53 | +Skaffold Schema changes: |
| 54 | + |
| 55 | +* `cacheTo` to specify custom cache destinations, adding `--cache-to` support to the Docker CLI build process when using buildx. |
| 56 | + |
| 57 | +Build Process Enhancements: |
| 58 | + |
| 59 | +* The build process now intelligently detects and utilizes buildx if available and configured. |
| 60 | +* Reduced dependency on the local Docker daemon when using buildx, enhancing security and flexibility. |
| 61 | + |
| 62 | +Backward Compatibility: |
| 63 | + |
| 64 | +All changes are designed to be backward compatible. |
| 65 | +If buildx is not detected or used, Skaffold will fall back to the traditional Docker builder. |
| 66 | + |
| 67 | +This cache behavior is intended to provide transparent user experience, with similar functionality compared to traditional local docker builds, |
| 68 | +without additional boilerplate or different configuration / patches for a CI workflow. |
| 69 | + |
| 70 | +## Design approach |
| 71 | + |
| 72 | +`docker buildx build` can be configured to execute against a [remote buildkit instance](https://docs.docker.com/build/builders/drivers/remote/). |
| 73 | +No docker daemon is necessary for this to work, but also that is supported by default using the [docker driver](https://docs.docker.com/build/builders/drivers/docker/). |
| 74 | +Additionally, [docker container driver](https://docs.docker.com/build/builders/drivers/docker-container) |
| 75 | +or [kubernetes driver](https://docs.docker.com/build/builders/drivers/kubernetes/) are available too for advanced use cases. |
| 76 | + |
| 77 | +Since `docker build` and `docker buildx build` essentially share the same options, no major modifications are needed to Skaffold for backward compatibility. |
| 78 | + |
| 79 | +This proposal implements the logic to detect if buildx is the default builder (looking for an alias in the docker config). |
| 80 | +To [set buildx as the default builder](https://github.com/docker/buildx?tab=readme-ov-file#set-buildx-as-the-default-builder), the command `docker buildx install` should be used. |
| 81 | + |
| 82 | +Then, additional buildx features are availables, like multi-platform support and different cache destinations. |
| 83 | + |
| 84 | +Cache adjustment is extended via the the `cache-tag`, useful to point to latest or a generic cache tag, instead of the generated one for this build |
| 85 | +(via `tagPolicy` that would be useless in most distributed cases, as it can be invalidated by minor changes, specially if using `inputDigest`). |
| 86 | + |
| 87 | +Multi-platform images now can be built nativelly, without multiplexing the pipeline nor additional steps to stich the different images. |
| 88 | + |
| 89 | +### User experience |
| 90 | + |
| 91 | +To use BuildKit transparently, BuildX should be installed as default builder (this creates an alias in docker config): |
| 92 | + |
| 93 | +``` |
| 94 | +docker buildx install |
| 95 | +``` |
| 96 | + |
| 97 | +Then Skaffold should be configured to detect buildx (default builder) and set a generic cache tag: |
| 98 | + |
| 99 | +``` |
| 100 | +skaffold config set -g buildx-builder default |
| 101 | +skaffold config set -g cache-tag cache |
| 102 | +``` |
| 103 | + |
| 104 | +Example basic config, this will be sufficient for many users: |
| 105 | + |
| 106 | +```yaml |
| 107 | +apiVersion: skaffold/v4beta13 |
| 108 | +build: |
| 109 | + artifacts: |
| 110 | + - image: my-app-image |
| 111 | + context: my-app-image |
| 112 | + docker: |
| 113 | + dockerfile: Dockerfile |
| 114 | + cacheFrom: |
| 115 | + - "my-app-image" |
| 116 | + cacheTo: |
| 117 | + - "my-app-image" |
| 118 | + local: |
| 119 | + useBuildkit: true |
| 120 | + useDockerCLI: true |
| 121 | + tryImportMissing: true |
| 122 | +``` |
| 123 | +
|
| 124 | +* If no tag is specified for cache, the configured `cache-tag` will be used (in this example my-app-image:cache) |
| 125 | +* If `cacheTo` destination is not specified, the `cacheFrom` image name and tag will be adjusted, adding `type=registry,mode=max` (only if push images is enabled) |
| 126 | + |
| 127 | +Advanced users would prefer to create buildkit instances for multiplatform images, e.g. using the docker container driver: |
| 128 | + |
| 129 | +``` |
| 130 | +docker buildx create --driver docker-container --name local |
| 131 | +skaffold config set -g buildx-builder local |
| 132 | +``` |
| 133 | + |
| 134 | +Remote builds are possible, pointing to a remote instance that could be deployed in another host or container: |
| 135 | +``` |
| 136 | +docker buildx create --name remote --driver remote tcp://buildkitd:2375 |
| 137 | +skaffold config set -g buildx-builder remote |
| 138 | +``` |
| 139 | + |
| 140 | +### Errors |
| 141 | + |
| 142 | +Example for missing builder (`ERROR: no builder "defaultx" found` if skaffold was configured incorrectly): |
| 143 | +``` |
| 144 | +skaffold build --default-repo localhost:5000 --platform linux/arm64,linux/amd64 --cache-artifacts=false --detect-minikube=false --push |
| 145 | +Generating tags... |
| 146 | + - my-app -> localhost:5000/my-app:v2.11.1-121-gc703038c9-dirty |
| 147 | +Starting build... |
| 148 | +Building [my-app]... |
| 149 | +Target platforms: [linux/arm64,linux/amd64] |
| 150 | +ERROR: no builder "defaultx" found |
| 151 | +exit status 1. Docker build ran into internal error. Please retry. |
| 152 | +If this keeps happening, please open an issue.. |
| 153 | +``` |
| 154 | + |
| 155 | +Example of improper configuration for multi-platform emulation builds: |
| 156 | + |
| 157 | +``` |
| 158 | +$ /src/out/skaffold build --default-repo localhost:5000 --platform linux/arm64,linux/amd64 --cache-artifacts=false --detect-minikube=false --push |
| 159 | +Generating tags... |
| 160 | + - my-app -> localhost:5000/my-app:v2.11.1-122-ga0fb3239a-dirty |
| 161 | +Starting build... |
| 162 | +Building [my-app]... |
| 163 | +Target platforms: [linux/arm64,linux/amd64] |
| 164 | +#0 building with "default" instance using docker driver |
| 165 | +
|
| 166 | +... |
| 167 | +
|
| 168 | +#19 [linux/arm64 builder 3/3] RUN go build -o /app main.go |
| 169 | +#19 0.639 exec /bin/sh: exec format error |
| 170 | +#19 ERROR: process "/bin/sh -c go build -o /app main.go" did not complete successfully: exit code: 1 |
| 171 | +------ |
| 172 | + > [linux/arm64 builder 3/3] RUN go build -o /app main.go: |
| 173 | +0.639 exec /bin/sh: exec format error |
| 174 | +------ |
| 175 | +Dockerfile:6 |
| 176 | +-------------------- |
| 177 | + 4 | |
| 178 | + 5 | COPY main.go . |
| 179 | + 6 | >>> RUN go build -o /app main.go |
| 180 | + 7 | |
| 181 | + 8 | FROM alpine:3 |
| 182 | +-------------------- |
| 183 | +ERROR: failed to solve: process "/bin/sh -c go build -o /app main.go" did not complete successfully: exit code: 1 |
| 184 | +running build: exit status 1. To run cross-platform builds, use a proper buildx builder. To create and select it, run: |
| 185 | +
|
| 186 | + docker buildx create --driver docker-container --name buildkit |
| 187 | +
|
| 188 | + skaffold config set buildx-builder buildkit |
| 189 | +
|
| 190 | +For more details, see https://docs.docker.com/build/building/multi-platform/. |
| 191 | +
|
| 192 | +``` |
| 193 | + |
| 194 | +This is a corner case, as the default docker builder should not be used for multi-platform builds. |
| 195 | +An actionable error was returned with the step-by-step instructions. |
| 196 | + |
| 197 | +## Implementation plan |
| 198 | + |
| 199 | +For the minimal viable product (initial PR): |
| 200 | + |
| 201 | +* Add a new global config buildx-builder to enable buildx detection and support for different builders |
| 202 | +* Implements logic to detect buildx is the default builder (via docker config alias) |
| 203 | +* Remove dependency on docker daemon if using BuildX (still supported to load images if using minikube or similar) |
| 204 | +* Add a new global config cache-tag to override default cache tagging (instead of generated tag) |
| 205 | +* Add cacheTo to sakffold schema for the cache destination (optional, default is to use new cacheFrom + cache tag) |
| 206 | +* Add --cache-to support in Docker CLI build if using BuildX CLI |
| 207 | +* Add multiplatform images building support for buildkit under buildx |
| 208 | + |
| 209 | +Additional features, error handling and examples can be implemented in the future. |
| 210 | + |
| 211 | +Future work: include more BuildKit advanced features support like |
| 212 | +[multiple build contexts](https://www.docker.com/blog/dockerfiles-now-support-multiple-build-contexts/), |
| 213 | +for an improved mono-repo experience for large code-bases. |
| 214 | + |
| 215 | +Other advanced featues of buildx and buildkit includes [Attestations](https://docs.docker.com/build/metadata/attestations/). |
| 216 | +Note: default attestation is disabled for multiplatform builds, as it creates metadata with "unknown/unknown" arch/OS, |
| 217 | +causing issues with registry tools an libraries, see [GH discussion](https://github.com/orgs/community/discussions/45969). |
| 218 | + |
| 219 | +## Release plan |
| 220 | + |
| 221 | +The buildx support could go through the release stages Alpha -> Beta -> Stable. |
| 222 | +This will allow time for community feedback and avoid unnecessary features or rework. |
| 223 | + |
| 224 | +The following features would be released at each stage: |
| 225 | + |
| 226 | +**Alpha** |
| 227 | + |
| 228 | +Implement minimal changes to support buildx (initial PR): |
| 229 | +- buildx detection |
| 230 | +- cache-to export |
| 231 | +- multiplatform images |
| 232 | + |
| 233 | +**Beta** |
| 234 | + |
| 235 | +- Implement additional buildx features needed by the community, like multiple context support |
| 236 | +- Implement additional actionable errors, if needed |
| 237 | +- Update user-facing documentation |
| 238 | +- Implement a new buildkit basic example using buildx |
| 239 | + |
| 240 | +**Stable** |
| 241 | + |
| 242 | +- Remove custom buildkit example using custom builder |
| 243 | +- Implement a new buildkit advanced example using buildx and remote rootless daemon for CI |
| 244 | + |
| 245 | +## Automated test plan |
| 246 | + |
| 247 | +New test cases were implemented to cover the new functionality (added to existing test suites): |
| 248 | + |
| 249 | +1. Unit tests for buildx, similar to docker build. |
| 250 | + |
| 251 | + * `TestDockerCLIBuild`: "buildkit buildx load", "buildkit buildx push" (including both buildx detection and cache-to) |
| 252 | + |
| 253 | +2. Integration tests, idem: |
| 254 | + |
| 255 | + * `TestBuild`: "docker buildx" |
| 256 | + * `TestBuildWithWithPlatform`: "docker buildx linux/amd64", "docker buildx linux/arm64" |
| 257 | + * `TestBuildWithMultiPlatforms`: "build multiplatform images with buildx" |
| 258 | + |
| 259 | +3. Add basic and comprehensive buildx examples to the `integration/examples` |
| 260 | + directory. |
| 261 | + |
| 262 | +Note that for multi-platform images and advanced examples, a registry is needed to push the images. |
| 263 | +A docker container with a local registry was implemented in the setup of integration tests. |
| 264 | +With this approach, all buildx tests can be run locally (not needing GCP nor any other cloud resource like a remote registry). |
| 265 | +This avoids modifications to docker daemon config (like insecure-registry exclusions), but it needs a buildkit running within the host network, as both uses localhost. |
| 266 | + |
| 267 | +## Credits |
| 268 | + |
| 269 | +This proposal is related to [#8172](https://github.com/GoogleContainerTools/skaffold/pull/8172): "Add buildx option for daemon-less BuildKit support". |
| 270 | +Initial code was inspired by by a prior [ebekebe fork](https://github.com/ebekebe/skaffold/commit/1c1fdeb18f4d2847e65e283fba498a14745039af). |
| 271 | + |
| 272 | +A more general approach was implemented in this initial PR [#9648](https://github.com/GoogleContainerTools/skaffold/pull/9648), |
| 273 | +with configurable builders, actionable errors and multi-platform support. |
| 274 | + |
| 275 | +This actually fixes existing issues like: |
| 276 | +* [#5018](https://github.com/GoogleContainerTools/skaffold/issues/5018): "Docker Buildx Integration" |
| 277 | +* [#6732](https://github.com/GoogleContainerTools/skaffold/issues/6732): "Support building securely against remote buildkitd" |
| 278 | +* [#9197](https://github.com/GoogleContainerTools/skaffold/issues/9197): "Support additional docker buildkit options" |
| 279 | + |
| 280 | +The fix proposed here uses buildx nativelly, avoiding custom build scripts, like the one in the official example: |
| 281 | +[custom-buildx](https://github.com/GoogleContainerTools/skaffold/tree/main/examples/custom-buildx) |
| 282 | + |
| 283 | +Future work is related to [#2110](https://github.com/GoogleContainerTools/skaffold/issues/2110): "feature request: more control over the docker build context" |
| 284 | + |
0 commit comments