mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Merge pull request #17489 from WeiZhang555/network-rm-multi
Enhance `docker network rm` to delete multi net
This commit is contained in:
commit
a600bf4eab
4 changed files with 80 additions and 18 deletions
|
@ -87,19 +87,28 @@ func (cli *DockerCli) CmdNetworkCreate(args ...string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// CmdNetworkRm deletes a network
|
// CmdNetworkRm deletes one or more networks
|
||||||
//
|
//
|
||||||
// Usage: docker network rm <NETWORK-NAME | NETWORK-ID>
|
// Usage: docker network rm NETWORK-NAME|NETWORK-ID [NETWORK-NAME|NETWORK-ID...]
|
||||||
func (cli *DockerCli) CmdNetworkRm(args ...string) error {
|
func (cli *DockerCli) CmdNetworkRm(args ...string) error {
|
||||||
cmd := Cli.Subcmd("network rm", []string{"NETWORK"}, "Deletes a network", false)
|
cmd := Cli.Subcmd("network rm", []string{"NETWORK [NETWORK...]"}, "Deletes one or more networks", false)
|
||||||
cmd.Require(flag.Exact, 1)
|
cmd.Require(flag.Min, 1)
|
||||||
err := cmd.ParseFlags(args, true)
|
err := cmd.ParseFlags(args, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
_, _, err = readBody(cli.call("DELETE", "/networks/"+cmd.Arg(0), nil, nil))
|
|
||||||
if err != nil {
|
status := 0
|
||||||
return err
|
for _, net := range cmd.Args() {
|
||||||
|
_, _, err = readBody(cli.call("DELETE", "/networks/"+net, nil, nil))
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(cli.err, "%s\n", err)
|
||||||
|
status = 1
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if status != 0 {
|
||||||
|
return Cli.StatusError{StatusCode: status}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -193,7 +202,7 @@ func (cli *DockerCli) CmdNetworkLs(args ...string) error {
|
||||||
//
|
//
|
||||||
// Usage: docker network inspect [OPTIONS] <NETWORK> [NETWORK...]
|
// Usage: docker network inspect [OPTIONS] <NETWORK> [NETWORK...]
|
||||||
func (cli *DockerCli) CmdNetworkInspect(args ...string) error {
|
func (cli *DockerCli) CmdNetworkInspect(args ...string) error {
|
||||||
cmd := Cli.Subcmd("network inspect", []string{"NETWORK [NETWORK...]"}, "Displays detailed information on a network", false)
|
cmd := Cli.Subcmd("network inspect", []string{"NETWORK [NETWORK...]"}, "Displays detailed information on one or more networks", false)
|
||||||
cmd.Require(flag.Min, 1)
|
cmd.Require(flag.Min, 1)
|
||||||
err := cmd.ParseFlags(args, true)
|
err := cmd.ParseFlags(args, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -208,7 +217,7 @@ func (cli *DockerCli) CmdNetworkInspect(args ...string) error {
|
||||||
if strings.Contains(err.Error(), "not found") {
|
if strings.Contains(err.Error(), "not found") {
|
||||||
fmt.Fprintf(cli.err, "Error: No such network: %s\n", name)
|
fmt.Fprintf(cli.err, "Error: No such network: %s\n", name)
|
||||||
} else {
|
} else {
|
||||||
fmt.Fprintf(cli.err, "%s", err)
|
fmt.Fprintf(cli.err, "%s\n", err)
|
||||||
}
|
}
|
||||||
status = 1
|
status = 1
|
||||||
continue
|
continue
|
||||||
|
|
|
@ -10,18 +10,33 @@ parent = "smn_cli"
|
||||||
|
|
||||||
# network rm
|
# network rm
|
||||||
|
|
||||||
Usage: docker network rm [OPTIONS] NAME | ID
|
Usage: docker network rm [OPTIONS] NETWORK [NETWORK...]
|
||||||
|
|
||||||
Deletes a network
|
Deletes one or more networks
|
||||||
|
|
||||||
--help=false Print usage
|
--help=false Print usage
|
||||||
|
|
||||||
Removes a network by name or identifier. To remove a network, you must first disconnect any containers connected to it.
|
Removes one or more networks by name or identifier. To remove a network,
|
||||||
|
you must first disconnect any containers connected to it.
|
||||||
|
To remove the network named 'my-network':
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
$ docker network rm my-network
|
$ docker network rm my-network
|
||||||
```
|
```
|
||||||
|
|
||||||
|
To delete multiple networks in a single `docker network rm` command, provide
|
||||||
|
multiple network names or id's. The following example deletes a network with id
|
||||||
|
`3695c422697f` and a network named `my-network`:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
$ docker network rm 3695c422697f my-network
|
||||||
|
```
|
||||||
|
|
||||||
|
When you specify multiple networks, the command attempts to delete each in turn.
|
||||||
|
If the deletion of one network fails, the command continues to the next on the
|
||||||
|
list and tries to delete that. The command reports success or failure for each
|
||||||
|
deletion.
|
||||||
|
|
||||||
## Related information
|
## Related information
|
||||||
|
|
||||||
* [network disconnect ](network_disconnect.md)
|
* [network disconnect ](network_disconnect.md)
|
||||||
|
|
|
@ -270,6 +270,29 @@ func (s *DockerSuite) TestDockerNetworkDeleteNotExists(c *check.C) {
|
||||||
c.Assert(err, checker.NotNil, check.Commentf("%v", out))
|
c.Assert(err, checker.NotNil, check.Commentf("%v", out))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *DockerSuite) TestDockerNetworkDeleteMultiple(c *check.C) {
|
||||||
|
dockerCmd(c, "network", "create", "testDelMulti0")
|
||||||
|
assertNwIsAvailable(c, "testDelMulti0")
|
||||||
|
dockerCmd(c, "network", "create", "testDelMulti1")
|
||||||
|
assertNwIsAvailable(c, "testDelMulti1")
|
||||||
|
dockerCmd(c, "network", "create", "testDelMulti2")
|
||||||
|
assertNwIsAvailable(c, "testDelMulti2")
|
||||||
|
out, _ := dockerCmd(c, "run", "-d", "--net", "testDelMulti2", "busybox", "top")
|
||||||
|
waitRun(strings.TrimSpace(out))
|
||||||
|
|
||||||
|
// delete three networks at the same time, since testDelMulti2
|
||||||
|
// contains active container, it's deletion should fail.
|
||||||
|
out, _, err := dockerCmdWithError("network", "rm", "testDelMulti0", "testDelMulti1", "testDelMulti2")
|
||||||
|
// err should not be nil due to deleting testDelMulti2 failed.
|
||||||
|
c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
|
||||||
|
// testDelMulti2 should fail due to network has active endpoints
|
||||||
|
c.Assert(out, checker.Contains, "has active endpoints")
|
||||||
|
assertNwNotAvailable(c, "testDelMulti0")
|
||||||
|
assertNwNotAvailable(c, "testDelMulti1")
|
||||||
|
// testDelMulti2 can't be deleted, so it should exists
|
||||||
|
assertNwIsAvailable(c, "testDelMulti2")
|
||||||
|
}
|
||||||
|
|
||||||
func (s *DockerSuite) TestDockerInspectMultipleNetwork(c *check.C) {
|
func (s *DockerSuite) TestDockerInspectMultipleNetwork(c *check.C) {
|
||||||
out, _ := dockerCmd(c, "network", "inspect", "host", "none")
|
out, _ := dockerCmd(c, "network", "inspect", "host", "none")
|
||||||
networkResources := []types.NetworkResource{}
|
networkResources := []types.NetworkResource{}
|
||||||
|
|
|
@ -2,24 +2,39 @@
|
||||||
% Docker Community
|
% Docker Community
|
||||||
% OCT 2015
|
% OCT 2015
|
||||||
# NAME
|
# NAME
|
||||||
docker-network-rm - remove a new network
|
docker-network-rm - remove one or more networks
|
||||||
|
|
||||||
# SYNOPSIS
|
# SYNOPSIS
|
||||||
**docker network rm**
|
**docker network rm**
|
||||||
[**--help**]
|
[**--help**]
|
||||||
NETWORK
|
NETWORK [NETWORK...]
|
||||||
|
|
||||||
# DESCRIPTION
|
# DESCRIPTION
|
||||||
|
|
||||||
Removes a network by name or identifier. To remove a network, you must first disconnect any containers connected to it.
|
Removes one or more networks by name or identifier. To remove a network,
|
||||||
|
you must first disconnect any containers connected to it.
|
||||||
|
To remove the network named 'my-network':
|
||||||
|
|
||||||
```
|
```bash
|
||||||
$ docker network rm my-network
|
$ docker network rm my-network
|
||||||
```
|
```
|
||||||
|
|
||||||
|
To delete multiple networks in a single `docker network rm` command, provide
|
||||||
|
multiple network names or id's. The following example deletes a network with id
|
||||||
|
`3695c422697f` and a network named `my-network`:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
$ docker network rm 3695c422697f my-network
|
||||||
|
```
|
||||||
|
|
||||||
|
When you specify multiple networks, the command attempts to delete each in turn.
|
||||||
|
If the deletion of one network fails, the command continues to the next on the
|
||||||
|
list and tries to delete that. The command reports success or failure for each
|
||||||
|
deletion.
|
||||||
|
|
||||||
# OPTIONS
|
# OPTIONS
|
||||||
**NETWORK**
|
**NETWORK**
|
||||||
Specify network name
|
Specify network name or id
|
||||||
|
|
||||||
**--help**
|
**--help**
|
||||||
Print usage statement
|
Print usage statement
|
||||||
|
|
Loading…
Reference in a new issue