Ver código fonte

Merge pull request #25701 from WeiZhang555/update-conflict-rm-restart

Forbid update restart policy of container with AutoRemove flag
Tõnis Tiigi 9 anos atrás
pai
commit
9a3e7d9e34

+ 3 - 3
api/client/container/run.go

@@ -95,6 +95,9 @@ func runRun(dockerCli *client.DockerCli, flags *pflag.FlagSet, opts *runOptions,
 		return cli.StatusError{StatusCode: 125}
 		return cli.StatusError{StatusCode: 125}
 	}
 	}
 
 
+	if hostConfig.AutoRemove && !hostConfig.RestartPolicy.IsNone() {
+		return ErrConflictRestartPolicyAndAutoRemove
+	}
 	if hostConfig.OomKillDisable != nil && *hostConfig.OomKillDisable && hostConfig.Memory == 0 {
 	if hostConfig.OomKillDisable != nil && *hostConfig.OomKillDisable && hostConfig.Memory == 0 {
 		fmt.Fprintf(stderr, "WARNING: Disabling the OOM killer on containers without setting a '-m/--memory' limit may be dangerous.\n")
 		fmt.Fprintf(stderr, "WARNING: Disabling the OOM killer on containers without setting a '-m/--memory' limit may be dangerous.\n")
 	}
 	}
@@ -166,9 +169,6 @@ func runRun(dockerCli *client.DockerCli, flags *pflag.FlagSet, opts *runOptions,
 			fmt.Fprintf(stdout, "%s\n", createResponse.ID)
 			fmt.Fprintf(stdout, "%s\n", createResponse.ID)
 		}()
 		}()
 	}
 	}
-	if hostConfig.AutoRemove && !hostConfig.RestartPolicy.IsNone() {
-		return ErrConflictRestartPolicyAndAutoRemove
-	}
 	attach := config.AttachStdin || config.AttachStdout || config.AttachStderr
 	attach := config.AttachStdin || config.AttachStdout || config.AttachStderr
 	if attach {
 	if attach {
 		var (
 		var (

+ 3 - 0
container/container_unix.go

@@ -302,6 +302,9 @@ func (container *Container) UpdateContainer(hostConfig *containertypes.HostConfi
 
 
 	// update HostConfig of container
 	// update HostConfig of container
 	if hostConfig.RestartPolicy.Name != "" {
 	if hostConfig.RestartPolicy.Name != "" {
+		if container.HostConfig.AutoRemove && !hostConfig.RestartPolicy.IsNone() {
+			return fmt.Errorf("Restart policy cannot be updated because AutoRemove is enabled for the container")
+		}
 		container.HostConfig.RestartPolicy = hostConfig.RestartPolicy
 		container.HostConfig.RestartPolicy = hostConfig.RestartPolicy
 	}
 	}
 
 

+ 3 - 0
container/container_windows.go

@@ -72,6 +72,9 @@ func (container *Container) UpdateContainer(hostConfig *containertypes.HostConfi
 	}
 	}
 	// update HostConfig of container
 	// update HostConfig of container
 	if hostConfig.RestartPolicy.Name != "" {
 	if hostConfig.RestartPolicy.Name != "" {
+		if container.HostConfig.AutoRemove && !hostConfig.RestartPolicy.IsNone() {
+			return fmt.Errorf("Restart policy cannot be updated because AutoRemove is enabled for the container")
+		}
 		container.HostConfig.RestartPolicy = hostConfig.RestartPolicy
 		container.HostConfig.RestartPolicy = hostConfig.RestartPolicy
 	}
 	}
 	return nil
 	return nil

+ 1 - 1
docs/reference/api/docker_remote_api_v1.25.md

@@ -460,7 +460,7 @@ Create a container
             An ever increasing delay (double the previous delay, starting at 100mS)
             An ever increasing delay (double the previous delay, starting at 100mS)
             is added before each restart to prevent flooding the server.
             is added before each restart to prevent flooding the server.
     -   **AutoRemove** - Boolean value, set to `true` to automatically remove the container on daemon side
     -   **AutoRemove** - Boolean value, set to `true` to automatically remove the container on daemon side
-            when the container's process exits.
+            when the container's process exits. Note that `RestartPolicy` other than `none` is exclusive to `AutoRemove`.
     -   **UsernsMode**  - Sets the usernamespace mode for the container when usernamespace remapping option is enabled.
     -   **UsernsMode**  - Sets the usernamespace mode for the container when usernamespace remapping option is enabled.
            supported values are: `host`.
            supported values are: `host`.
     -   **NetworkMode** - Sets the networking mode for the container. Supported
     -   **NetworkMode** - Sets the networking mode for the container. Supported

+ 4 - 0
docs/reference/commandline/update.md

@@ -107,3 +107,7 @@ To update restart policy for one or more containers:
 ```bash
 ```bash
 $ docker update --restart=on-failure:3 abebf7571666 hopeful_morse
 $ docker update --restart=on-failure:3 abebf7571666 hopeful_morse
 ```
 ```
+
+Note that if the container is started with "--rm" flag, you cannot update the restart
+policy for it. The `AutoRemove` and `RestartPolicy` are mutually exclusive for the
+container.

+ 10 - 0
integration-cli/docker_cli_update_test.go

@@ -29,3 +29,13 @@ func (s *DockerSuite) TestUpdateRestartPolicy(c *check.C) {
 	maximumRetryCount := inspectField(c, id, "HostConfig.RestartPolicy.MaximumRetryCount")
 	maximumRetryCount := inspectField(c, id, "HostConfig.RestartPolicy.MaximumRetryCount")
 	c.Assert(maximumRetryCount, checker.Equals, "5")
 	c.Assert(maximumRetryCount, checker.Equals, "5")
 }
 }
+
+func (s *DockerSuite) TestUpdateRestartWithAutoRemoveFlag(c *check.C) {
+	out, _ := runSleepingContainer(c, "--rm")
+	id := strings.TrimSpace(out)
+
+	// update restart policy for an AutoRemove container
+	out, _, err := dockerCmdWithError("update", "--restart=always", id)
+	c.Assert(err, checker.NotNil)
+	c.Assert(out, checker.Contains, "Restart policy cannot be updated because AutoRemove is enabled for the container")
+}

+ 4 - 0
man/docker-update.1.md

@@ -148,3 +148,7 @@ To update restart policy for one or more containers:
 ```bash
 ```bash
 $ docker update --restart=on-failure:3 abebf7571666 hopeful_morse
 $ docker update --restart=on-failure:3 abebf7571666 hopeful_morse
 ```
 ```
+
+Note that if the container is started with "--rm" flag, you cannot update the restart
+policy for it. The `AutoRemove` and `RestartPolicy` are mutually exclusive for the
+container.