Skip to content

Commit

Permalink
Support environment variables in docker-container driver
Browse files Browse the repository at this point in the history
Fixes docker#169

Signed-off-by: Sune Keller <[email protected]>
  • Loading branch information
sirlatrom committed Oct 28, 2019
1 parent 6db68d0 commit 0abfd0e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
4 changes: 2 additions & 2 deletions commands/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,10 @@ func csvToMap(in []string) (map[string]string, error) {
}
for _, v := range fields {
p := strings.SplitN(v, "=", 2)
if len(p) != 2 {
if len(p) < 2 {
return nil, errors.Errorf("invalid value %q, expecting k=v", v)
}
m[p[0]] = p[1]
m[p[0]] = strings.Join(p[1:], "=")
}
}
return m, nil
Expand Down
3 changes: 3 additions & 0 deletions driver/docker-container/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Driver struct {
factory driver.Factory
netMode string
image string
env []string
}

func (d *Driver) Bootstrap(ctx context.Context, l progress.Logger) error {
Expand Down Expand Up @@ -57,6 +58,7 @@ func (d *Driver) create(ctx context.Context, l progress.SubLogger) error {
if d.image != "" {
imageName = d.image
}
env := d.env
if err := l.Wrap("pulling image "+imageName, func() error {
rc, err := d.DockerAPI.ImageCreate(ctx, imageName, types.ImageCreateOptions{})
if err != nil {
Expand All @@ -70,6 +72,7 @@ func (d *Driver) create(ctx context.Context, l progress.SubLogger) error {

cfg := &container.Config{
Image: imageName,
Env: env,
}
if d.InitConfig.BuildkitFlags != nil {
cfg.Cmd = d.InitConfig.BuildkitFlags
Expand Down
14 changes: 11 additions & 3 deletions driver/docker-container/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package docker

import (
"context"
"fmt"
"strings"

"github.com/docker/buildx/driver"
dockerclient "github.com/docker/docker/client"
Expand Down Expand Up @@ -39,14 +41,20 @@ func (f *factory) New(ctx context.Context, cfg driver.InitConfig) (driver.Driver
}
d := &Driver{factory: f, InitConfig: cfg}
for k, v := range cfg.DriverOpts {
switch k {
case "network":
switch {
case k == "network":
d.netMode = v
if v == "host" {
d.InitConfig.BuildkitFlags = append(d.InitConfig.BuildkitFlags, "--allow-insecure-entitlement=network.host")
}
case "image":
case k == "image":
d.image = v
case strings.HasPrefix(k, "env."):
envName := k[4:]
if len(envName) == 0 {
return nil, errors.Errorf("invalid env option %q, expecting env.FOO=bar", k)
}
d.env = append(d.env, fmt.Sprintf("%s=%s", envName, v))
default:
return nil, errors.Errorf("invalid driver option %s for docker-container driver", k)
}
Expand Down

0 comments on commit 0abfd0e

Please sign in to comment.