Browse code

Forbid update restart policy of container with AutoRemove flag

"--restart" and "--rm" are conflict options, if a container is started
with AutoRemove flag, we should forbid the update action for its Restart
Policy.

Signed-off-by: Zhang Wei <zhangwei555@huawei.com>

Zhang Wei authored on 2016/08/15 17:38:47
Showing 7 changed files
... ...
@@ -95,6 +95,9 @@ func runRun(dockerCli *client.DockerCli, flags *pflag.FlagSet, opts *runOptions,
95 95
 		return cli.StatusError{StatusCode: 125}
96 96
 	}
97 97
 
98
+	if hostConfig.AutoRemove && !hostConfig.RestartPolicy.IsNone() {
99
+		return ErrConflictRestartPolicyAndAutoRemove
100
+	}
98 101
 	if hostConfig.OomKillDisable != nil && *hostConfig.OomKillDisable && hostConfig.Memory == 0 {
99 102
 		fmt.Fprintf(stderr, "WARNING: Disabling the OOM killer on containers without setting a '-m/--memory' limit may be dangerous.\n")
100 103
 	}
... ...
@@ -166,9 +169,6 @@ func runRun(dockerCli *client.DockerCli, flags *pflag.FlagSet, opts *runOptions,
166 166
 			fmt.Fprintf(stdout, "%s\n", createResponse.ID)
167 167
 		}()
168 168
 	}
169
-	if hostConfig.AutoRemove && !hostConfig.RestartPolicy.IsNone() {
170
-		return ErrConflictRestartPolicyAndAutoRemove
171
-	}
172 169
 	attach := config.AttachStdin || config.AttachStdout || config.AttachStderr
173 170
 	if attach {
174 171
 		var (
... ...
@@ -302,6 +302,9 @@ func (container *Container) UpdateContainer(hostConfig *containertypes.HostConfi
302 302
 
303 303
 	// update HostConfig of container
304 304
 	if hostConfig.RestartPolicy.Name != "" {
305
+		if container.HostConfig.AutoRemove && !hostConfig.RestartPolicy.IsNone() {
306
+			return fmt.Errorf("Restart policy cannot be updated because AutoRemove is enabled for the container")
307
+		}
305 308
 		container.HostConfig.RestartPolicy = hostConfig.RestartPolicy
306 309
 	}
307 310
 
... ...
@@ -72,6 +72,9 @@ func (container *Container) UpdateContainer(hostConfig *containertypes.HostConfi
72 72
 	}
73 73
 	// update HostConfig of container
74 74
 	if hostConfig.RestartPolicy.Name != "" {
75
+		if container.HostConfig.AutoRemove && !hostConfig.RestartPolicy.IsNone() {
76
+			return fmt.Errorf("Restart policy cannot be updated because AutoRemove is enabled for the container")
77
+		}
75 78
 		container.HostConfig.RestartPolicy = hostConfig.RestartPolicy
76 79
 	}
77 80
 	return nil
... ...
@@ -460,7 +460,7 @@ Create a container
460 460
             An ever increasing delay (double the previous delay, starting at 100mS)
461 461
             is added before each restart to prevent flooding the server.
462 462
     -   **AutoRemove** - Boolean value, set to `true` to automatically remove the container on daemon side
463
-            when the container's process exits.
463
+            when the container's process exits. Note that `RestartPolicy` other than `none` is exclusive to `AutoRemove`.
464 464
     -   **UsernsMode**  - Sets the usernamespace mode for the container when usernamespace remapping option is enabled.
465 465
            supported values are: `host`.
466 466
     -   **NetworkMode** - Sets the networking mode for the container. Supported
... ...
@@ -107,3 +107,7 @@ To update restart policy for one or more containers:
107 107
 ```bash
108 108
 $ docker update --restart=on-failure:3 abebf7571666 hopeful_morse
109 109
 ```
110
+
111
+Note that if the container is started with "--rm" flag, you cannot update the restart
112
+policy for it. The `AutoRemove` and `RestartPolicy` are mutually exclusive for the
113
+container.
... ...
@@ -29,3 +29,13 @@ func (s *DockerSuite) TestUpdateRestartPolicy(c *check.C) {
29 29
 	maximumRetryCount := inspectField(c, id, "HostConfig.RestartPolicy.MaximumRetryCount")
30 30
 	c.Assert(maximumRetryCount, checker.Equals, "5")
31 31
 }
32
+
33
+func (s *DockerSuite) TestUpdateRestartWithAutoRemoveFlag(c *check.C) {
34
+	out, _ := runSleepingContainer(c, "--rm")
35
+	id := strings.TrimSpace(out)
36
+
37
+	// update restart policy for an AutoRemove container
38
+	out, _, err := dockerCmdWithError("update", "--restart=always", id)
39
+	c.Assert(err, checker.NotNil)
40
+	c.Assert(out, checker.Contains, "Restart policy cannot be updated because AutoRemove is enabled for the container")
41
+}
... ...
@@ -148,3 +148,7 @@ To update restart policy for one or more containers:
148 148
 ```bash
149 149
 $ docker update --restart=on-failure:3 abebf7571666 hopeful_morse
150 150
 ```
151
+
152
+Note that if the container is started with "--rm" flag, you cannot update the restart
153
+policy for it. The `AutoRemove` and `RestartPolicy` are mutually exclusive for the
154
+container.