Merge pull request #43697 from thaJeztah/builder_remove_removecontainer
Builder: some improvements on cleaning up containers
This commit is contained in:
commit
e6f46e637c
3 changed files with 9 additions and 23 deletions
|
@ -62,8 +62,6 @@ type ExecBackend interface {
|
||||||
ContainerCreateIgnoreImagesArgsEscaped(ctx context.Context, config backend.ContainerCreateConfig) (container.CreateResponse, error)
|
ContainerCreateIgnoreImagesArgsEscaped(ctx context.Context, config backend.ContainerCreateConfig) (container.CreateResponse, error)
|
||||||
// ContainerRm removes a container specified by `id`.
|
// ContainerRm removes a container specified by `id`.
|
||||||
ContainerRm(name string, config *backend.ContainerRmConfig) error
|
ContainerRm(name string, config *backend.ContainerRmConfig) error
|
||||||
// ContainerKill stops the container execution abruptly.
|
|
||||||
ContainerKill(containerID string, sig string) error
|
|
||||||
// ContainerStart starts a new container
|
// ContainerStart starts a new container
|
||||||
ContainerStart(ctx context.Context, containerID string, hostConfig *container.HostConfig, checkpoint string, checkpointDir string) error
|
ContainerStart(ctx context.Context, containerID string, hostConfig *container.HostConfig, checkpoint string, checkpointDir string) error
|
||||||
// ContainerWait stops processing until the given container is stopped.
|
// ContainerWait stops processing until the given container is stopped.
|
||||||
|
|
|
@ -10,6 +10,7 @@ import (
|
||||||
"github.com/docker/docker/api/types/container"
|
"github.com/docker/docker/api/types/container"
|
||||||
"github.com/docker/docker/builder"
|
"github.com/docker/docker/builder"
|
||||||
containerpkg "github.com/docker/docker/container"
|
containerpkg "github.com/docker/docker/container"
|
||||||
|
"github.com/docker/docker/errdefs"
|
||||||
"github.com/docker/docker/pkg/stringid"
|
"github.com/docker/docker/pkg/stringid"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
@ -60,9 +61,11 @@ func (c *containerManager) Run(ctx context.Context, cID string, stdout, stderr i
|
||||||
go func() {
|
go func() {
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
log.G(ctx).Debugln("Build cancelled, killing and removing container:", cID)
|
log.G(ctx).Debugln("Build cancelled, removing container:", cID)
|
||||||
c.backend.ContainerKill(cID, "")
|
err = c.backend.ContainerRm(cID, &backend.ContainerRmConfig{ForceRemove: true, RemoveVolume: true})
|
||||||
c.removeContainer(cID, stdout)
|
if err != nil {
|
||||||
|
_, _ = fmt.Fprintf(stdout, "Removing container %s: %v\n", stringid.TruncateID(cID), err)
|
||||||
|
}
|
||||||
cancelErrCh <- errCancelled
|
cancelErrCh <- errCancelled
|
||||||
case <-finished:
|
case <-finished:
|
||||||
cancelErrCh <- nil
|
cancelErrCh <- nil
|
||||||
|
@ -122,23 +125,12 @@ func (e *statusCodeError) StatusCode() int {
|
||||||
return e.code
|
return e.code
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *containerManager) removeContainer(containerID string, stdout io.Writer) error {
|
|
||||||
rmConfig := &backend.ContainerRmConfig{
|
|
||||||
ForceRemove: true,
|
|
||||||
RemoveVolume: true,
|
|
||||||
}
|
|
||||||
if err := c.backend.ContainerRm(containerID, rmConfig); err != nil {
|
|
||||||
fmt.Fprintf(stdout, "Error removing intermediate container %s: %v\n", stringid.TruncateID(containerID), err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// RemoveAll containers managed by this container manager
|
// RemoveAll containers managed by this container manager
|
||||||
func (c *containerManager) RemoveAll(stdout io.Writer) {
|
func (c *containerManager) RemoveAll(stdout io.Writer) {
|
||||||
for containerID := range c.tmpContainers {
|
for containerID := range c.tmpContainers {
|
||||||
if err := c.removeContainer(containerID, stdout); err != nil {
|
if err := c.backend.ContainerRm(containerID, &backend.ContainerRmConfig{ForceRemove: true, RemoveVolume: true}); err != nil && !errdefs.IsNotFound(err) {
|
||||||
return
|
_, _ = fmt.Fprintf(stdout, "Removing intermediate container %s: %v\n", stringid.TruncateID(containerID), err)
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
delete(c.tmpContainers, containerID)
|
delete(c.tmpContainers, containerID)
|
||||||
_, _ = fmt.Fprintf(stdout, " ---> Removed intermediate container %s\n", stringid.TruncateID(containerID))
|
_, _ = fmt.Fprintf(stdout, " ---> Removed intermediate container %s\n", stringid.TruncateID(containerID))
|
||||||
|
|
|
@ -45,10 +45,6 @@ func (m *MockBackend) CommitBuildStep(ctx context.Context, c backend.CommitConfi
|
||||||
return "", nil
|
return "", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockBackend) ContainerKill(containerID string, sig string) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *MockBackend) ContainerStart(ctx context.Context, containerID string, hostConfig *container.HostConfig, checkpoint string, checkpointDir string) error {
|
func (m *MockBackend) ContainerStart(ctx context.Context, containerID string, hostConfig *container.HostConfig, checkpoint string, checkpointDir string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue