mirror of
				https://github.com/moby/moby.git
				synced 2022-11-09 12:21:53 -05:00 
			
		
		
		
	Merge pull request #9356 from cc272309126/fix-exec-paused-container
Fix the issue that when docker exec a paused container, it will always
This commit is contained in:
		
						commit
						bb24f99d74
					
				
					 3 changed files with 76 additions and 1 deletions
				
			
		| 
						 | 
				
			
			@ -98,7 +98,9 @@ func (d *Daemon) getActiveContainer(name string) (*Container, error) {
 | 
			
		|||
	if !container.IsRunning() {
 | 
			
		||||
		return nil, fmt.Errorf("Container %s is not running", name)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if container.IsPaused() {
 | 
			
		||||
		return nil, fmt.Errorf("Container %s is paused, unpause the container before exec", name)
 | 
			
		||||
	}
 | 
			
		||||
	return container, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -230,3 +230,36 @@ func TestExecExitStatus(t *testing.T) {
 | 
			
		|||
 | 
			
		||||
	logDone("exec - exec non-zero ExitStatus")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestExecPausedContainer(t *testing.T) {
 | 
			
		||||
 | 
			
		||||
	defer deleteAllContainers()
 | 
			
		||||
	defer unpauseAllContainers()
 | 
			
		||||
 | 
			
		||||
	runCmd := exec.Command(dockerBinary, "run", "-d", "--name", "testing", "busybox", "top")
 | 
			
		||||
	out, _, err := runCommandWithOutput(runCmd)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		t.Fatal(out, err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	ContainerID := stripTrailingCharacters(out)
 | 
			
		||||
 | 
			
		||||
	pausedCmd := exec.Command(dockerBinary, "pause", "testing")
 | 
			
		||||
	out, _, _, err = runCommandWithStdoutStderr(pausedCmd)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		t.Fatal(out, err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	execCmd := exec.Command(dockerBinary, "exec", "-i", "-t", ContainerID, "echo", "hello")
 | 
			
		||||
	out, _, err = runCommandWithOutput(execCmd)
 | 
			
		||||
	if err == nil {
 | 
			
		||||
		t.Fatal("container should fail to exec new command if it is paused")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	expected := ContainerID + " is paused, unpause the container before exec"
 | 
			
		||||
	if !strings.Contains(out, expected) {
 | 
			
		||||
		t.Fatal("container should not exec new command if it is paused")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	logDone("exec - exec should not exec a pause container")
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -328,6 +328,46 @@ func deleteAllContainers() error {
 | 
			
		|||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func getPausedContainers() (string, error) {
 | 
			
		||||
	getPausedContainersCmd := exec.Command(dockerBinary, "ps", "-f", "status=paused", "-q", "-a")
 | 
			
		||||
	out, exitCode, err := runCommandWithOutput(getPausedContainersCmd)
 | 
			
		||||
	if exitCode != 0 && err == nil {
 | 
			
		||||
		err = fmt.Errorf("failed to get a list of paused containers: %v\n", out)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return out, err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func unpauseContainer(container string) error {
 | 
			
		||||
	unpauseCmd := exec.Command(dockerBinary, "unpause", container)
 | 
			
		||||
	exitCode, err := runCommand(unpauseCmd)
 | 
			
		||||
	if exitCode != 0 && err == nil {
 | 
			
		||||
		err = fmt.Errorf("failed to unpause container")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func unpauseAllContainers() error {
 | 
			
		||||
	containers, err := getPausedContainers()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		fmt.Println(containers)
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	containers = strings.Replace(containers, "\n", " ", -1)
 | 
			
		||||
	containers = strings.Trim(containers, " ")
 | 
			
		||||
	containerList := strings.Split(containers, " ")
 | 
			
		||||
 | 
			
		||||
	for _, value := range containerList {
 | 
			
		||||
		if err = unpauseContainer(value); err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func deleteImages(images ...string) error {
 | 
			
		||||
	args := make([]string, 1, 2)
 | 
			
		||||
	args[0] = "rmi"
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue