diff --git a/docs/reference/buildx_create.md b/docs/reference/buildx_create.md index c14efdaa..f15f84b3 100644 --- a/docs/reference/buildx_create.md +++ b/docs/reference/buildx_create.md @@ -153,6 +153,7 @@ No driver options. - `image=IMAGE` - Sets the container image to be used for running buildkit. - `network=NETMODE` - Sets the network mode for running the buildkit container. - `cgroup-parent=CGROUP` - Sets the cgroup parent of the buildkit container if docker is using the "cgroupfs" driver. Defaults to `/docker/buildx`. +- `restart-policy=POLICY` - Sets the [restart policy](https://docs.docker.com/engine/reference/run/#restart-policies---restart) of the buildkit container. #### `kubernetes` driver diff --git a/driver/docker-container/driver.go b/driver/docker-container/driver.go index f81ad8a3..b8e1f05c 100644 --- a/driver/docker-container/driver.go +++ b/driver/docker-container/driver.go @@ -35,11 +35,12 @@ const ( type Driver struct { driver.InitConfig - factory driver.Factory - netMode string - image string - cgroupParent string - env []string + factory driver.Factory + netMode string + image string + cgroupParent string + restartPolicy container.RestartPolicy + env []string } func (d *Driver) IsMobyDriver() bool { @@ -110,7 +111,8 @@ func (d *Driver) create(ctx context.Context, l progress.SubLogger) error { if err := l.Wrap("creating container "+d.Name, func() error { hc := &container.HostConfig{ - Privileged: true, + Privileged: true, + RestartPolicy: d.restartPolicy, Mounts: []mount.Mount{ { Type: mount.TypeVolume, diff --git a/driver/docker-container/factory.go b/driver/docker-container/factory.go index 118d9c5a..a5ea32b5 100644 --- a/driver/docker-container/factory.go +++ b/driver/docker-container/factory.go @@ -6,12 +6,14 @@ import ( "strings" "github.com/docker/buildx/driver" + dockeropts "github.com/docker/cli/opts" dockerclient "github.com/docker/docker/client" "github.com/pkg/errors" ) const prioritySupported = 30 const priorityUnsupported = 70 +const defaultRestartPolicy = "unless-stopped" func init() { driver.Register(&factory{}) @@ -39,7 +41,15 @@ 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} + rp, err := dockeropts.ParseRestartPolicy(defaultRestartPolicy) + if err != nil { + return nil, err + } + d := &Driver{ + factory: f, + InitConfig: cfg, + restartPolicy: rp, + } for k, v := range cfg.DriverOpts { switch { case k == "network": @@ -51,6 +61,11 @@ func (f *factory) New(ctx context.Context, cfg driver.InitConfig) (driver.Driver d.image = v case k == "cgroup-parent": d.cgroupParent = v + case k == "restart-policy": + d.restartPolicy, err = dockeropts.ParseRestartPolicy(v) + if err != nil { + return nil, err + } case strings.HasPrefix(k, "env."): envName := strings.TrimPrefix(k, "env.") if envName == "" {