mirror of
https://gitea.com/Lydanne/buildx.git
synced 2025-05-29 17:05:46 +08:00
Merge pull request #1271 from crazy-max/ctn-restart
docker-container: restart-policy opt
This commit is contained in:
commit
d0177c6da3
@ -44,17 +44,18 @@ type Driver struct {
|
|||||||
|
|
||||||
// if you add fields, remember to update docs:
|
// if you add fields, remember to update docs:
|
||||||
// https://github.com/docker/docs/blob/main/content/build/drivers/docker-container.md
|
// https://github.com/docker/docs/blob/main/content/build/drivers/docker-container.md
|
||||||
netMode string
|
netMode string
|
||||||
image string
|
image string
|
||||||
memory opts.MemBytes
|
memory opts.MemBytes
|
||||||
memorySwap opts.MemSwapBytes
|
memorySwap opts.MemSwapBytes
|
||||||
cpuQuota int64
|
cpuQuota int64
|
||||||
cpuPeriod int64
|
cpuPeriod int64
|
||||||
cpuShares int64
|
cpuShares int64
|
||||||
cpusetCpus string
|
cpusetCpus string
|
||||||
cpusetMems string
|
cpusetMems string
|
||||||
cgroupParent string
|
cgroupParent string
|
||||||
env []string
|
restartPolicy container.RestartPolicy
|
||||||
|
env []string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Driver) IsMobyDriver() bool {
|
func (d *Driver) IsMobyDriver() bool {
|
||||||
@ -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
|
useInit := true // let it cleanup exited processes created by BuildKit's container API
|
||||||
return l.Wrap("creating container "+d.Name, func() error {
|
return l.Wrap("creating container "+d.Name, func() error {
|
||||||
hc := &container.HostConfig{
|
hc := &container.HostConfig{
|
||||||
Privileged: true,
|
Privileged: true,
|
||||||
|
RestartPolicy: d.restartPolicy,
|
||||||
Mounts: []mount.Mount{
|
Mounts: []mount.Mount{
|
||||||
{
|
{
|
||||||
Type: mount.TypeVolume,
|
Type: mount.TypeVolume,
|
||||||
|
@ -7,12 +7,14 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/docker/buildx/driver"
|
"github.com/docker/buildx/driver"
|
||||||
|
dockeropts "github.com/docker/cli/opts"
|
||||||
dockerclient "github.com/docker/docker/client"
|
dockerclient "github.com/docker/docker/client"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
const prioritySupported = 30
|
const prioritySupported = 30
|
||||||
const priorityUnsupported = 70
|
const priorityUnsupported = 70
|
||||||
|
const defaultRestartPolicy = "unless-stopped"
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
driver.Register(&factory{})
|
driver.Register(&factory{})
|
||||||
@ -40,7 +42,15 @@ func (f *factory) New(ctx context.Context, cfg driver.InitConfig) (driver.Driver
|
|||||||
if cfg.DockerAPI == nil {
|
if cfg.DockerAPI == nil {
|
||||||
return nil, errors.Errorf("%s driver requires docker API access", f.Name())
|
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 {
|
for k, v := range cfg.DriverOpts {
|
||||||
switch {
|
switch {
|
||||||
case k == "network":
|
case k == "network":
|
||||||
@ -82,6 +92,11 @@ func (f *factory) New(ctx context.Context, cfg driver.InitConfig) (driver.Driver
|
|||||||
d.cpusetMems = v
|
d.cpusetMems = v
|
||||||
case k == "cgroup-parent":
|
case k == "cgroup-parent":
|
||||||
d.cgroupParent = v
|
d.cgroupParent = v
|
||||||
|
case k == "restart-policy":
|
||||||
|
d.restartPolicy, err = dockeropts.ParseRestartPolicy(v)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
case strings.HasPrefix(k, "env."):
|
case strings.HasPrefix(k, "env."):
|
||||||
envName := strings.TrimPrefix(k, "env.")
|
envName := strings.TrimPrefix(k, "env.")
|
||||||
if envName == "" {
|
if envName == "" {
|
||||||
|
@ -17,6 +17,7 @@ func createCmd(sb integration.Sandbox, opts ...cmdOpt) (string, error) {
|
|||||||
|
|
||||||
var createTests = []func(t *testing.T, sb integration.Sandbox){
|
var createTests = []func(t *testing.T, sb integration.Sandbox){
|
||||||
testCreateMemoryLimit,
|
testCreateMemoryLimit,
|
||||||
|
testCreateRestartAlways,
|
||||||
}
|
}
|
||||||
|
|
||||||
func testCreateMemoryLimit(t *testing.T, sb integration.Sandbox) {
|
func testCreateMemoryLimit(t *testing.T, sb integration.Sandbox) {
|
||||||
@ -37,3 +38,22 @@ func testCreateMemoryLimit(t *testing.T, sb integration.Sandbox) {
|
|||||||
require.NoError(t, err, out)
|
require.NoError(t, err, out)
|
||||||
builderName = strings.TrimSpace(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)
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user