2017-12-15 09:28:33 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2018-03-27 04:16:32 +00:00
|
|
|
"io"
|
2017-12-15 09:28:33 +00:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/containerd/console"
|
|
|
|
"github.com/moby/buildkit/client"
|
2018-07-24 10:51:57 +00:00
|
|
|
dockerfile "github.com/moby/buildkit/frontend/dockerfile/builder"
|
2017-12-15 09:28:33 +00:00
|
|
|
"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/sirupsen/logrus"
|
|
|
|
"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 loaded to Docker.
|
|
|
|
`
|
|
|
|
dockerIncompatibleFlags := []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "buildkit-addr",
|
|
|
|
Usage: "buildkit daemon address",
|
|
|
|
EnvVar: "BUILDKIT_HOST",
|
|
|
|
Value: appdefaults.Address,
|
|
|
|
},
|
2018-07-24 10:51:57 +00:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "clientside-frontend",
|
|
|
|
Usage: "run dockerfile frontend client side, rather than builtin to buildkitd",
|
|
|
|
EnvVar: "BUILDKIT_CLIENTSIDE_FRONTEND",
|
|
|
|
},
|
2017-12-15 09:28:33 +00:00
|
|
|
}
|
|
|
|
app.Flags = append([]cli.Flag{
|
|
|
|
cli.StringSliceFlag{
|
|
|
|
Name: "build-arg",
|
|
|
|
Usage: "Set build-time variables",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "file, f",
|
|
|
|
Usage: "Name of the Dockerfile (Default is 'PATH/Dockerfile')",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "tag, t",
|
|
|
|
Usage: "Name and optionally a tag in the 'name:tag' format",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "target",
|
|
|
|
Usage: "Set the target build stage to build.",
|
|
|
|
},
|
|
|
|
}, 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 {
|
2018-06-06 10:52:53 +00:00
|
|
|
ctx := appcontext.Context()
|
|
|
|
|
2017-12-15 09:28:33 +00:00
|
|
|
if tag := clicontext.String("tag"); tag == "" {
|
|
|
|
return errors.New("tag is not specified")
|
|
|
|
}
|
2018-06-06 10:52:53 +00:00
|
|
|
c, err := client.New(ctx, clicontext.String("buildkit-addr"), client.WithBlock())
|
2017-12-15 09:28:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-03-27 04:16:32 +00:00
|
|
|
pipeR, pipeW := io.Pipe()
|
|
|
|
solveOpt, err := newSolveOpt(clicontext, pipeW)
|
2017-12-15 09:28:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ch := make(chan *client.SolveStatus)
|
2018-06-06 10:52:53 +00:00
|
|
|
eg, ctx := errgroup.WithContext(ctx)
|
2017-12-15 09:28:33 +00:00
|
|
|
eg.Go(func() error {
|
2018-07-24 10:51:57 +00:00
|
|
|
var err error
|
|
|
|
if clicontext.Bool("clientside-frontend") {
|
|
|
|
_, err = c.Build(ctx, *solveOpt, "", dockerfile.Build, ch)
|
|
|
|
} else {
|
|
|
|
_, err = c.Solve(ctx, nil, *solveOpt, ch)
|
|
|
|
}
|
2018-05-03 00:35:07 +00:00
|
|
|
return err
|
2017-12-15 09:28:33 +00:00
|
|
|
})
|
|
|
|
eg.Go(func() error {
|
2018-05-30 02:49:51 +00:00
|
|
|
var c console.Console
|
|
|
|
if cn, err := console.ConsoleFromFile(os.Stderr); err == nil {
|
|
|
|
c = cn
|
2017-12-15 09:28:33 +00:00
|
|
|
}
|
2018-05-30 02:49:51 +00:00
|
|
|
// not using shared context to not disrupt display but let is finish reporting errors
|
2018-08-08 13:53:04 +00:00
|
|
|
return progressui.DisplaySolveStatus(context.TODO(), "", c, os.Stdout, ch)
|
2017-12-15 09:28:33 +00:00
|
|
|
})
|
2018-03-27 04:16:32 +00:00
|
|
|
eg.Go(func() error {
|
|
|
|
if err := loadDockerTar(pipeR); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return pipeR.Close()
|
|
|
|
})
|
2017-12-15 09:28:33 +00:00
|
|
|
if err := eg.Wait(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-03-27 04:16:32 +00:00
|
|
|
logrus.Infof("Loaded the image %q to Docker.", clicontext.String("tag"))
|
2017-12-15 09:28:33 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-03-27 04:16:32 +00:00
|
|
|
func newSolveOpt(clicontext *cli.Context, w io.WriteCloser) (*client.SolveOpt, error) {
|
2017-12-15 09:28:33 +00:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
localDirs := map[string]string{
|
|
|
|
"context": buildCtx,
|
|
|
|
"dockerfile": filepath.Dir(file),
|
|
|
|
}
|
|
|
|
|
2018-07-24 10:51:57 +00:00
|
|
|
frontend := "dockerfile.v0" // TODO: use gateway
|
|
|
|
if clicontext.Bool("clientside-frontend") {
|
|
|
|
frontend = ""
|
|
|
|
}
|
2017-12-15 09:28:33 +00:00
|
|
|
frontendAttrs := map[string]string{
|
|
|
|
"filename": filepath.Base(file),
|
|
|
|
}
|
|
|
|
if target := clicontext.String("target"); target != "" {
|
|
|
|
frontendAttrs["target"] = target
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, buildArg := range clicontext.StringSlice("build-arg") {
|
|
|
|
kv := strings.SplitN(buildArg, "=", 2)
|
|
|
|
if len(kv) != 2 {
|
|
|
|
return nil, errors.Errorf("invalid build-arg value %s", buildArg)
|
|
|
|
}
|
|
|
|
frontendAttrs["build-arg:"+kv[0]] = kv[1]
|
|
|
|
}
|
|
|
|
return &client.SolveOpt{
|
|
|
|
Exporter: "docker", // TODO: use containerd image store when it is integrated to Docker
|
|
|
|
ExporterAttrs: map[string]string{
|
2018-03-27 04:16:32 +00:00
|
|
|
"name": clicontext.String("tag"),
|
2017-12-15 09:28:33 +00:00
|
|
|
},
|
2018-03-27 04:16:32 +00:00
|
|
|
ExporterOutput: w,
|
|
|
|
LocalDirs: localDirs,
|
2018-07-24 10:51:57 +00:00
|
|
|
Frontend: frontend,
|
2018-03-27 04:16:32 +00:00
|
|
|
FrontendAttrs: frontendAttrs,
|
2017-12-15 09:28:33 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-03-27 04:16:32 +00:00
|
|
|
func loadDockerTar(r io.Reader) error {
|
2017-12-15 09:28:33 +00:00
|
|
|
// no need to use moby/moby/client here
|
2018-03-27 04:16:32 +00:00
|
|
|
cmd := exec.Command("docker", "load")
|
|
|
|
cmd.Stdin = r
|
2017-12-15 09:28:33 +00:00
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
return cmd.Run()
|
|
|
|
}
|