Browse code

runconfig/errors: split `ErrConflictHostNetwork`

Split the `ErrConflictHostNetwork` error into two distinct errors:

1. `ErrConflictConnectToHostNetwork` when attempting to change the
network mode of a running container from a different mode to `host`
2. `ErrConflictDisconnectFromHostNetwork` when the network mode of a
running container is `host` and attempting to disconnect from `host`

This commit clarifies error messaging by differentiating between the two
errors, making it clearer which operation failed and how to fix it.

Signed-off-by: Brendon Smith <bws@bws.bio>

Brendon Smith authored on 2025/03/09 07:13:20
Showing 3 changed files
... ...
@@ -173,7 +173,7 @@ func (daemon *Daemon) updateNetworkSettings(ctr *container.Container, n *libnetw
173 173
 	}
174 174
 
175 175
 	if !ctr.HostConfig.NetworkMode.IsHost() && containertypes.NetworkMode(n.Type()).IsHost() {
176
-		return runconfig.ErrConflictHostNetwork
176
+		return runconfig.ErrConflictConnectToHostNetwork
177 177
 	}
178 178
 
179 179
 	for s, v := range ctr.NetworkSettings.Networks {
... ...
@@ -1053,7 +1053,7 @@ func (daemon *Daemon) DisconnectFromNetwork(ctx context.Context, ctr *container.
1053 1053
 		delete(ctr.NetworkSettings.Networks, networkName)
1054 1054
 	} else if err == nil {
1055 1055
 		if ctr.HostConfig.NetworkMode.IsHost() && containertypes.NetworkMode(n.Type()).IsHost() {
1056
-			return runconfig.ErrConflictHostNetwork
1056
+			return runconfig.ErrConflictDisconnectFromHostNetwork
1057 1057
 		}
1058 1058
 
1059 1059
 		if err := daemon.disconnectFromNetwork(ctx, ctr, n, false); err != nil {
... ...
@@ -1155,7 +1155,7 @@ func (s *DockerNetworkSuite) TestDockerNetworkConnectToHostFromOtherNetwork(c *t
1155 1155
 	cli.DockerCmd(c, "network", "disconnect", "bridge", "container1")
1156 1156
 	out, _, err := dockerCmdWithError("network", "connect", "host", "container1")
1157 1157
 	assert.ErrorContains(c, err, "", out)
1158
-	assert.Assert(c, is.Contains(out, runconfig.ErrConflictHostNetwork.Error()))
1158
+	assert.Assert(c, is.Contains(out, runconfig.ErrConflictConnectToHostNetwork.Error()))
1159 1159
 }
1160 1160
 
1161 1161
 func (s *DockerNetworkSuite) TestDockerNetworkDisconnectFromHost(c *testing.T) {
... ...
@@ -1163,7 +1163,7 @@ func (s *DockerNetworkSuite) TestDockerNetworkDisconnectFromHost(c *testing.T) {
1163 1163
 	cli.WaitRun(c, "container1")
1164 1164
 	out, _, err := dockerCmdWithError("network", "disconnect", "host", "container1")
1165 1165
 	assert.Assert(c, err != nil, "Should err out disconnect from host")
1166
-	assert.Assert(c, is.Contains(out, runconfig.ErrConflictHostNetwork.Error()))
1166
+	assert.Assert(c, is.Contains(out, runconfig.ErrConflictDisconnectFromHostNetwork.Error()))
1167 1167
 }
1168 1168
 
1169 1169
 func (s *DockerNetworkSuite) TestDockerNetworkConnectWithPortMapping(c *testing.T) {
... ...
@@ -5,8 +5,10 @@ const (
5 5
 	ErrConflictContainerNetworkAndLinks validationError = "conflicting options: container type network can't be used with links. This would result in undefined behavior"
6 6
 	// ErrConflictSharedNetwork conflict between private and other networks
7 7
 	ErrConflictSharedNetwork validationError = "container sharing network namespace with another container or host cannot be connected to any other network"
8
-	// ErrConflictHostNetwork conflict from being disconnected from host network or connected to host network.
9
-	ErrConflictHostNetwork validationError = "container cannot be disconnected from host network or connected to host network"
8
+	// ErrConflictConnectToHostNetwork error when attempting to connect a container to host network when not in host network mode
9
+	ErrConflictConnectToHostNetwork validationError = "cannot connect container to host network - container must be created in host network mode"
10
+	// ErrConflictDisconnectFromHostNetwork error when attempting to disconnect a container from host network when in host network mode
11
+	ErrConflictDisconnectFromHostNetwork validationError = "cannot disconnect container from host network - container was created in host network mode"
10 12
 	// ErrConflictNoNetwork conflict between private and other networks
11 13
 	ErrConflictNoNetwork validationError = "container cannot be connected to multiple networks with one of the networks in private (none) mode"
12 14
 	// ErrConflictNetworkAndDNS conflict between --dns and the network mode