Skip to content

Commit

Permalink
Merge pull request #706 from jim-minter/no_force_remove
Browse files Browse the repository at this point in the history
Merged by openshift-bot
  • Loading branch information
OpenShift Bot authored Mar 15, 2017
2 parents 0e899bd + 952341a commit 7e0fc9e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
17 changes: 15 additions & 2 deletions pkg/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ type Client interface {
ContainerInspect(ctx context.Context, containerID string) (dockertypes.ContainerJSON, error)
ContainerRemove(ctx context.Context, containerID string, options dockertypes.ContainerRemoveOptions) error
ContainerStart(ctx context.Context, containerID string) error
ContainerKill(ctx context.Context, containerID, signal string) error
ContainerWait(ctx context.Context, containerID string) (int, error)
CopyToContainer(ctx context.Context, container, path string, content io.Reader, opts dockertypes.CopyToContainerOptions) error
CopyFromContainer(ctx context.Context, container, srcPath string) (io.ReadCloser, dockertypes.ContainerPathStat, error)
Expand Down Expand Up @@ -600,11 +601,17 @@ func (d *stiDocker) RemoveContainer(id string) error {
defer cancel()
opts := dockertypes.ContainerRemoveOptions{
RemoveVolumes: true,
Force: true,
}
return d.client.ContainerRemove(ctx, id, opts)
}

// KillContainer kills a container.
func (d *stiDocker) KillContainer(id string) error {
ctx, cancel := getDefaultContext()
defer cancel()
return d.client.ContainerKill(ctx, id, "SIGKILL")
}

// GetLabels retrieves the labels of the given image.
func (d *stiDocker) GetLabels(name string) (map[string]string, error) {
name = getImageName(name)
Expand Down Expand Up @@ -932,6 +939,13 @@ func (d *stiDocker) RunContainer(opts RunContainerOptions) error {

// Container was created, so we defer its removal, and also remove it if we get a SIGINT/SIGTERM/SIGQUIT/SIGHUP.
removeContainer := func() {
glog.V(4).Infof("Killing container %q ...", container.ID)
if killErr := d.KillContainer(container.ID); killErr != nil {
glog.V(0).Infof("warning: Failed to kill container %q: %v", container.ID, killErr)
} else {
glog.V(4).Infof("Killed container %q", container.ID)
}

glog.V(4).Infof("Removing container %q ...", container.ID)
if removeErr := d.RemoveContainer(container.ID); removeErr != nil {
glog.V(0).Infof("warning: Failed to remove container %q: %v", container.ID, removeErr)
Expand Down Expand Up @@ -1066,7 +1080,6 @@ func (d *stiDocker) BuildImage(opts BuildImageOptions) error {
NoCache: true,
SuppressOutput: false,
Remove: true,
ForceRemove: true,
}
if opts.CGroupLimits != nil {
dockerOpts.Memory = opts.CGroupLimits.MemoryLimitBytes
Expand Down
5 changes: 5 additions & 0 deletions pkg/docker/fake_docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ func (f *FakeDocker) RemoveContainer(id string) error {
return f.RemoveContainerError
}

// KillContainer kills a fake container
func (f *FakeDocker) KillContainer(id string) error {
return nil
}

// GetScriptsURL returns a default STI scripts URL
func (f *FakeDocker) GetScriptsURL(image string) (string, error) {
f.DefaultURLImage = image
Expand Down
5 changes: 5 additions & 0 deletions pkg/docker/test/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,11 @@ func (d *FakeDockerClient) ContainerRemove(ctx context.Context, containerID stri
return errors.New("container does not exist")
}

// ContainerKill terminates the container process but does not remove the container from the docker host.
func (d *FakeDockerClient) ContainerKill(ctx context.Context, containerID, signal string) error {
return nil
}

// ContainerStart sends a request to the docker daemon to start a container.
func (d *FakeDockerClient) ContainerStart(ctx context.Context, containerID string) error {
d.Calls = append(d.Calls, "start")
Expand Down
2 changes: 1 addition & 1 deletion test/integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,9 +586,9 @@ func (i *integrationTest) runInContainer(image string, command []string) int {
func (i *integrationTest) removeContainer(cID string) {
ctx, cancel := getDefaultContext()
defer cancel()
i.engineClient.ContainerKill(ctx, cID, "SIGKILL")
removeOpts := dockertypes.ContainerRemoveOptions{
RemoveVolumes: true,
Force: true,
}
err := i.engineClient.ContainerRemove(ctx, cID, removeOpts)
if err != nil {
Expand Down

0 comments on commit 7e0fc9e

Please sign in to comment.