Skip to content

Commit 65a6955

Browse files
authored
Merge pull request #672 from crazy-max/keep-buildkit-state
Keep BuildKit state in a volume
2 parents 6e3a319 + 258d12b commit 65a6955

File tree

7 files changed

+90
-42
lines changed

7 files changed

+90
-42
lines changed

commands/rm.go

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import (
1111
)
1212

1313
type rmOptions struct {
14-
builder string
14+
builder string
15+
keepState bool
1516
}
1617

1718
func runRm(dockerCli command.Cli, in rmOptions) error {
@@ -28,7 +29,7 @@ func runRm(dockerCli command.Cli, in rmOptions) error {
2829
if err != nil {
2930
return err
3031
}
31-
err1 := stop(ctx, dockerCli, ng, true)
32+
err1 := rm(ctx, dockerCli, ng, in.keepState)
3233
if err := txn.Remove(ng.Name); err != nil {
3334
return err
3435
}
@@ -40,7 +41,7 @@ func runRm(dockerCli command.Cli, in rmOptions) error {
4041
return err
4142
}
4243
if ng != nil {
43-
err1 := stop(ctx, dockerCli, ng, true)
44+
err1 := rm(ctx, dockerCli, ng, in.keepState)
4445
if err := txn.Remove(ng.Name); err != nil {
4546
return err
4647
}
@@ -66,10 +67,13 @@ func rmCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
6667
},
6768
}
6869

70+
flags := cmd.Flags()
71+
flags.BoolVar(&options.keepState, "keep-state", false, "Keep BuildKit state")
72+
6973
return cmd
7074
}
7175

72-
func stop(ctx context.Context, dockerCli command.Cli, ng *store.NodeGroup, rm bool) error {
76+
func rm(ctx context.Context, dockerCli command.Cli, ng *store.NodeGroup, keepState bool) error {
7377
dis, err := driversForNodeGroup(ctx, dockerCli, ng, "")
7478
if err != nil {
7579
return err
@@ -79,34 +83,9 @@ func stop(ctx context.Context, dockerCli command.Cli, ng *store.NodeGroup, rm bo
7983
if err := di.Driver.Stop(ctx, true); err != nil {
8084
return err
8185
}
82-
if rm {
83-
if err := di.Driver.Rm(ctx, true); err != nil {
84-
return err
85-
}
86-
}
87-
}
88-
if di.Err != nil {
89-
err = di.Err
90-
}
91-
}
92-
return err
93-
}
94-
95-
func stopCurrent(ctx context.Context, dockerCli command.Cli, rm bool) error {
96-
dis, err := getDefaultDrivers(ctx, dockerCli, false, "")
97-
if err != nil {
98-
return err
99-
}
100-
for _, di := range dis {
101-
if di.Driver != nil {
102-
if err := di.Driver.Stop(ctx, true); err != nil {
86+
if err := di.Driver.Rm(ctx, true, !keepState); err != nil {
10387
return err
10488
}
105-
if rm {
106-
if err := di.Driver.Rm(ctx, true); err != nil {
107-
return err
108-
}
109-
}
11089
}
11190
if di.Err != nil {
11291
err = di.Err

commands/stop.go

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package commands
22

33
import (
4+
"context"
5+
6+
"github.com/docker/buildx/store"
47
"github.com/docker/cli/cli"
58
"github.com/docker/cli/cli/command"
69
"github.com/moby/buildkit/util/appcontext"
@@ -25,7 +28,7 @@ func runStop(dockerCli command.Cli, in stopOptions) error {
2528
if err != nil {
2629
return err
2730
}
28-
if err := stop(ctx, dockerCli, ng, false); err != nil {
31+
if err := stop(ctx, dockerCli, ng); err != nil {
2932
return err
3033
}
3134
return nil
@@ -36,10 +39,10 @@ func runStop(dockerCli command.Cli, in stopOptions) error {
3639
return err
3740
}
3841
if ng != nil {
39-
return stop(ctx, dockerCli, ng, false)
42+
return stop(ctx, dockerCli, ng)
4043
}
4144

42-
return stopCurrent(ctx, dockerCli, false)
45+
return stopCurrent(ctx, dockerCli)
4346
}
4447

4548
func stopCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
@@ -66,3 +69,39 @@ func stopCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
6669

6770
return cmd
6871
}
72+
73+
func stop(ctx context.Context, dockerCli command.Cli, ng *store.NodeGroup) error {
74+
dis, err := driversForNodeGroup(ctx, dockerCli, ng, "")
75+
if err != nil {
76+
return err
77+
}
78+
for _, di := range dis {
79+
if di.Driver != nil {
80+
if err := di.Driver.Stop(ctx, true); err != nil {
81+
return err
82+
}
83+
}
84+
if di.Err != nil {
85+
err = di.Err
86+
}
87+
}
88+
return err
89+
}
90+
91+
func stopCurrent(ctx context.Context, dockerCli command.Cli) error {
92+
dis, err := getDefaultDrivers(ctx, dockerCli, false, "")
93+
if err != nil {
94+
return err
95+
}
96+
for _, di := range dis {
97+
if di.Driver != nil {
98+
if err := di.Driver.Stop(ctx, true); err != nil {
99+
return err
100+
}
101+
}
102+
if di.Err != nil {
103+
err = di.Err
104+
}
105+
}
106+
return err
107+
}

docs/reference/buildx_rm.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,24 @@ docker buildx rm [NAME]
77
<!---MARKER_GEN_START-->
88
Remove a builder instance
99

10+
### Options
11+
12+
| Name | Description |
13+
| --- | --- |
14+
| `--builder string` | Override the configured builder instance |
15+
| [`--keep-state`](#keep-state) | Keep BuildKit state |
16+
1017

1118
<!---MARKER_GEN_END-->
1219

1320
## Description
1421

1522
Removes the specified or current builder. It is a no-op attempting to remove the
1623
default builder.
24+
25+
## Examples
26+
27+
### <a name="keep-state"></a> Keep BuildKit state (--keep-state)
28+
29+
Keep BuildKit state, so it can be reused by a new builder with the same name.
30+
Currently, only supported by the [`docker-container` driver](buildx_create.md#driver).

driver/docker-container/driver.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,18 @@ import (
1717
"github.com/docker/docker/api/types"
1818
dockertypes "github.com/docker/docker/api/types"
1919
"github.com/docker/docker/api/types/container"
20+
"github.com/docker/docker/api/types/mount"
2021
"github.com/docker/docker/api/types/network"
2122
dockerclient "github.com/docker/docker/client"
2223
"github.com/docker/docker/pkg/stdcopy"
2324
"github.com/moby/buildkit/client"
25+
"github.com/moby/buildkit/util/appdefaults"
2426
"github.com/moby/buildkit/util/tracing/detect"
2527
"github.com/pkg/errors"
2628
)
2729

30+
const volumeStateSuffix = "_state"
31+
2832
type Driver struct {
2933
driver.InitConfig
3034
factory driver.Factory
@@ -103,6 +107,13 @@ func (d *Driver) create(ctx context.Context, l progress.SubLogger) error {
103107
hc := &container.HostConfig{
104108
Privileged: true,
105109
UsernsMode: "host",
110+
Mounts: []mount.Mount{
111+
{
112+
Type: mount.TypeVolume,
113+
Source: d.Name + volumeStateSuffix,
114+
Target: appdefaults.Root,
115+
},
116+
},
106117
}
107118
if d.netMode != "" {
108119
hc.NetworkMode = container.NetworkMode(d.netMode)
@@ -226,7 +237,7 @@ func (d *Driver) start(ctx context.Context, l progress.SubLogger) error {
226237
}
227238

228239
func (d *Driver) Info(ctx context.Context) (*driver.Info, error) {
229-
container, err := d.DockerAPI.ContainerInspect(ctx, d.Name)
240+
ctn, err := d.DockerAPI.ContainerInspect(ctx, d.Name)
230241
if err != nil {
231242
if dockerclient.IsErrNotFound(err) {
232243
return &driver.Info{
@@ -236,7 +247,7 @@ func (d *Driver) Info(ctx context.Context) (*driver.Info, error) {
236247
return nil, err
237248
}
238249

239-
if container.State.Running {
250+
if ctn.State.Running {
240251
return &driver.Info{
241252
Status: driver.Running,
242253
}, nil
@@ -258,16 +269,21 @@ func (d *Driver) Stop(ctx context.Context, force bool) error {
258269
return nil
259270
}
260271

261-
func (d *Driver) Rm(ctx context.Context, force bool) error {
272+
func (d *Driver) Rm(ctx context.Context, force bool, rmVolume bool) error {
262273
info, err := d.Info(ctx)
263274
if err != nil {
264275
return err
265276
}
266277
if info.Status != driver.Inactive {
267-
return d.DockerAPI.ContainerRemove(ctx, d.Name, dockertypes.ContainerRemoveOptions{
278+
if err := d.DockerAPI.ContainerRemove(ctx, d.Name, dockertypes.ContainerRemoveOptions{
268279
RemoveVolumes: true,
269-
Force: true,
270-
})
280+
Force: force,
281+
}); err != nil {
282+
return err
283+
}
284+
if rmVolume {
285+
return d.DockerAPI.VolumeRemove(ctx, d.Name+volumeStateSuffix, false)
286+
}
271287
}
272288
return nil
273289
}

driver/docker/driver.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func (d *Driver) Stop(ctx context.Context, force bool) error {
3333
return nil
3434
}
3535

36-
func (d *Driver) Rm(ctx context.Context, force bool) error {
36+
func (d *Driver) Rm(ctx context.Context, force bool, rmVolume bool) error {
3737
return nil
3838
}
3939

driver/driver.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ type Driver interface {
5454
Bootstrap(context.Context, progress.Logger) error
5555
Info(context.Context) (*Info, error)
5656
Stop(ctx context.Context, force bool) error
57-
Rm(ctx context.Context, force bool) error
57+
Rm(ctx context.Context, force bool, rmVolume bool) error
5858
Client(ctx context.Context) (*client.Client, error)
5959
Features() map[Feature]bool
6060
IsMobyDriver() bool

driver/kubernetes/driver.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func (d *Driver) Stop(ctx context.Context, force bool) error {
143143
return nil
144144
}
145145

146-
func (d *Driver) Rm(ctx context.Context, force bool) error {
146+
func (d *Driver) Rm(ctx context.Context, force bool, rmVolume bool) error {
147147
if err := d.deploymentClient.Delete(ctx, d.deployment.Name, metav1.DeleteOptions{}); err != nil {
148148
return errors.Wrapf(err, "error while calling deploymentClient.Delete for %q", d.deployment.Name)
149149
}

0 commit comments

Comments
 (0)