Skip to content

Commit

Permalink
Add platform flag to build (#217)
Browse files Browse the repository at this point in the history
  • Loading branch information
hlxid authored and jessfraz committed Apr 4, 2019
1 parent 97f0060 commit 013bb21
Show file tree
Hide file tree
Showing 19 changed files with 307 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ Flags:
--label Set metadata for an image (default: [])
--no-cache Do not use cache when building the image (default: false)
--no-console Use non-console progress UI (default: false)
--platform Set platforms for which the image should be built (default: <yourPlatform>)
-s, --state directory to hold the global state (default: /home/user/.local/share/img)
-t, --tag Name and optionally a tag in the 'name:tag' format (default: [])
--target Set the target build stage to build (default: <none>)
Expand Down
9 changes: 9 additions & 0 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"errors"
"flag"
"fmt"
"github.com/containerd/containerd/platforms"
"io"
"io/ioutil"
"os"
Expand Down Expand Up @@ -44,6 +45,7 @@ func (cmd *buildCommand) Register(fs *flag.FlagSet) {
fs.Var(&cmd.tags, "tag", "Name and optionally a tag in the 'name:tag' format")
fs.Var(&cmd.tags, "t", "Name and optionally a tag in the 'name:tag' format")
fs.StringVar(&cmd.target, "target", "", "Set the target build stage to build")
fs.Var(&cmd.platforms, "platform", "Set platforms for which the image should be built")
fs.Var(&cmd.buildArgs, "build-arg", "Set build-time variables")
fs.Var(&cmd.labels, "label", "Set metadata for an image")
fs.BoolVar(&cmd.noConsole, "no-console", false, "Use non-console progress UI")
Expand All @@ -56,6 +58,7 @@ type buildCommand struct {
labels stringSlice
target string
tags stringSlice
platforms stringSlice

contextDir string
noConsole bool
Expand Down Expand Up @@ -123,6 +126,11 @@ func (cmd *buildCommand) Run(ctx context.Context, args []string) (err error) {
}
}

if len(cmd.platforms) < 1 {
cmd.platforms = []string{platforms.DefaultString()}
}
platforms := strings.Join(cmd.platforms, ",")

// Create the client.
c, err := client.New(stateDir, backend, cmd.getLocalDirs())
if err != nil {
Expand All @@ -135,6 +143,7 @@ func (cmd *buildCommand) Run(ctx context.Context, args []string) (err error) {
// We use the base for filename here because we already set up the local dirs which sets the path in createController.
"filename": filepath.Base(cmd.dockerfilePath),
"target": cmd.target,
"platform": platforms,
}
if cmd.noCache {
frontendAttrs["no-cache"] = ""
Expand Down
13 changes: 13 additions & 0 deletions build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,16 @@ func TestBuildMultipleTags(t *testing.T) {
t.FailNow()
}
}

func TestBuildMultiplePlatforms(t *testing.T) {
args := []string{"build", "--platform", "amd64", "--platform", "linux/arm64,linux/arm/v7", "-t", "testbuildplatforms", "-"}

_, err := doRun(args, withDockerfile(`
FROM alpine
`))

if err != nil {
t.Logf("img %v failed unexpectedly: %v", args, err)
t.FailNow()
}
}
12 changes: 11 additions & 1 deletion client/workeropt.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package client
import (
"context"
"fmt"
"github.com/moby/buildkit/util/binfmt_misc"
"os/exec"
"path/filepath"
"syscall"
Expand Down Expand Up @@ -118,6 +119,15 @@ func (c *Client) createWorkerOpt(withExecutor bool) (opt base.WorkerOpt, err err

xlabels := base.Labels("oci", c.backend)

var supportedPlatforms []specs.Platform
for _, p := range binfmt_misc.SupportedPlatforms() {
parsed, err := platforms.Parse(p)
if err != nil {
return opt, err
}
supportedPlatforms = append(supportedPlatforms, platforms.Normalize(parsed))
}

opt = base.WorkerOpt{
ID: id,
Labels: xlabels,
Expand All @@ -128,7 +138,7 @@ func (c *Client) createWorkerOpt(withExecutor bool) (opt base.WorkerOpt, err err
Applier: apply.NewFileSystemApplier(contentStore),
Differ: walking.NewWalkingDiff(contentStore),
ImageStore: imageStore,
Platforms: []specs.Platform{platforms.Normalize(platforms.DefaultSpec())},
Platforms: supportedPlatforms,
ResolveOptionsFunc: resolver.NewResolveOptionsFunc(nil),
}

Expand Down
32 changes: 32 additions & 0 deletions vendor/github.com/moby/buildkit/util/binfmt_misc/Dockerfile

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions vendor/github.com/moby/buildkit/util/binfmt_misc/Makefile

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions vendor/github.com/moby/buildkit/util/binfmt_misc/arm_check.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions vendor/github.com/moby/buildkit/util/binfmt_misc/check.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions vendor/github.com/moby/buildkit/util/binfmt_misc/detect.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

94 changes: 94 additions & 0 deletions vendor/github.com/moby/buildkit/util/binfmt_misc/generate.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 013bb21

Please sign in to comment.