Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docker-container: restart-policy opt #1271

Merged
merged 1 commit into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this PR might've missed updating these docs 👀

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's here :-) docker/docs#19355

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
17 changes: 16 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,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":
Expand Down Expand Up @@ -82,6 +92,11 @@ 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":
d.restartPolicy, err = dockeropts.ParseRestartPolicy(v)
if err != nil {
return nil, err
}
case strings.HasPrefix(k, "env."):
envName := strings.TrimPrefix(k, "env.")
if envName == "" {
Expand Down
20 changes: 20 additions & 0 deletions tests/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func createCmd(sb integration.Sandbox, opts ...cmdOpt) (string, error) {

var createTests = []func(t *testing.T, sb integration.Sandbox){
testCreateMemoryLimit,
testCreateRestartAlways,
}

func testCreateMemoryLimit(t *testing.T, sb integration.Sandbox) {
Expand All @@ -37,3 +38,22 @@ func testCreateMemoryLimit(t *testing.T, sb integration.Sandbox) {
require.NoError(t, err, out)
builderName = strings.TrimSpace(out)
}

func testCreateRestartAlways(t *testing.T, sb integration.Sandbox) {
if sb.Name() != "docker-container" {
t.Skip("only testing for docker-container driver")
}

var builderName string
t.Cleanup(func() {
if builderName == "" {
return
}
out, err := rmCmd(sb, withArgs(builderName))
require.NoError(t, err, out)
})

out, err := createCmd(sb, withArgs("--driver", "docker-container", "--driver-opt", "restart-policy=always"))
require.NoError(t, err, out)
builderName = strings.TrimSpace(out)
}