-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This command mimics `docker build` CLI flags so that Docker users can more easily get started with BuildKit. Signed-off-by: Akihiro Suda <suda.akihiro@lab.ntt.co.jp>
- Loading branch information
1 parent
5fa37d1
commit 742f567
Showing
2 changed files
with
199 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/containerd/console" | ||
"github.com/moby/buildkit/client" | ||
"github.com/moby/buildkit/util/appcontext" | ||
"github.com/moby/buildkit/util/appdefaults" | ||
"github.com/moby/buildkit/util/progress/progressui" | ||
"github.com/pkg/errors" | ||
"github.com/urfave/cli" | ||
"golang.org/x/sync/errgroup" | ||
) | ||
|
||
func main() { | ||
app := cli.NewApp() | ||
app.Name = "build-using-dockerfile" | ||
app.UsageText = `build-using-dockerfile [OPTIONS] PATH | URL | -` | ||
app.Description = ` | ||
build using Dockerfile. | ||
This command mimics behavior of "docker build" command so that people can easily get started with BuildKit. | ||
This command is NOT the replacement of "docker build", and should NOT be used for building production images. | ||
By default, the built image is exported as a tar archive with OCI Image Layout ("./oci.tar"). | ||
You can import oci.tar to Docker as follows: | ||
$ skopeo copy oci-archive:oci.tar docker-daemon:foo/bar:latest | ||
` | ||
// TODO: call `docker load` rather than creating a file | ||
exporterOptDefault := cli.StringSlice([]string{"output=./oci.tar"}) | ||
dockerIncompatibleFlags := []cli.Flag{ | ||
cli.StringFlag{ | ||
Name: "buildkit-exporter", | ||
Usage: "Define exporter for build result", | ||
Value: "oci", // TODO: docker v1 exporter (unless moby supports OCI importer) | ||
}, | ||
cli.StringSliceFlag{ | ||
Name: "buildkit-exporter-opt", | ||
Usage: "Define custom options for exporter", | ||
Value: &exporterOptDefault, | ||
}, | ||
cli.StringFlag{ | ||
Name: "buildkit-addr", | ||
Usage: "listening address", | ||
EnvVar: "BUILDKIT_HOST", | ||
Value: appdefaults.Address, | ||
}, | ||
} | ||
app.Flags = append([]cli.Flag{ | ||
cli.StringFlag{ | ||
Name: "file, f", | ||
Usage: "Name of the Dockerfile (Default is 'PATH/Dockerfile')", | ||
}, | ||
cli.StringFlag{ | ||
Name: "target", | ||
Usage: "Set the target build stage to build.", | ||
}, | ||
cli.StringSliceFlag{ | ||
Name: "build-arg", | ||
Usage: "Set build-time variables", | ||
}, | ||
}, dockerIncompatibleFlags...) | ||
app.Action = action | ||
if err := app.Run(os.Args); err != nil { | ||
fmt.Fprintf(os.Stderr, "error: %v\n", err) | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
func action(clicontext *cli.Context) error { | ||
c, err := client.New(clicontext.GlobalString("buildkit-addr"), client.WithBlock()) | ||
if err != nil { | ||
return err | ||
} | ||
solveOpt, err := newSolveOpt(clicontext) | ||
if err != nil { | ||
return err | ||
} | ||
ch := make(chan *client.SolveStatus) | ||
eg, ctx := errgroup.WithContext(appcontext.Context()) | ||
eg.Go(func() error { | ||
return c.Solve(ctx, nil, *solveOpt, ch) | ||
}) | ||
eg.Go(func() error { | ||
if c, err := console.ConsoleFromFile(os.Stderr); err == nil { | ||
// not using shared context to not disrupt display but let is finish reporting errors | ||
return progressui.DisplaySolveStatus(context.TODO(), c, ch) | ||
} | ||
return nil | ||
}) | ||
return eg.Wait() | ||
} | ||
|
||
func newSolveOpt(clicontext *cli.Context) (*client.SolveOpt, error) { | ||
buildCtx := clicontext.Args().First() | ||
if buildCtx == "" { | ||
return nil, errors.New("please specify build context (e.g. \".\" for the current directory)") | ||
} else if buildCtx == "-" { | ||
return nil, errors.New("stdin not supported yet") | ||
} | ||
|
||
file := clicontext.String("file") | ||
if file == "" { | ||
file = filepath.Join(buildCtx, "Dockerfile") | ||
} | ||
exporterAttrs, err := attrMap(clicontext.StringSlice("buildkit-exporter-opt")) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "invalid buildkit-exporter-opt") | ||
} | ||
|
||
localDirs := map[string]string{ | ||
"context": buildCtx, | ||
"dockerfile": filepath.Dir(file), | ||
} | ||
|
||
frontendAttrs := map[string]string{ | ||
"filename": filepath.Base(file), | ||
} | ||
if target := clicontext.String("target"); target != "" { | ||
frontendAttrs["target"] = target | ||
} | ||
buildArgs, err := attrMap(clicontext.StringSlice("build-arg")) | ||
if err != nil { | ||
return nil, err | ||
} | ||
for k, v := range buildArgs { | ||
frontendAttrs["build-arg:"+k] = v | ||
} | ||
return &client.SolveOpt{ | ||
Exporter: clicontext.String("buildkit-exporter"), | ||
ExporterAttrs: exporterAttrs, | ||
LocalDirs: localDirs, | ||
Frontend: "dockerfile.v0", // TODO: use gateway | ||
FrontendAttrs: frontendAttrs, | ||
}, nil | ||
} | ||
|
||
func attrMap(sl []string) (map[string]string, error) { | ||
m := map[string]string{} | ||
for _, v := range sl { | ||
parts := strings.SplitN(v, "=", 2) | ||
if len(parts) != 2 { | ||
return nil, errors.Errorf("invalid value %s", v) | ||
} | ||
m[parts[0]] = parts[1] | ||
} | ||
return m, nil | ||
} |