build: handle --add-host

Signed-off-by: Tibor Vass <tibor@docker.com>
pull/27/head
Tibor Vass 6 years ago
parent dc07613bd2
commit 77ed999572

@ -36,6 +36,7 @@ type Options struct {
BuildArgs map[string]string BuildArgs map[string]string
Pull bool Pull bool
ImageIDFile string ImageIDFile string
ExtraHosts []string
NoCache bool NoCache bool
Target string Target string
@ -227,6 +228,12 @@ func Build(ctx context.Context, drivers []DriverInfo, opt map[string]Options, pw
so.FrontendAttrs["platform"] = strings.Join(pp, ",") so.FrontendAttrs["platform"] = strings.Join(pp, ",")
} }
extraHosts, err := toBuildkitExtraHosts(opt.ExtraHosts)
if err != nil {
return nil, err
}
so.FrontendAttrs["add-hosts"] = extraHosts
var statusCh chan *client.SolveStatus var statusCh chan *client.SolveStatus
if pw != nil { if pw != nil {
statusCh = pw.Status() statusCh = pw.Status()

@ -3,7 +3,11 @@ package build
import ( import (
"archive/tar" "archive/tar"
"bytes" "bytes"
"net"
"os" "os"
"strings"
"github.com/pkg/errors"
) )
// archiveHeaderSize is the number of bytes in an archive header // archiveHeaderSize is the number of bytes in an archive header
@ -32,3 +36,20 @@ func isArchive(header []byte) bool {
_, err := r.Next() _, err := r.Next()
return err == nil return err == nil
} }
// toBuildkitExtraHosts converts hosts from docker key:value format to buildkit's csv format
func toBuildkitExtraHosts(inp []string) (string, error) {
if len(inp) == 0 {
return "", nil
}
hosts := make([]string, 0, len(inp))
for _, h := range inp {
parts := strings.Split(h, ":")
if len(parts) != 2 || parts[0] == "" || net.ParseIP(parts[1]) == nil {
return "", errors.Errorf("invalid host %s", h)
}
hosts = append(hosts, parts[0]+"="+parts[1])
}
return strings.Join(hosts, ","), nil
}

@ -31,9 +31,9 @@ type buildOptions struct {
ssh []string ssh []string
outputs []string outputs []string
imageIDFile string imageIDFile string
extraHosts []string
// unimplemented // unimplemented
extraHosts []string
squash bool squash bool
quiet bool quiet bool
networkMode string networkMode string
@ -62,9 +62,6 @@ type commonOptions struct {
} }
func runBuild(dockerCli command.Cli, in buildOptions) error { func runBuild(dockerCli command.Cli, in buildOptions) error {
if len(in.extraHosts) > 0 {
return errors.Errorf("extra hosts currently not implemented")
}
if in.squash { if in.squash {
return errors.Errorf("squash currently not implemented") return errors.Errorf("squash currently not implemented")
} }
@ -90,6 +87,7 @@ func runBuild(dockerCli command.Cli, in buildOptions) error {
NoCache: in.noCache, NoCache: in.noCache,
Target: in.target, Target: in.target,
ImageIDFile: in.imageIDFile, ImageIDFile: in.imageIDFile,
ExtraHosts: in.extraHosts,
} }
platforms, err := build.ParsePlatformSpecs(in.platforms) platforms, err := build.ParsePlatformSpecs(in.platforms)
@ -169,7 +167,6 @@ func buildCmd(dockerCli command.Cli) *cobra.Command {
flags.BoolVar(&options.squash, "squash", false, "Squash newly built layers into a single new layer") flags.BoolVar(&options.squash, "squash", false, "Squash newly built layers into a single new layer")
flags.MarkHidden("quiet") flags.MarkHidden("quiet")
flags.MarkHidden("network") flags.MarkHidden("network")
flags.MarkHidden("add-host")
flags.MarkHidden("squash") flags.MarkHidden("squash")
// hidden flags // hidden flags

Loading…
Cancel
Save