From bcc882cbf134cabb4ea5e154bff0aa8e90a5398c Mon Sep 17 00:00:00 2001 From: Tonis Tiigi Date: Wed, 31 Jul 2019 17:42:49 -0700 Subject: [PATCH] docker-container: allow using host network Signed-off-by: Tonis Tiigi --- driver/docker-container/driver.go | 9 +++++++-- driver/docker-container/factory.go | 14 +++++++++++++- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/driver/docker-container/driver.go b/driver/docker-container/driver.go index 99207f01..ecdd7199 100644 --- a/driver/docker-container/driver.go +++ b/driver/docker-container/driver.go @@ -27,6 +27,7 @@ var buildkitImage = "moby/buildkit:master" // TODO: make this verified and confi type Driver struct { driver.InitConfig factory driver.Factory + netMode string } func (d *Driver) Bootstrap(ctx context.Context, l progress.Logger) error { @@ -70,9 +71,13 @@ func (d *Driver) create(ctx context.Context, l progress.SubLogger) error { } if err := l.Wrap("creating container "+d.Name, func() error { - _, err := d.DockerAPI.ContainerCreate(ctx, cfg, &container.HostConfig{ + hc := &container.HostConfig{ Privileged: true, - }, &network.NetworkingConfig{}, d.Name) + } + if d.netMode != "" { + hc.NetworkMode = container.NetworkMode(d.netMode) + } + _, err := d.DockerAPI.ContainerCreate(ctx, cfg, hc, &network.NetworkingConfig{}, d.Name) if err != nil { return err } diff --git a/driver/docker-container/factory.go b/driver/docker-container/factory.go index 16b7c15c..37db8268 100644 --- a/driver/docker-container/factory.go +++ b/driver/docker-container/factory.go @@ -37,8 +37,20 @@ func (f *factory) New(ctx context.Context, cfg driver.InitConfig) (driver.Driver if cfg.DockerAPI == nil { return nil, errors.Errorf("%s driver requires docker API access", f.Name()) } + d := &Driver{factory: f, InitConfig: cfg} + for k, v := range cfg.DriverOpts { + switch k { + case "network": + d.netMode = v + if v == "host" { + d.InitConfig.BuildkitFlags = append(d.InitConfig.BuildkitFlags, "--allow-insecure-entitlement=network.host") + } + default: + return nil, errors.Errorf("invalid driver option %s for docker-container driver") + } + } - return &Driver{factory: f, InitConfig: cfg}, nil + return d, nil } func (f *factory) AllowsInstances() bool {