Skip to content

Commit

Permalink
feat: support driver-opt restart-policy, default unless-stopped
Browse files Browse the repository at this point in the history
  • Loading branch information
whatwewant committed Jan 22, 2024
1 parent d852568 commit b7dd740
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
26 changes: 14 additions & 12 deletions driver/docker-container/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,18 @@ type Driver struct {

// if you add fields, remember to update docs:
// https://github.com/docker/docs/blob/main/content/build/drivers/docker-container.md
netMode string
image string
memory opts.MemBytes
memorySwap opts.MemSwapBytes
cpuQuota int64
cpuPeriod int64
cpuShares int64
cpusetCpus string
cpusetMems string
cgroupParent string
env []string
netMode string
image string
memory opts.MemBytes
memorySwap opts.MemSwapBytes
cpuQuota int64
cpuPeriod int64
cpuShares int64
cpusetCpus string
cpusetMems string
cgroupParent string
restartPolicy container.RestartPolicy
env []string
}

func (d *Driver) IsMobyDriver() bool {
Expand Down Expand Up @@ -121,7 +122,8 @@ func (d *Driver) create(ctx context.Context, l progress.SubLogger) error {
useInit := true // let it cleanup exited processes created by BuildKit's container API
return l.Wrap("creating container "+d.Name, func() error {
hc := &container.HostConfig{
Privileged: true,
Privileged: true,
RestartPolicy: d.restartPolicy,
Mounts: []mount.Mount{
{
Type: mount.TypeVolume,
Expand Down
19 changes: 18 additions & 1 deletion driver/docker-container/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,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{})
Expand Down Expand Up @@ -40,7 +42,16 @@ 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":
Expand Down Expand Up @@ -82,6 +93,12 @@ func (f *factory) New(ctx context.Context, cfg driver.InitConfig) (driver.Driver
d.cpusetMems = v
case k == "cgroup-parent":
d.cgroupParent = v
case k == "restart-policy":
vv, err := dockeropts.ParseRestartPolicy(v)
if err != nil {
return nil, err
}
d.restartPolicy = vv
case strings.HasPrefix(k, "env."):
envName := strings.TrimPrefix(k, "env.")
if envName == "" {
Expand Down

0 comments on commit b7dd740

Please sign in to comment.