Skip to content

Commit 5482483

Browse files
committed
feat(builder): use docker-buildx plugin to build for non-native platforms
1 parent a970bf5 commit 5482483

File tree

1,115 files changed

+102365
-66496
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,115 files changed

+102365
-66496
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,30 @@ The `--dockerfile` option makes it possible to build a new minified image direct
537537

538538
The `--use-local-mounts` option is used to choose how the `docker-slim` sensor is added to the target container and how the sensor artifacts are delivered back to the master. If you enable this option you'll get the original `docker-slim` behavior where it uses local file system volume mounts to add the sensor executable and to extract the artifacts from the target container. This option doesn't always work as expected in the dockerized environment where `docker-slim` itself is running in a Docker container. When this option is disabled (default behavior) then a separate Docker volume is used to mount the sensor and the sensor artifacts are explicitly copied from the target container.
539539

540+
#### `docker-buildx` plugin
541+
542+
You can build for multiple platforms using the `docker-buildx` plugin `build` command by setting `--use-buildx=true`.
543+
This plugin is automatically installed when installing newer versions of docker-ce.
544+
545+
A subset of plugin flags are supported, eaching being prefixed with `--buildx-`.
546+
547+
As an example, here we start a local registry, create a multi-platform builder,
548+
then run `docker-slim build` to build a manifest list for `linux/arm64` and `linux/amd64` platforms
549+
and push the list to the local registry:
550+
551+
```console
552+
$ docker run -d -p 5000:5000 --name test-registry registry:2
553+
$ docker buildx create --use --driver-opt network=host --bootstrap --driver docker-container --name mybuilder
554+
$ docker-slim build --use-buildx=true --buildx-platforms linux/arm64,linux/amd64 --buildx-push=true --pull --tag localhost:5000/foo/nginx:slim nginx:latest
555+
cmd=build info=param.http.probe message='using default probe'
556+
cmd=build state=started
557+
...
558+
cmd=build state=building message="building optimized image" builder=buildx platforms=linux/arm64,linux/amd64
559+
...
560+
```
561+
562+
Read more about installation and configuration [here](https://docs.docker.com/buildx/working-with-buildx/).
563+
540564
## RUNNING CONTAINERIZED
541565

542566
The current version of `docker-slim` is able to run in containers. It will try to detect if it's running in a containerized environment, but you can also tell `docker-slim` explicitly using the `--in-container` global flag.

go.mod

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ require (
1818
github.com/docker-slim/go-update v0.0.0-20190422071557-ed40247aff59
1919
github.com/docker-slim/uilive v0.0.2 // indirect
2020
github.com/docker-slim/uiprogress v0.0.0-20190505193231-9d4396e6d40b
21+
github.com/docker/cli v20.10.12+incompatible
2122
github.com/docker/docker v20.10.12+incompatible
2223
github.com/docker/go-connections v0.4.0
2324
github.com/dsnet/compress v0.0.1 // indirect
2425
github.com/dustin/go-humanize v1.0.0
2526
github.com/fatih/color v1.13.0
2627
github.com/fsouza/go-dockerclient v1.7.4
28+
github.com/fvbommel/sortorder v1.0.2 // indirect
2729
github.com/getkin/kin-openapi v0.19.0
2830
github.com/ghodss/yaml v1.0.0
2931
github.com/gocolly/colly/v2 v2.0.1
@@ -38,13 +40,18 @@ require (
3840
github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6
3941
github.com/pkg/errors v0.9.1
4042
github.com/pkg/term v0.0.0-20200520122047-c3ffed290a03 // indirect
41-
github.com/robertkrimen/otto v0.0.0-20211024170158-b87d35c0b86f
4243
github.com/sirupsen/logrus v1.8.1
4344
github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635
45+
github.com/theupdateframework/notary v0.7.0 // indirect
4446
github.com/ulikunitz/xz v0.5.7 // indirect
4547
github.com/urfave/cli/v2 v2.3.0
4648
golang.org/x/net v0.0.0-20211216030914-fe4d6282115f
4749
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e
4850
)
4951

5052
replace github.com/compose-spec/compose-go => ./pkg/third_party/compose-go
53+
54+
replace (
55+
github.com/docker/cli => github.com/docker/cli v20.10.3-0.20220408161037-b68db383d389+incompatible
56+
github.com/docker/docker => github.com/docker/docker v20.10.3-0.20220414164044-61404de7df1a+incompatible
57+
)

go.sum

Lines changed: 101 additions & 16 deletions
Large diffs are not rendered by default.

pkg/app/master/builder/builder.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package builder
2+
3+
import (
4+
"context"
5+
"path/filepath"
6+
7+
"github.com/docker-slim/docker-slim/pkg/util/fsutil"
8+
)
9+
10+
// ImageBuilder builds an image and returns some data from the build.
11+
type ImageBuilder interface {
12+
// Build the image.
13+
Build(context.Context) error
14+
// GetLogs from the image builder.
15+
GetLogs() string
16+
// HasData file/dir that got ADD'd or COPY'd into the image, respectively.
17+
HasData() bool
18+
}
19+
20+
const (
21+
tarData = "files.tar"
22+
dirData = "files"
23+
)
24+
25+
// getDataName returns a predetermined name if it exists within the root dir.
26+
func getDataName(root string) string {
27+
28+
dataTar := filepath.Join(root, tarData)
29+
dataDir := filepath.Join(root, dirData)
30+
if fsutil.IsRegularFile(dataTar) {
31+
return tarData
32+
} else if fsutil.IsDir(dataDir) {
33+
return dirData
34+
}
35+
36+
return ""
37+
}

0 commit comments

Comments
 (0)